我的代码隐藏(c#)文件使用while()循环为我数据库中任何给定数量的问题动态声明RadioButtonLists:
我在for循环中为每个tmpRBL添加项目。
我将每个RadioButtonList注册到我在while()循环的每次迭代开始时创建的子面板。
然后我将每个面板添加到父面板。
while(reader.Read()
{
...
RadioButtonList tmpRBL = new RadioButtonList();
temp = "RadioButtonList" + count.ToString();
tmpRBL.ID = temp;
tmpRBL.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Vertical;
tmpRBL.TextAlign = System.Web.UI.WebControls.TextAlign.Left;
...
for (int i = 1; i <= numAnswers; i++)
{
tmpItem = new ListItem("", i.ToString());
tmpRBL.Items.Add(tmpItem);
}
...
p.Controls.Add(tmpRBL);
...
questionPanel.Controls.Add(p);
}
如何检索这些动态创建的RadioButtonLists的selectedIndex?我花了大部分时间在线尝试其他类似问题的各种修复,但没有运气。
如果我在Chrome中使用'Inspect Element',我可以在他们想要的位置看到RadioButtonLists,表面上是我分配的ID(RadioButtonList1,RadioButtonList2等),但我尝试的所有内容都以null对象结束。
我对C#比较陌生,这是我第一次处理动态控件,所以提前感谢所提供的任何帮助。
答案 0 :(得分:3)
基本上,如果您动态创建控件,则需要在页面的每个帖子中重新加载这些控件( 具有相同的ID )。
否则,它们将变为null,您将无法访问它们。
这是一个示例。它会动态加载RadioButtonList
控件,并在单击按钮时显示所选的值。
<asp:PlaceHolder runat="server" ID="PlaceHolder1"/>
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Submit" />
<asp:Label runat="server" ID="Label1"/>
protected void Page_Load(object sender, EventArgs e)
{
LoadControls();
}
protected void Button1_Click(object sender, EventArgs e)
{
var radioButtonList = PlaceHolder1.FindControl("1") as RadioButtonList;
Label1.Text = radioButtonList.SelectedValue;
}
private void LoadControls()
{
var tmpRBL = new RadioButtonList();
tmpRBL.ID = "1";
for (int i = 1; i <= 5; i++)
{
var tmpItem = new ListItem(i.ToString(), i.ToString());
tmpRBL.Items.Add(tmpItem);
}
PlaceHolder1.Controls.Add(tmpRBL);
}
答案 1 :(得分:0)
非常感谢。以同样的方式,我,finnaly设法控制其他控件:-)。这是vb.net中的代码隐藏。
我现在非常幸运: - )
创造&amp;从动态创建的RadioButtonList&amp;中检索值。文本框
Private Property RadioButtonList As RadioButtonList
Private Property TextBoxList As TextBox
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoadControls()
End Sub
Protected Sub Button111_Click(sender As Object, e As EventArgs)
Dim majstor_rbl(3) As String
Dim majstor_txt(3) As String
For i As Integer = 1 To 3
RadioButtonList = TryCast(PlaceHolder1.FindControl(i & "rbl"), RadioButtonList)
majstor_rbl(i) = RadioButtonList.SelectedValue
TextBoxList = TryCast(PlaceHolder1.FindControl(i & "txt"), TextBox)
majstor_txt(i) = TextBoxList.Text
Next
txt_podatak4.Text = majstor_rbl(1) & " - " & majstor_txt(1)
txt_podatak5.Text = majstor_rbl(2) & " - " & majstor_txt(2)
txt_podatak6.Text = majstor_rbl(3) & " - " & majstor_txt(3)
End Sub
Private Sub LoadControls()
For j As Integer = 1 To 3
Dim tmpRBL As Object = New RadioButtonList
Dim tmpTXT As Object = New TextBox
tmpRBL.ID = j & "rbl"
tmpTXT.ID = j & "txt"
For i As Integer = 1 To 3
Dim tmpItem As Object = New ListItem(" ", i.ToString())
tmpRBL.Items.Add(tmpItem)
Next
tmpRBL.RepeatLayout = RepeatLayout.Flow
tmpRBL.RepeatDirection = RepeatDirection.Horizontal
PlaceHolder1.Controls.Add(tmpTXT)
PlaceHolder1.Controls.Add(tmpRBL)
PlaceHolder1.Controls.Add(New LiteralControl("</br>"))
Next
End Sub
.aspx
<asp:PlaceHolder runat="server" ID="PlaceHolder1"/>
<asp:Button runat="server" ID="Button3" OnClick="Button111_Click" Text="Submit" />
<asp:TextBox ID="txt_podatak4" runat="server" CssClass="form-control"></asp:TextBox>
<asp:TextBox ID="txt_podatak5" runat="server" CssClass="form-control"></asp:TextBox>
<asp:TextBox ID="txt_podatak6" runat="server" CssClass="form-control"></asp:TextBox>