繁体   English   中英

从客户端调用方法

[英]To call a method from client side

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


        }

如果您的任务是将该文本框中的所有文本都大写,则可以使用更简单的方法。

方法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阶级添加到中提琴中即可!

方法二

方法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。 中提琴!

您不能像这样从客户端调用服务器方法。 您将不得不使用AJAX封装该调用。

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

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

ViewState在Web方法内部不可用-仅当从网页发生回发并且VIEWSTATE隐藏字段可用时,ViewState才可用。

WebMethod不需要发布ViewState,也不需要创建Page对象的实例。 因此,您将需要诸如Session之类的另一种机制(尽管这有其自身的问题,尤其是在负载平衡的环境中),以便将您当前依赖的数据存储在ViewState中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM