2014-09-24 209 views
0

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


     } 

回答

0

不能调用从这样的客户端服务器的方法。你将不得不使用AJAX包装呼叫。

的Javascript已经有一个ToUpper的方法...

http://www.w3schools.com/jsref/jsref_touppercase.asp

+0

我纠正它,我可以打电话,但视图状态的值变为空 – user3930037 2014-09-24 14:15:25

+0

我很困惑你是怎么调用这个...你的意思是说,在浏览器中,你点击一个文本框,然后从文本框中,然后看到错误?另外,AddNewRowToGrid()的代码在哪里? – Scottie 2014-09-24 14:24:18

+0

textbox是在网格view.I是实际的错误是在AddNewRowToGrid();有视图状态变为null。是因为静态方法 – user3930037 2014-09-24 14:28:13

0

的ViewState内不可webMethods的 - 当回传从网页上出现与VIEWSTATE隐藏字段可用的ViewState才可用。

WebMethods不要求ViewState被POST,并且他们不创建一个Page对象的实例。因此,您需要使用另一种机制,例如Session(尽管这有其自身的问题,特别是在负载平衡的环境中),以便将您当前依赖的数据存储在ViewState中。

1

如果您的任务是利用该文本框中的所有文本,则有更简单的方法来执行此操作。

方法1

这是最简单的,因为它不需要编码,只是一个CSS规则。在你head部分,补充一点:

<head runat="server"> 
    <style type="text/css"> 
     .upper { 
      text-transform: uppercase; 
     } 
    </style> 
</head> 

而在你的模板字段,你的行改成这样:

<asp:TextBox ID="txtQuantity" CssClass="upper" runat="server" Height="20px" Width="150px" onblur="CallingServerSideFunction()" > </asp:TextBox> 

如果您有其他的文本框也需要资本,再加上一点upper类它和中提琴!

方法2

鉴于方法1将利用字母作为用户的类型,方法2将利用用户的标签远离之后的字母。如果你的项目还没有它,你将需要jQuery。在你的aspx页面的底部,收盘body标签之前,补充一点:

<script type="text/javascript"> 
    $("#<%= GridView1.ClientID %> input[id*='txtQuantity']").focusout(function() { 
     $(this).val($(this).val().toUpperCase()); 
    }); 
</script> 

我不知道你的网格视图名称是的,但你的网格视图的任何名称替换GridView1是。如果您有其他需要大写的文本框,只需复制并粘贴此代码,并用TextBox的任何id代替txtQuantity。中提琴!

+0

嗨Touper是一种方法,我不会将文本转换为上层类 – user3930037 2014-09-25 06:04:21

相关问题