我有註冊表單和按鈕。 OnClick - 我在服務器端調用函數,用Zipcodes在數據庫中驗證用戶的郵政編碼。如果驗證通過成功 - 用戶數據存儲在數據庫中(這裏我繼續使用服務器功能)。但是,如果ZipCode不匹配 - 我調用Javascript函數,如果用戶仍然想要將他的數據保存到數據庫。如果是 - 我使用Ajax請求保存它。問題是當我調用Javascript函數時 - 首先它應該在客戶端接收用戶數據。但是,當讀數據發生時 - 我收到一個錯誤「無法獲取未定義或空引用的屬性值」。但用戶的數據仍然存在於表單的字段中。看起來服務器從表單讀取的數據 - 在某處重置 - 並且不能在客戶機上第二次讀取。 這裏是我的ASP形式無法獲取未定義或空引用的屬性'值'
<body>
<form id="frmZipValidation" runat="server">
<div>
<asp:Label runat="server">Registration Form</asp:Label>
<asp:TextBox runat="server" ID="txtbxName"></asp:TextBox>
<asp:TextBox runat="server" ID="txtbxZipCode"></asp:TextBox>
<asp:DropDownList runat="server" ID="DDLCountry">
<asp:ListItem Text="Select country" Value="Select" Selected="True"></asp:ListItem>
<asp:ListItem Text="USA" Value="USA"></asp:ListItem>
<asp:ListItem Text="Canada" Value="Canada"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="txtbxState"></asp:TextBox>
<asp:TextBox runat="server" ID="txtbxCity"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click"/>
</div>
</form>
</body>
這裏是我的服務器端
public partial class Default : System.Web.UI.Page
{
string Name;
string ZipCode;
string Country;
string State;
string City;
bool IsMatch;
Addresses dbAddresses = new Addresses();
User newUser;
protected void Page_Load(object sender, EventArgs e)
{
if (Request["Action"] != null && Request["Action"].Trim() != "")
{
if (Request["Action"] == "AddUser")
{
AddUser(Request["Name"], Request["ZipCode"], Request["Country"], Request["State"], Request["City"]);
}
}
}
private void AddUser(string UserName, string UserZip, string UserCountry, string UserState, string UserCity)
{
newUser = new User(UserName, UserZip, UserCountry, UserState, UserCity);
dbAddresses.Users.Add(newUser);
dbAddresses.SaveChanges();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValid)
{
ZipCode = txtbxZipCode.Text;
Country = DDLCountry.Text;
State = txtbxState.Text;
City = txtbxCity.Text;
Name = txtbxName.Text;
IsMatch = false;
List<ZipCode> ZipC = (from z in dbAddresses.Zips
where z.Zip == ZipCode
select z).ToList();
//If ZipCode entered by client do not exists at Database return false
if (!ZipC.Any())
{
IsMatch = false;
}
else
{
for (int i = 0; i < ZipC.Count; i++)
{
if (ZipC[i].Country.ToString() == Country)
{
if (ZipC[i].State.ToString() == State)
{
if (ZipC[i].City.ToString() == City)
{
AddUser(Name, ZipCode, Country, State, City);
//Message to the user that all saved successfully
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "1", "<script>alert('Your data was saved successfully!');</script>");
IsMatch = true;
break;
}
else
{
IsMatch = false;
break;
}
}
else
{
IsMatch = false;
break;
}
}
else
{
IsMatch = false;
break;
}
}
}
//If user's data are not match, then go to JS client code where - If user wants in any case to save data - make it using AJAX request
if (!IsMatch)
{
string clientScript = "AjaxRequestSaveToDB();";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "MyClientScript", clientScript);
}
}
}
}
這裏是使用Javascript:
function AjaxRequestSaveToDB()
{
var SaveData = confirm('Zip/Postal code doesn’t match region. Are you sure you want to save this data?');
if (SaveData)
{
var UserName = document.getElementById('txtbxName').value;
var UserZipCode = document.getElementById('txtbxZipCode').value;
var UserCountry = document.getElementById('DDLCountry').value;
var USerState = document.getElementById('txtbxState').value;
var UserCity = document.getElementById('txtbxCity').value;
SendDataToServer('AddUser', UserName, UserZipCode, UserCountry, USerState, UserCity);
alert("You data was saved successfully!");
}
else { alert('Not saved');
}
}
}
function SendDataToServer(RequestType, Name, ZipCode, Country, State, City)
{
var xmlHttp = getXmlHttp();
var Url = "Default.aspx?Action=" + escape(RequestType)
+ "&Name=" + escape(Name)
+ "&ZipCode=" + escape(ZipCode)
+ "&Country=" + escape(Country)
+ "&State=" + escape(State)
+ "&City=" + escape(City);
xmlHttp.open("GET", Url, true);
xmlHttp.send();
}
如果不是看到的是,郵政編碼是空的服務器,你用什麼一個html按鈕,並在按鈕被點擊後立即在客戶端執行檢查,然後在需要時從那裏觸發回發?我認爲這個問題是,ASP按鈕正在完成一個完整的頁面回發和刷新,所以當您在回發後調用javascript函數時,用戶輸入的所有值都遠遠超出頁面的範圍。 –
我嘗試從一開始就獲取客戶端的所有數據,然後將它們傳輸到服務器以驗證數據庫,但是當我需要切換回客戶端時 - 程序不會進行轉換。它只是不能識別JavaScript函數的調用代碼。我寫它爲:Page.ClientScript.RegisterClientScriptBlock(typeof(Page),「1」,「」);我甚至試着用簡單的「警報」 - 不起作用... – Pancheska
不幸的是,我認爲你最好的選擇是讓網頁做你想做的事情,而且爲了最簡化的用戶體驗將幾乎是一個完整的重寫,使用客戶端控件(可能帶有jQuery驗證),帶有XML內容的AJAX請求和一個asp.net處理程序頁面。當我獲得免費分鐘時,我會爲你寫出完整的答案。與此同時[本網站](http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp)相當徹底地超越了自定義ajax請求。 –