RadioButtonList SelectedItem始终返回EmptyString

时间:2009-03-24 20:02:41

标签: c# asp.net radiobuttonlist

我在我的.aspx页面中使用了这个自定义radiobuttonlist,以便能够让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创建的所有radiobuttons具有相同的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中选择了一个值时,我一直得到一个空洞。同样适用于其他radiobuttonlist rblReason。在任何一种情况下,当从代码隐藏检查时,我得到了SelectedValue的emptystring。

如果你看一下标记,它的外观如下:

<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> 

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

看来你正在填充页面加载的RadioButtonList -  如果是这样 - 请确保用一个包围RadioButtonList的人口  如果/ Then / Postback阻止:  如果不是Page.IsPostBack那么  '填充你的RadioButtonList  结束如果

例如:

        if (!IsPostBack)
        {
            loadradiobuttonlist();
        }
相关问题