动态创建单选按钮不显示已检查的ASP.NET

时间:2013-08-02 14:20:00

标签: javascript asp.net c#-4.0 radio-button controls

我正在编写一个WebApplication,它有一个RadioButton控件列表,根据数据库中的信息动态生成。我的问题是,我“检查”的RadioButton没有显示为被检查。

HTML:

<asp:CustomValidator ID="vgCheckRequiredChoice" Display="Static" runat="server" ValidationGroup="vgTicketTypeChoice"
    Text="You must select an office location to continue." ForeColor="Red"></asp:CustomValidator>
<table style="padding: 10px 10px 10px 10px; width: 100%" cellpadding="5px 5px 5px 5px">
    <asp:Repeater ID="rptType" runat="server" OnItemDataBound="rptType_ItemDataBound">
        <ItemTemplate>
            <tr style="border-bottom: solid 1px #000000; padding: 40px 10px 10px 10px;">
                <td style="width: 30%;">
                    <asp:RadioButton runat="server" ID="RadioButtonLocation" Text='<%# Bind("LocationName") %>' 
                    GroupName='<%# Bind("LocationID") %>' ValidationGroup="vgTicketTypeChoice"
                    CausesValidation="true" />
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>
<div style="float: right; display: inline; padding-right: 20px; padding-top: 20px;">
    <asp:Button runat="server" ID="ButtonCancel" Text="Cancel" PostBackUrl="~/Default.aspx" />
    <asp:Button runat="server" ID="ButtonDefiningLocation" Text="Next >>"OnClick="ButtonDefiningLocation_Click" />
</div>

以下是我用来检查每个独特RadioButton的JavaScript:

 <script type="text/javascript">
    function SetUniqueRadioButton(nameregex, current) {
        re = new RegExp(nameregex);
        for (i = 0; i < document.forms[0].elements.length; i++) {
            elm = document.forms[0].elements[i]
            if (elm.type === 'radio') {
                if (re.test(elm.name)) {
                    elm.checked = false;
                }
            }
        }
        current.checked = true;
    }
</script>

最后但并非最不重要的CodeBehind:

protected void rptType_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            return;
        RadioButton rdo = (RadioButton)e.Item.FindControl("RadioButtonLocation");
        string script =
           "SetUniqueRadioButton('rptType.*VisitingOffice',this)";
        rdo.Attributes.Add("onclick", script);
    }

    protected void ButtonDefiningLocation_Click(object sender, EventArgs e)
    {
        rptType.DataBind();
        if (Page.IsValid)
        {
            foreach (RepeaterItem ri in rptType.Items)
            {
                switch (ri.ItemType)
                {
                    case ListItemType.Item:
                    case ListItemType.AlternatingItem:
                        RadioButton rb = (RadioButton)ri.FindControl("RadioButtonLocation");
                        if (rb.Checked)
                        {
                            var LocationID = rb.GroupName;
                            DataService ds = new DataService();
                            DataTable tbl = ds.GetLocation(LocationID);
                            Response.Write(LocationID);

                        }
                        break;
                }
            }
        }
    }

正如你所看到的,在代码隐藏中,当我逐步执行程序并进入“rb.checked”时,它将显示rb的正确选项(例如:{Text =“Houston”Checked = false}即使休斯顿是选定的RadioButton。)

编辑:我已将其跟踪到javascript函数。 javascript函数没有将current.checked设置为true。现在,我不知道为什么在函数中显式调用它时会发生这种情况。任何建议或建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果其他人有同样的问题,我想出了我的错误所在。在Page_Load中,我忘了在if“不是PostBack”部分中为转发器设置DataSource和DataBind。 PageLoad应该是这样的。

protected void Page_Load(object sender, EventArgs e)
    {
        var FormattingPlaceHolder = Master.FindControl("BottomLinkButtonDiv");
        FormattingPlaceHolder.Visible = false;
        if(!IsPostBack)
        {
        var ds = new DataService();
        rptType.DataSource = ds.GetLocationIDs();
        rptType.DataBind();
        }
    }

感谢大家的建议。快乐的编码。

答案 1 :(得分:0)

我的猜测是在click事件中对数据绑定的调用是重置值。如果我需要显式调用databind,我会在控制更改事件后执行此操作,例如在LoadComplete http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx我曾经遇到过很多这个问题,但我现在很明智(触摸木头)。