【发布时间】:2025-12-21 10:05:11
【问题描述】:
我在我的 .aspx 页面中使用了这个自定义单选按钮列表,以便能够让 GroupName 实际工作,因为我将在同一个 .aspx 页面上有 2 个 RadiobuttonList 控件:
public class CustRadioButtonList : RadioButtonList, IRepeatInfoUser
{
void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
RadioButton radioButton = new RadioButton();
radioButton.Page = this.Page;
radioButton.GroupName = "radioButtonGroup";
radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
radioButton.Text = this.Items[repeatIndex].Text;
radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
radioButton.Checked = this.Items[repeatIndex].Selected;
radioButton.TextAlign = this.TextAlign;
radioButton.AutoPostBack = this.AutoPostBack;
radioButton.TabIndex = this.TabIndex;
radioButton.Enabled = this.Enabled;
radioButton.RenderControl(writer);
}
}
所以这只是一个简单的扩展,我设置 GroupName 以确保该 RadioButtonList 创建的所有单选按钮具有相同的 GroupName,因此现在,如果有人从 RadiobuttonList1 中选择一个值,它会取消选择他们在 Radiobutton2 中选择的任何值反之亦然(因此它们是相互排斥的单选按钮集)。
注意:单选按钮列表肯定是通过包含在检查 !Page.IsPostBack 中的方法调用绑定的,因此这不是这里的问题。
这是我在 .aspx 页面中使用它的示例:
<aj:CustRadioButtonList checked="false" ID="rblEmail" runat="server" />
<aj:CustRadioButtonList checked="false" ID="rblReason" runat="server" />
在我的代码隐藏中,我正在检查页面上按钮的 onclick 中的 rblEmail 中的 selectedValue..但它总是返回空字符串,即使我选择了列表中的一项:
protected void btnContinue_Click(object sender, EventArgs e)
{
_actionID = rblEmail.SelectedValue;
我花了一整天的时间试图弄清楚为什么我在 rblEmail 中明确选择了一个值时仍然收到一个空字符串。其他单选按钮列表 rblReason 也是如此。在任何一种情况下,当从代码隐藏中检查时,我都会得到 SelectedValue 的空字符串。
如果您查看标记,它的外观如下:
<table id="rblEmail" checked="false" border="0">
<tr>
<td><input id="rblEmail_0" type="radio" name="radioButtonGroup" value="0" /><label for="rblEmai_0">All Offers</label></td>
</tr><tr>
<td><input id="rblEmail_1" type="radio" name="radioButtonGroup" value="1" /><label for="rblEmail_1">week</label></td>
</tr><tr>
<td><input id="rblEmail_2" type="radio" name="radioButtonGroup" value="2" /><label for="rblEmail_2">month</label></td>
</tr><tr>
<td><input id="rblEmail_3" type="radio" name="radioButtonGroup" value="3" /><label for="rblEmail_3">Holiday</label></td>
</tr>
</table>
</div>
...
<table id="rblReason" checked="false" border="0">
<tr>
<td><input id="rblReason_0" type="radio" name="radioButtonGroup" value="1" /><label for="rblReason_0">I receive</label></td>
</tr><tr>
<td><input id="rblReason_1" type="radio" name="radioButtonGroup" value="2" /><label for="rblReason_1">I have no need</label></td>
</tr><tr>
<td><input id="rblReason_2" type="radio" name="radioButtonGroup" value="3" /><label for="rblReason_2">Other</label></td>
</tr>
</table>
【问题讨论】:
-
只是想知道,为什么使用单个普通 RBL 不起作用?
-
我将如何吐出它,我在两个控件中都有 5 个单选按钮。并且在转发器中放置一个 asp.net 单选按钮控件几乎与 Grouping 相关的问题和与 GroupName 相关的错误被控件的 UniqueID 覆盖我很好奇,你的意思是直线上升?
-
雷克斯,详细解释一下你的意思。你说的是中继器还是什么?我查了一下,发现了很多与分组问题几乎相同的问题,等等。为什么要把我的时间浪费在同样的问题上
-
为什么它也会在asp.net中引起痛苦的例子:justskins.com/forums/workaround-for-bug-q316495-90650.html
标签: c# asp.net radiobuttonlist