在下面的代碼中,我在onblur事件的網格視圖內部有一個文本框,我想調用服務器端的一個方法,我嘗試調用一個方法,但是在靜態方法中拋出error.It視圖狀態變爲空。如何檢索viewstate值?請幫我解決問題。從客戶端調用方法
的Javascript:
function CallingServerSideFunction() {
PageMethods.ToUpper(CallSuccess, CallError);
function CallSuccess(res) {
alert(res);
}
function CallError() {
alert('Error');
}
}
代碼隱藏:
[System.Web.Services.WebMethod]
public static void ToUpper()
{
AddNewRowToGrid();//throws error how can i call this method?
}
標記:
<asp:ScriptManager ID="newIndentScriptManager" EnablePageMethods="true" runat="server"></asp:ScriptManager>
<asp:TemplateField HeaderText="Quantity" ItemStyle-Width="150px">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Height="20px" Width="150px" onblur="CallingServerSideFunction()" > </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
DropDownList txtProductName = (DropDownList)gvProduct.Rows[rowIndex].Cells[0].FindControl("ddlProduct");
TextBox txtCurrentStock = (TextBox)gvProduct.Rows[rowIndex].Cells[1].FindControl("txtCurrentStock");
TextBox txtQuantity = (TextBox)gvProduct.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
TextBox txtProductRequiredDate = (TextBox)gvProduct.Rows[rowIndex].Cells[4].FindControl("txtProductRequiredDate");
Label txtUnitType = (Label)gvProduct.Rows[rowIndex].Cells[3].FindControl("lblunittype");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Column1"] = txtProductName.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = txtCurrentStock.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = txtQuantity.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = txtProductRequiredDate.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = txtUnitType.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
gvProduct.DataSource = dtCurrentTable;
gvProduct.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
我糾正它,我可以打電話,但視圖狀態的值變爲空 – user3930037 2014-09-24 14:15:25
我很困惑你是怎麼調用這個...你的意思是說,在瀏覽器中,你點擊一個文本框,然後從文本框中,然後看到錯誤?另外,AddNewRowToGrid()的代碼在哪裏? – Scottie 2014-09-24 14:24:18
textbox是在網格view.I是實際的錯誤是在AddNewRowToGrid();有視圖狀態變爲null。是因爲靜態方法 – user3930037 2014-09-24 14:28:13