好的,所以我把这个面板放在桌子的td里。
在此面板中,我正在创建一个带有文本框的动态表,并在按钮单击时按下控件。
问题是,当试图接收控件时,我失败了。我已经检查过超过25个网站,找不到合适的解决方案或与我类似的东西。
这是我正在尝试访问控件的代码。它位于按钮事件处理程序中。
for (int i = 0; i < ClickCount; i++)
{
//Control myControls = Page.FindControl("table1");
//Panel myPanel = (Panel)mainTable.FindControl("Panel1");
//Table myTable = (Table)myPanel.FindControl("table1");
string FirstName = null;
string LastName = null;
string Age = null;
string PCountry = null;
string PCommittee = null;
TextBox txtF = (TextBox)Panel1.FindControl("TextBoxF" + i.ToString());
FirstName = txtF.Text;
TextBox txtL = (TextBox)Panel1.FindControl("TextBoxL" + i.ToString());
LastName = txtL.Text;
TextBox txtA = (TextBox)Panel1.FindControl("TextBoxA" + i.ToString());
Age = txtA.Text;
DropDownList ddlPCountry = (DropDownList)myTable.FindControl("ddlPCountry" + i.ToString());
PCountry = ddlPCountry.SelectedValue;
DropDownList ddlPCommittee = (DropDownList)myTable.FindControl("ddlCommittee" + i.ToString());
PCommittee = ddlPCommittee.SelectedValue;
string groupInsert = "insert into GroupUser (GroupUserID, FirstName, LastName, Age, PreferredCountry, PreferredCommittee) ";
groupInsert += "values (@GroupUserID, @FirstName, @LastName, @Age, @PreferredCountry, @PreferredCommittee);";
SqlCommand group = conn.CreateCommand();
group.CommandType = CommandType.Text;
group.CommandText = groupInsert;
group.Parameters.Add(new SqlParameter("@GroupUserID", ID));
group.Parameters.Add(new SqlParameter("@FirstName", FirstName));
group.Parameters.Add(new SqlParameter("@LastName", LastName));
group.Parameters.Add(new SqlParameter("@Age", Age));
//group.Parameters.Add(new SqlParameter("@PreferredCountry", ddlPCountry.SelectedValue));
//group.Parameters.Add(new SqlParameter("@PreferredCommittee", ddlPCommittee.SelectedValue));
int temp = group.ExecuteNonQuery();
}
Response.Redirect("StartPage.aspx");
这是在另一个按钮事件处理程序中创建控件。
Table table = new Table();
table.ID = "table1";
for (int i = 0; i < ClickCount; i++)
{
TableRow row1 = new TableRow();
TableRow row2 = new TableRow();
TableRow row3 = new TableRow();
TableRow row4 = new TableRow();
TableRow row5 = new TableRow();
TableRow row6 = new TableRow();
TableRow row7 = new TableRow();
TextBox TxtBoxF = new TextBox();
TextBox TxtBoxL = new TextBox();
TextBox TxtBoxA = new TextBox();
DropDownList ddlPCountry = new DropDownList();
DropDownList ddlPCommittee = new DropDownList();
Label lblF = new Label();
Label lblL = new Label();
Label lblA = new Label();
Label lblPCountry = new Label();
Label lblPCommittee = new Label();
TxtBoxF.ID = "TextBoxF" + i.ToString();
TxtBoxL.ID = "TextBoxL" + i.ToString();
TxtBoxA.ID = "TextBoxA" + i.ToString();
ddlPCountry.ID = "ddlPCountry" + i.ToString();
ddlPCommittee.ID = "ddlPCommittee" + i.ToString();
lblF.ID = "LabelF" + i.ToString();
lblL.ID = "LabelU" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblPCountry.ID = "LabelPCountry" + i.ToString();
lblPCommittee.ID = "LabelPCommittee" + i.ToString();
lblF.Text = "First Name: ";
lblL.Text = "Last Name :";
lblA.Text = "Age: ";
lblPCountry.Text = "Preferred Country: ";
lblPCommittee.Text = "Preferred Committee: ";
ddlPCountry.DataSource = countryDt;
ddlPCountry.DataValueField = "CountryID";
ddlPCountry.DataTextField = "CountryName";
ddlPCountry.DataBind();
ddlPCommittee.DataSource = committeeDt;
ddlPCommittee.DataValueField = "CommitteeID";
ddlPCommittee.DataTextField = "CommitteeName";
ddlPCommittee.DataBind();
TxtBoxF.Attributes.Add("runat", "server");
TxtBoxL.Attributes.Add("runat", "server");
TxtBoxA.Attributes.Add("runat", "server");
ddlPCountry.Attributes.Add("runat", "server");
ddlPCommittee.Attributes.Add("runat", "server");
for (int a = 0; a < 2; a++)
{
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Controls.Add(lblF);
cell2.Controls.Add(TxtBoxF);
row1.Controls.Add(cell1);
row1.Controls.Add(cell2);
}
for (int b = 0; b < 2; b++)
{
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Controls.Add(lblL);
cell2.Controls.Add(TxtBoxL);
row2.Controls.Add(cell1);
row2.Controls.Add(cell2);
}
for (int c = 0; c < 2; c++)
{
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Controls.Add(lblA);
cell2.Controls.Add(TxtBoxA);
row3.Controls.Add(cell1);
row3.Controls.Add(cell2);
}
for (int d = 0; d < 2; d++)
{
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Controls.Add(lblPCountry);
cell2.Controls.Add(ddlPCountry);
row4.Controls.Add(cell1);
row4.Controls.Add(cell2);
}
for (int f = 0; f < 2; f++)
{
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
cell1.Controls.Add(lblPCommittee);
cell2.Controls.Add(ddlPCommittee);
row5.Controls.Add(cell1);
row5.Controls.Add(cell2);
}
table.Rows.Add(row1);
table.Rows.Add(row2);
table.Rows.Add(row3);
table.Rows.Add(row4);
table.Rows.Add(row5);
}
Panel1.Controls.Add(table);
Panel1.Controls.Add(new LiteralControl("<br />"));
Panel1.Controls.Add(new LiteralControl("<br />"));
conn.Close();
这是aspx:
<body>
<form id="form1" runat="server">
<div>
<h1>Group Registration</h1>
<br />
<table style="width:100%;" id="mainTable" runat="server">
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>First Name:</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>Age:</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style1">Contact Number:</td>
<td class="auto-style1">
<asp:TextBox ID="txtContactNumber" runat="server"></asp:TextBox>
</td>
<td class="auto-style1"></td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>Institute</td>
<td>
<asp:TextBox ID="txtInstitute" runat="server" Width="400px"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style1">Preferred Country:</td>
<td class="auto-style1">
<asp:DropDownList ID="ddlCountry" runat="server">
</asp:DropDownList>
</td>
<td class="auto-style1"></td>
</tr>
<tr>
<td>Preferred Committee:</td>
<td>
<asp:DropDownList ID="ddlCommittee" runat="server">
</asp:DropDownList>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Username:</td>
<td><asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td>Password:</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnAddAnother" runat="server" OnClick="btnAddAnother_Click" Text="Add Another Member" />
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btnRegister" runat="server" Text="Sign Up" OnClick="btnRegister_Click" />
</td>
<td> </td>
</tr>
</table>
</div>
</form>
答案 0 :(得分:1)
问题1:
如果它具有属性 runat =&#34; server&#34; ,则无法访问服务器端的任何控件(来自C#代码隐藏)。所以,你必须添加:
.Attributes.Add(&#34; runat&#34;,&#34; server&#34;);
到按钮事件处理程序中要访问的每个控件。在每个控制声明之后,您必须执行以下操作:
TxtBoxF.Attributes.Add("runat", "server");
TxtBoxL.Attributes.Add("runat", "server");
TxtBoxA.Attributes.Add("runat", "server");
ddlPCountry.Attributes.Add("runat", "server");
ddlPCommittee.Attributes.Add("runat", "server");
lblF.Attributes.Add("runat", "server");
lblL.Attributes.Add("runat", "server");
lblA.Attributes.Add("runat", "server");
lblPCountry.Attributes.Add("runat", "server");
lblPCommittee.Attributes.Add("runat", "server");
问题2:
您应该在 Panel1 中搜索控件,而不是表格。但是,由于我看不到您的ASPX代码,我不确定您的面板是否具有属性 runat = server 。
我建议您对ASPX页面进行以下更改:
更改后贴标签:
<form id="form1" runat="server">
<div>
到
<div id="myControls" runat="server">
然后,在动态控件创建结束时,更改:
Panel1.Controls.Add(table);
Panel1.Controls.Add(new LiteralControl("<br />"));
Panel1.Controls.Add(new LiteralControl("<br />"));
到
myControls.Controls.Add(table);
myControls.Controls.Add(new LiteralControl("<br />"));
myControls.Controls.Add(new LiteralControl("<br />"));
然后,在您的控制访问代码中,您需要更改:
TextBox txtF = (TextBox)Panel1.FindControl("TextBoxF" + i.ToString());
TextBox txtL = (TextBox)Panel1.FindControl("TextBoxL" + i.ToString());
TextBox txtA = (TextBox)Panel1.FindControl("TextBoxA" + i.ToString());
DropDownList ddlPCountry = (DropDownList)myTable.FindControl("ddlPCountry" + i.ToString());
DropDownList ddlPCommittee = (DropDownList)myTable.FindControl("ddlCommittee" + i.ToString());
到
TextBox txtF = (TextBox)myControls.FindControl("TextBoxF" + i.ToString());
TextBox txtL = (TextBox)myControls.FindControl("TextBoxL" + i.ToString());
TextBox txtA = (TextBox)myControls.FindControl("TextBoxA" + i.ToString());
DropDownList ddlPCountry = (DropDownList)myControls.FindControl("ddlPCountry" + i.ToString());
DropDownList ddlPCommittee = (DropDownList)myControls.FindControl("ddlCommittee" + i.ToString());
可能的问题3:
应在Page Init期间创建新控件。因此,您应该将所有动态控件创建代码放入page_init:
protected void Page_Init(object sender, EventArgs e)
{
// Put dynamic controls creation here!
}