簡體   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