2014-01-07 168 views
0

我有一個asp.net網站,我通過動態的方式在DataGrid中創建演示文件。 接下來,我必須檢查數量動態字段中的值是否爲空或者不是數字,或者是否定的,等等。Asp.net,jQuery和動態內容

我想檢查它,當我單擊發送按鈕時,我會喜歡用jQuery腳本來做到這一點;問題是:我如何檢索動態文本框的ID?在html頁面中,每個頁面都有一個前綴,我不想使用循環。 在例如:

protected void DettOrdGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     {... 
      e.Row.Cells.Add(cellQta); //here i add a cell to the row of the grid 
      ... 
     } 

在HTML頁面中:

$(document).ready(function){ 
     $("#btnSaveProvvisorio").delegate("textbox", "change", function(){ 
      var jtbNumBollaQuin; 
      jtbNumBollaQuin=$('#<%=myTextBoxQta.ClientID%>').text(); 
      alert("Valore quantita : " + jtbNumBollaQuin); 
     }) 
     } 

的錯誤是:

Compilation Error 
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'myTextBoxQta' does not exist in the current context 

謝謝!

Here the aspx.net code: 
protected void DettOrdGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 


... 


myTextBoxQta = new TextBox(); 
       myTextBoxQta.ID = "myTextBoxQta"; 
       myTextBoxQta.Attributes.Add("nomeTextBox", "myTextBoxQta"); 
       myTextBoxQta.Attributes.Add("runat", "server"); 
       myTextBoxQta.Attributes.Add("onChange", "javascript:TotaliOrdDett();"); 
       myTextBoxQta.Style["text-align"] = "center"; 


i = e.Row.Cells.Count; 
       i = i - 1; 
       //Column label 
       if (e.Row.RowIndex == -1) 
       { 
        if (risultato > 0) 
        {... 
        } 
else //...add a textbox 
       { 


        myOracleConnection = myDbClass.dbConnessione("myconn"); 
        //System.Diagnostics.Debug.WriteLine("Cella: " + e.Row.Cells[0].Text); 
        myQuery = "SELECT QTA_NR, KG_LORDI FROM TABLE WHERE SOC='1' AND ORD=" + ordHidden.Value + " AND ROW_ORD=" + e.Row.Cells[0].Text; 
        qtaKgInseriti = myDbClass.EseguiSqlSelect(myQuery, myOracleConnection, qtaKgInseriti); 
        myDbClass.dbDisconnessione(myOracleConnection); 


        e.Row.Cells[i].Controls.Add(myTextBoxQta); 
        cellQta.Controls.Add(myTextBoxQta); 
        e.Row.Cells.Add(cellQta); 
        //myTextBoxQta.Text = e.Row.RowIndex.ToString(); 
        if (risultato > 0) 
        { 
         myTextBoxQta.Text = qtaKgInseriti.Tables[0].Rows[0]["QTA_NR"].ToString(); 
         txtSumQta.Text = (Int32.Parse(txtSumQta.Text) + Int32.Parse(myTextBoxQta.Text)).ToString(); 
        } 
        e.Row.Cells[i].Controls.Add(myTextBoxKg); 
        cellKg.Controls.Add(myTextBoxKg); 
        e.Row.Cells.Add(cellKg); 
        //myTextBoxKg.Text = e.Row.RowIndex.ToString(); 
        if (risultato > 0) 
        { 
         myTextBoxKg.Text = qtaKgInseriti.Tables[0].Rows[0]["KG_LORDI"].ToString(); 
         txtSumKg.Text = (Double.Parse(txtSumKg.Text) + Double.Parse(myTextBoxKg.Text)).ToString(); 
        } 


        myCompValNr = new CompareValidator(); 
        myCompValNr.ID = "myCompValNr"; 
        myCompValNr.ErrorMessage = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Please enter a number grater than zero!</div>"; 
        myCompValNr.ControlToValidate = "myTextBoxQta"; 
        myCompValNr.Type = ValidationDataType.Integer; 
        myCompValNr.Operator = ValidationCompareOperator.GreaterThan; 
        myCompValNr.ValueToCompare = "0"; 
        myCompValNr.Display = ValidatorDisplay.Dynamic; 
        e.Row.Cells[i + 1].Controls.Add(myCompValNr); 

        myReqFvNr = new RequiredFieldValidator(); 
        myReqFvNr.ControlToValidate = myTextBoxQta.ID; 
        myReqFvNr.Text = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Field Qta cannot be null!</div>"; 
        e.Row.Cells[i + 1].Controls.Add(myReqFvNr); 


        myCompValKg = new CompareValidator(); 
        myCompValKg.ID = "myCompValKg"; 
        myCompValKg.ErrorMessage = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Please enter a number grater than zero!</div>"; 
        myCompValKg.ControlToValidate = "myTextBoxKg"; 
        myCompValKg.Type = ValidationDataType.Double; 
        myCompValKg.Operator = ValidationCompareOperator.GreaterThan; 
        myCompValKg.ValueToCompare = "0"; 
        myCompValKg.Display = ValidatorDisplay.Dynamic; 
        e.Row.Cells[i + 2].Controls.Add(myCompValKg); 

        myReqFvKg = new RequiredFieldValidator(); 
        myReqFvKg.ControlToValidate = myTextBoxKg.ID; 
        myReqFvKg.Text = "<div style=\"font-size: x-small; text-align: center; color: #FF0000\">Field Kg cannot be null!</div>"; 
        e.Row.Cells[i + 2].Controls.Add(myReqFvKg); 

       } 





     } 

感謝Chris和其他球員, 我發現瞭解決方案:

$(document).ready(function() { 
      $("#btnSaveProvvisorio").click(function() { 
       var i; 
       var jtbNumBollaQuin; 
       $('.MyTextBoxClass').each(function(i,v){ 
        jtbNumBollaQuin = $(this).val(); 
        alert("Valore quantita : " + jtbNumBollaQuin); 
       }); 
      }); 
     }); 

並設置myTextBoxQta.CssClass = 「MyTextBoxClass」;在aspx.cs文件中 和ClientIDMode =「Static」到btnSaveProvvisorio按鈕int aspx文件中。

非常感謝! 伊戈爾

+0

請添加一個名爲myTextBoxQta不能在你的頁面上,ASPX代碼 –

+0

的ASP.NET控件...它可能沒有中用runat =「服務器「 在上面。 –

+0

Chris,我把標籤:myTextBoxQta = new TextBox(); myTextBoxQta.ID =「myTextBoxQta」; myTextBoxQta.Attributes。添加(「nomeTextBox」,「myTextBoxQta」); myTextBoxQta.Attributes.Add(「runat」,「server」); myTextBoxQta.Attributes.Add(「onChange」,「javascript:TotaliOrdDett();」); myTextBoxQta.Style [「text-align」] =「center」; – Yellow75

回答

0

您可以使用data attribute

<textarea data-clientId="#<%=myTextBoxQta.ClientID%>" ></textarea> 

腳本

$(document).ready(function){ 
    $("#btnSaveProvvisorio").delegate("textbox", "change", function(){ 
     var jtbNumBollaQuin; 
     jtbNumBollaQuin=$($(this).data('clientId')).text(); 
     alert("Valore quantita : " + jtbNumBollaQuin); 
    }); 
});// check end of document.ready() 

還要檢查id<%=myTextBoxQta.ClientID%>存在於你的頁面。

0
  1. 命名myTextBoxQta您的網頁上不存在的ASP.NET控制......這可能不會對控件的標籤runat="server"

  2. 使用.on而不是.delegate

    • 更新*

看你的代碼後,你應該通過CSS來實現這個目的來測試你的代碼工作。

myTextBoxQta.CssClass = "MyTextBoxClass"; 

則...

$(document).ready(function){ 
    $("#btnSaveProvvisorio").delegate("textbox", "change", function(){ 
     var jtbNumBollaQuin; 
     jtbNumBollaQuin=$('.MyTextBoxClass').text(); 
     alert("Valore quantita : " + jtbNumBollaQuin); 
    }) 
} 
+0

克里斯,我很抱歉...與.on相同的錯誤。 – Yellow75