2014-09-24 108 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

相關問題