0

我在我的 .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> 
4

2 回答 2

0

似乎您正在页面加载时填充 RadioButtonList - 如果是这样 - 请确保您使用 If/Then/Postback 块包围 RadioButtonList 的填充:如果不是 Page.IsPostBack then ' 填充您的 RadioButtonList end if

例如:

        if (!IsPostBack)
        {
            loadradiobuttonlist();
        }
于 2014-01-03T18:28:35.937 回答
0

不知道为什么这不起作用,但似乎一点 javascript 会提供一个简单的解决方法。而不是自定义控件,只需使用常规 RadioButton 对象...然后附加一些 javascript 以在选择 List2 上的某些内容时清除 List1 上的选择,反之亦然。

如果您已经知道会有多少单选按钮,不知道为什么要在这里使用任何类型的中继器?(根据您上面的评论)

于 2009-03-25T07:47:17.520 回答