2016-02-19 80 views
0

我有10行的文本框(4列)如何檢查動態創建的文本框是否填充了數據?

如果所有10行已填入數據,我需要觸發一個事件做某些事情。

我新的網絡開發和我真的不知道如何這是動態的隱藏代碼生成的HTML頁面上引用的項目。這是它看起來像HTML:

<tr align="center"> 
<td style="height: 85px"> 
    </td> 
    <td align="center" style="width: 440px; height: 310px"> 
     <fieldset class="field1" style="text-align: center"> 
      <legend class="Eblegend" id="legendObligation2"> 
       <%=frameName%> 
      </legend> 
      <asp:Panel ID="pnlTable" runat="server" Width="620px" Height="310px"> 
      </asp:Panel> 
      </fieldset> 
    </td> 
</tr> 

此代碼變成可編輯的表格(4列10行)。

我需要檢查是否所有的盒子都被填滿,以便我可以啓用「下一步」按鈕。

的問題是,我不知道如何引用在動態生成的表中的這些「細胞」,這樣我可以說:「如果不爲null或爲空,這樣做」

誰能給我一個手?

+0

可如果youi're使用ASP,你做這樣的事情:文本框 '的foreach(VAR tbCtrl在this.Controls.OfType ()){ } ' – MethodMan

回答

0

爲了能夠訪問這些值,您還需要在PostBack的情況下創建TextBoxes。這必須發生得很早,控制的ID必須相同。

下面的代碼示出了動態地創建一個文本框和讀取值後的一個小樣品:

ASPX:

<form id="form1" runat="server"> 
    <div> 
     <asp:Button ID="btnCreateTextBox" runat="server" Text="Create TextBox" OnClick="btnCreateTextBox_Click" /> 
    </div> 
    <div> 
     <asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder> 
    </div> 
    <div> 
     <asp:Button ID="btnPostBack" runat="server" Text="Do a postback" /> 
    </div> 
    <div> 
     <asp:Label ID="lbl" runat="server" /> 
    </div> 
</form> 

代碼隱藏:

public partial class WebForm1 : System.Web.UI.Page 
{ 
    protected TextBox txt; 

    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     if (Request.Form.AllKeys.Any(x => x == "TextBox1")) 
      CreateTextBox(); 
    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (txt != null) 
      lbl.Text = "TextBox value is " + txt.Text; 
     else 
      lbl.Text = "No value in TextBox"; 
    } 

    protected void btnCreateTextBox_Click(object sender, EventArgs e) 
    { 
     if (txt == null) 
     { 
      CreateTextBox(); 
     } 
    } 

    private void CreateTextBox() 
    { 
     txt = new TextBox(); 
     txt.ID = "TextBox1"; 
     placeHolder.Controls.Add(txt); 
    } 
} 

重要的部分是TextBox創建於OnInit與如果PostBack數據中有值,則使用相同的ID。即使TextBox爲空,文本框的鍵(樣本情況下爲TextBox1)也存在於AllKeys集合中。