简体   繁体   English

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

[英]Dynamically Created Radio Button Not Showing Checked ASP.NET

I'm writing a WebApplication that has a list of RadioButton controls, dynamically generated based on information from a database. 我正在编写一个Web应用程序,其中包含RadioButton控件的列表,这些控件是根据数据库中的信息动态生成的。 My problem is, the RadioButton that I "check" is not displaying as being checked. 我的问题是,我“检查”的RadioButton没有显示为被检查。

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

Here is the JavaScript that I'm using to check each unique RadioButton: 这是我用来检查每个唯一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>

Last but not least the 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;
                }
            }
        }
    }

As you can see, in the codebehind, when I stepthrough the program and get down to "rb.checked" it will display the correct options for rb (Example: {Text="Houston" Checked= false} even if Houston is the selected RadioButton.) 如您所见,在后面的代码中,当我逐步执行程序并转到“ rb.checked”时,它将显示rb的正确选项(例如:即使选择了Houston,{Text =“ Houston” Checked = false}单选按钮。)

EDIT: I have tracked it down to the javascript function. 编辑:我已经将其跟踪到javascript函数。 The javascript function is not setting the current.checked equal to true. javascript函数未将current.checked设置为true。 Now, I have no idea why this would be happening when it is explicitly being called in the function. 现在,我不知道为什么在函数中显式调用它时会发生这种情况。 Any suggestions or advice would be much appreciated. 任何建议或意见将不胜感激。

In case anyone else has the same problem, I figured out where my mistake was. 万一其他人有同样的问题,我就会弄清楚我的错误在哪里。 In the Page_Load I had forgotten to set the DataSource and the DataBind for the repeater in the if "is not PostBack" section. 在Page_Load中,我忘记了在if“不是PostBack”部分中为转发器设置DataSource和DataBind。 PageLoad should look like this. 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();
        }
    }

Thanks everyone for their suggestions. 感谢大家的建议。 Happy coding. 快乐的编码。

My guess is that the call to databind in the click event is resetting the values. 我的猜测是在click事件中对databind的调用正在重置这些值。 If I need to call databind explicitly I do it after control changed events eg In LoadComplete http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx I used to have this problem quite a lot but I'm wise to it now (touch wood). 如果我需要显式调用databind,则在控制更改事件后执行此操作,例如在LoadComplete http://msdn.microsoft.com/zh-cn/library/ms178472(v=vs.100).aspx中,我以前经常遇到此问题很多,但是我现在很明智(触摸木头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM