2012-02-29 41 views
0

我有一個asp.net web應用程序,其中我用來生成文本框列表取決於表中的計數,並在列末尾生成按鈕動態地更新文本框中的值。我附上了示例代碼以供參考,例如我已將其分配爲 c限制爲如何在動態生成的按鈕單擊事件中更新此文本框控件。在c#和asp.net中更新動態創建的文本框

示例代碼:

TableRow rowpoint = new TableRow(); 
        TableCell cellpoint = new TableCell(); 
        cellpoint.Text = "Total Points"; 
        rowpoint.Cells.Add(cellpoint); 

        for (int c = 0; c < 5; c++) 
        { 

         TableCell cellinput = new TableCell(); 
         TextBox txtinput = new TextBox(); 

         cellinput.Controls.Add(txtinput); 
         rowpoint.Cells.Add(cellinput); 
        } 


        TableCell totpoint = new TableCell(); 

        totpoint.BackColor = System.Drawing.Color.FromArgb(221, 221, 221); 

        Button btnupdate2 = new Button(); 
        btnupdate2.Text = "Update"; 
        totpoint.Controls.Add(btnupdate2); 
        rowpoint.Cells.Add(totpoint); 
        tbl1.Rows.Add(rowpoint); 
        //End Adding Total Points 

回答

0

給一個唯一的ID給每個文本框在創建文本框。添加一個按鈕單擊處理程序在創建按鈕

btnupdate2.Click += new System.EventHandler(this.btnupdate2_OnClick) 

然後寫如下的事件處理程序

protected void btnupdate2_OnClick(object sender, EventArgs e) { 


} 

這裏面使用的FindControl得到每個文本框。

+0

hi praveen 當我給文本框和構建應用程序的唯一ID時,它會自動將'ctl00 $ contentpanel'附加到我的文本框中id.so如何獲得文本框的確切ID – sharad 2012-02-29 07:57:50

+0

使用FindControl時,您只需要給出ID你給了。例如:如果你設置爲txtinput.ID =「id1」; 然後您可以使用((Textbox)FindControl(「id1」))來獲取文本框運行時。將其映射到有效的唯一標識符(帶前綴)將自動完成。 – PraveenVenu 2012-02-29 07:59:49

+0

我按照你所說的嘗試,但它給了我這樣的錯誤**對象引用沒有設置爲對象的實例**。我也嘗試使用這個** this.Form.Controls在我的窗體中計數控件。計數**它給了我數3,因爲我的表單包含很多controls.I認爲這可能是因爲我使用的是從主窗體繼承的網頁內容窗體,所以它計算這個主窗體上的控件。什麼說... – sharad 2012-03-02 07:20:24

相關問題