2012-10-01 51 views
0

我有一個頁面,其中包含一個從數據庫填充的表格。它看起來有點像這樣:使用C#和ASP處理類似jQuery的HTML DOM

System Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
1  x  x  x  x  x  x  x  x  x x  x  x 
2  x  x  x  x  x  x  x  x  x x  x  x 
6  x  x  x  x  x  x  x  x  x x  x  x 

在表格內的每個小區(即,每個「X」上面的圖上)中包含的用戶需要輸入一個值到文本框。我動態地給每個文本框使用列標題和系統編號,例如'feb2','mar6','oct1'。

頁面底部有一個保存按鈕,當按下該按鈕時,將更新數據庫中所有系統和所有月份的值。

我的問題是這樣的:我怎樣才能得到文本框的值,並調用一個sql函數來更新值,而不必先知道文本框的ID?

這裏是我想的jQuery做一個例子(我更熟悉的語言):

function saveValues(){ 
    var rows = $("#SystemTargetsTable tr:not(:first-child)"); // skip the header row 
    var systemID; 
    var jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec; 

    rows.each(function(index) {   
    systemID = $(this).attr('id').substring(6); 

    jan = $('#Jan' + systemID).val(); 
    feb = $('#Feb' + systemID).val(); 
    mar = $('#Mar' + systemID).val(); 
    apr = $('#Apr' + systemID).val(); 
    may = $('#May' + systemID).val(); 
    jun = $('#Jun' + systemID).val(); 
    jul = $('#Jul' + systemID).val(); 
    aug = $('#Aug' + systemID).val(); 
    sep = $('#Sep' + systemID).val(); 
    oct = $('#Oct' + systemID).val(); 
    nov = $('#Nov' + systemID).val(); 
    dec = $('#Dec' + systemID).val(); 

    //ONCE I HAVE THE VALUES FOR EACH MONTH I WOULD LIKE TO CALL AN UPDATE STATEMENT USING THE SYSTEM ID. ONLY OF COURSES I CANNOT DO TIS FROM JAVASCRIPT! 
    }); 
} 

謝謝你們的時間。我希望這一切都有道理!

回答

0

使該表服務器控制,其中runat =「server」屬性&給出ID,例如: TBL1, 然後循環雖然錶行

for (var i = 0; i<tbl1.Rows.Count; i++) 
      { 
       var txtJan = tbl1.Rows[i].FindControl("Jan" + i.ToString()); 

var janValue =txtJan.Text;

   } 
0
You can not update and sql table row without knowing the ID of unique column 
Which in your case is the system column. 


Here is what I do : 

By the time you are populating you table with the dynamic text box for each row 


you assign the id of the textbox = system id value of that row. 


<asp:TextBox ID="System.ID_Value(FromDB)" value="Month_Value_(value from DB)" runat="server"></asp:TextBox> 

then on the click event of the save button 

protected void Button1_Click(object sender, EventArgs e) 
    { 
     foreach (var row in Table1.Rows) 
     { 
      string[] allkeys = Request.Form.AllKeys; 
      foreach (string key in allkeys) 
      { 
       if (key.ToUpper().StartsWith("System_ID")) 
       { 
        var ID = Convert.ToInt32(key.Split('_')[1]);   
       } 
       if (key.ToUpper().StartsWith("MOnth_Value")) 
       { 
        var Jan = Convert.ToInt32(key.Split('_')[2]); 
        var feb = Convert.ToInt32(key.Split('_')[3]); 
       } 
       // and so on then you call the update method 
       /* public void uodate (id,jan,feb,mrch- --------) 
       * { 




       }*/ 
      } 
     } 
    }