ASP.net循环通过表 [英] ASP.net looping through table
本文介绍了ASP.net循环通过表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如果任何人能帮助我,我有一个表,看起来像下面这样
<表ID =表1BORDER =0>
&所述; TR>
< TD>< B> 1< / B>问题1< / TD>
&所述; / TR>&下; TR>
< TD风格=BORDER-宽度:5像素,边框样式:实;>< / TD>
&所述; / TR>&下; TR>
< TD align =left的风格=宽度:1000像素;><输入ID =Radio 1的类型=电台NAME =组1值=收音机1/><标签=收音机1>正确答案为< /标签>< / TD>
&所述; / TR>&下; TR>
< TD align =left的风格=宽度:1000像素;><输入ID =Radio 1的类型=电台NAME =组1值=收音机1/><标签=收音机1>&ANSWER2 LT; /标签>< / TD>
&所述; / TR>&下; TR>
< TD align =left的风格=宽度:1000像素;><输入ID =Radio 1的类型=电台NAME =组1值=收音机1/><标签=收音机1>&ANSWER3 LT; /标签>< / TD>
&所述; / TR>&下; TR>
< TD align =left的风格=宽度:1000像素;><输入ID =Radio 1的类型=电台NAME =组1值=收音机1/><标签=收音机1> Answer4< /标签>< / TD>
&所述; / TR>&下; TR>
< TD风格=高度:30PX;>< / TD>
&所述; / TR>&下; TR>
< TD>< B> 2< / B>问题2'; / TD>
&所述; / TR>&下; TR>
< TD风格=BORDER-宽度:5像素,边框样式:实;>< / TD>
&所述; / TR>&下; TR>
< TD align =left的风格=宽度:1000像素;><输入ID =第二广播电台类型=电台NAME =组2值=第二广播电台/><标签=第二广播电台>是< /标签>< / TD>
&所述; / TR>&下; TR>
< TD align =left的风格=宽度:1000像素;><输入ID =第二广播电台类型=电台NAME =组2值=第二广播电台/><标签=第二广播电台>无< /标签>< / TD>
&所述; / TR>&下; TR>
< TD风格=高度:30PX;>< / TD>
< / TR>
< /表>
我如何去通过每一个单选按钮组循环和获得选定的单选按钮?
文非常感谢!
在code上面显示的是动态创建的......在我的aspx文件我有以下code
< ASP:表ID =表1=服务器>
< / ASP:表>
解决方案
这听起来像你开始用准系统<表>在您的标记页
,和动态添加的<输入方式>
之后
考虑采取这种方式:
- 在
=服务器
属性添加到您的表。 - 在code,你要添加的
<输入>
标签,添加一个新的单选
控制。这里使用一个ID可以稍后predict。也许你可以使用单选按钮列表
相反,如果选择在逻辑上划分! - 现在还不清楚,如果你手动添加这些
< TR>
和< TD>
为字符串。考虑新的TableRow的选择()
和新的TableCell()
。然后添加新的单选
到TableCell.Controls
收集与tc.Controls.Add( myNewRadioButton);
- 在回传code,只要通过
引用您的
,甚至可以通过循环在单选
控件ID控件
的表1
。 的集合属性
的foreach(在Table1.Controls控制X)
{
如果(x.GetType()。toString()方法。等于(System.Web.UI.WebControls.RadioButton))
{
如果(((单选)X).Checked)
{
//继续。
}
}
}
i was wondering if any one could help me out, i have a table which looks something like the following
<table id="Table1" border="0">
<tr>
<td><b>1.</b> Question 1</td>
</tr><tr>
<td style="border-width:5px;border-style:solid;"></td>
</tr><tr>
<td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer1</label></td>
</tr><tr>
<td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer2</label></td>
</tr><tr>
<td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer3</label></td>
</tr><tr>
<td align="left" style="width:1000px;"><input id="Radio1" type="radio" name="Group1" value="Radio1" /><label for="Radio1">Answer4</label></td>
</tr><tr>
<td style="height:30px;"></td>
</tr><tr>
<td><b>2.</b> Question 2</td>
</tr><tr>
<td style="border-width:5px;border-style:solid;"></td>
</tr><tr>
<td align="left" style="width:1000px;"><input id="Radio2" type="radio" name="Group2" value="Radio2" /><label for="Radio2">yes</label></td>
</tr><tr>
<td align="left" style="width:1000px;"><input id="Radio2" type="radio" name="Group2" value="Radio2" /><label for="Radio2">no</label></td>
</tr><tr>
<td style="height:30px;"></td>
</tr>
</table>
how do i go about looping through each group of radio buttons and getting the text of the selected radio button ??
thanks a lot !!
the code displayed above is created dynamically ... in my aspx file i have the following code
<asp:Table ID="Table1" runat="server">
</asp:Table>
解决方案
It sounds like you're starting with a barebones <table>
in your markup page, and dynamically adding those <input>
afterwards.
Consider taking this approach:
- Add the
runat="server"
attribute to your table. - In the code where you're adding those
<input>
tags, add a newRadioButton
control. Use an ID here that you can predict later. Perhaps you can use aRadioButtonList
instead, if the choices are logically grouped! - It's unclear if you're manually adding those
<tr>
and<td>
as strings. Consider the option ofnew TableRow()
andnew TableCell()
. Then add the newRadioButton
to theTableCell.Controls
collection withtc.Controls.Add(myNewRadioButton);
- In your postback code, simply refer to your
RadioButton
controls byid
, or even loop through theControls
collection property of theTable1
.
foreach (Control x in Table1.Controls)
{
if (x.GetType().ToString().Equals("System.Web.UI.WebControls.RadioButton"))
{
if (((RadioButton)x).Checked)
{
//proceed.
}
}
}
这篇关于ASP.net循环通过表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文