动态创建的单选按钮未显示选中的ASP.NET

Dynamically Created Radio Button Not Showing Checked ASP.NET

本文关键字:ASP NET 创建 单选按钮 动态 显示      更新时间:2023-09-26

我正在编写一个WebApplication,它有一个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}即使Houston是选定的RadioButton。)

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

如果其他人也有同样的问题,我会找出我的错误所在。在Page_Load中,我忘记在if"is not 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();
        }
    }

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

我的猜测是点击事件中对databind的调用正在重置值。如果我需要显式调用databind,我会在控制更改事件后执行,例如在LoadComplete中http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx我以前经常遇到这个问题,但现在我很明智了。