В приведенном ниже коде у меня есть текстовое поле внутри сетки на событии onblur. Я хочу вызвать метод на стороне сервера. Я попытался вызвать метод, но в статическом методе он выдает ошибку. view, которое становится null.Как получить значение viewstate? Pls поможет мне решить проблему.Вызов метода с клиентской стороны
Javascript:
function CallingServerSideFunction() {
PageMethods.ToUpper(CallSuccess, CallError);
function CallSuccess(res) {
alert(res);
}
function CallError() {
alert('Error');
}
}
Codebehind:
[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();
}
Я исправил это, я могу позвонить, но viewstate значение становится null – user3930037
Я запутался, как вы это называете вообще ...Вы имеете в виду, что в браузере вы нажимаете на текстовое поле, затем вне текстового поля, а затем видите ошибку? Кроме того, где ваш код для AddNewRowToGrid()? – Scottie
текстовое поле находится внутри сетки. Является фактической ошибкой внутри AddNewRowToGrid(); есть состояние представления, которое становится null.Is это из-за статического метода – user3930037