在下面的代码中,我在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