从客户端调用方法

时间:2014-09-24 14:01:10

标签: c# asp.net

在下面的代码中,我在onblur事件的网格视图中有一个文本框我想在服务器端调用一个方法我试图调用一个方法但是在静态方法中它会抛出错误。它有一个视图状态变为null。如何检索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();


        }

3 个答案:

答案 0 :(得分: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,你将需要jQuery。在aspx页面的底部,在结束body标记之前,添加以下内容:

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

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

答案 1 :(得分:0)

您无法像这样从客户端调用服务器方法。你必须使用AJAX包装调用。

Javascript已经有了ToUpper方法......

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

答案 2 :(得分:0)

ViewState在webmethods中不可用 - 只有在网页发生回发并且VIEWSTATE隐藏字段可用时,ViewState才可用。

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