RadioName GroupName parameter in dynamic loading of the user.

I have User Control

<table style="border-width: 0">
    <tr>
        <td style="vertical-align: middle;">
            <asp:RadioButton ID="rdOption" runat="server" Text="I m testing"  
                GroupName="Questions" oncheckedchanged="rdOption_CheckedChanged" 
                AutoPostBack="True"/>
        </td>
        <td style="vertical-align: middle; padding-left: 10px">
            <asp:TextBox ID="txtOthers" runat="server" CssClass="txtbox" Visible="false"></asp:TextBox>
        </td>
    </tr>
</table>

protected void Page_Load(object sender, EventArgs e)
    {
        rdOption.GroupName = "myGroup";
        rdOption.Text = Option.OptionDesc;
    }

on Survery.aspx I loaded this user control dynamically

 foreach (clsOptions option in _CurrentQuestion.Options)
        {
            UserControls_OptionField ctrl = Page.LoadControl("~/UserControls/OptionField.ascx") as UserControls_OptionField;
            ctrl.Option = option;
            pnlOption.Controls.Add(ctrl);
        }

The problem is that each option has a different group name as shown below. This is why the parameters do not work properly, and the whole option can be selected, and in MCQ you can select only one option.

<input id="ContentPlaceHolder1_ctl01_rdOption" type="radio" name="ctl00$ContentPlaceHolder1$ctl01$myGroup" value="rdOption">

<input id="ContentPlaceHolder1_ctl02_rdOption" type="radio" name="ctl00$ContentPlaceHolder1$ctl02$myGroup" value="rdOption">

enter image description here

+3
source share
5 answers

I think this is a bug that has been in ASP.Net since 1.0 (surprisingly, they completely forgot ^^).

You can try the following solution, which works for Repeater (or any data binding control), but should also work for your dynamic UserControls.

, ASP.Net NamingContainers, RadioButtons .

http://weblogs.asp.net/joseguay/archive/2008/07/24/having-radiobuttons-on-a-repeater-or-datalist.aspx

HEAD :

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

onclick RadioButtons :

string script = "SetUniqueRadioButton('rdOption.*myGroup',this)";
rdOption.Attributes.Add("onclick", script);

[ ]

+2

, , .

Joel , , Render-, .

, Render() :

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        using (var stringWriter = new StringWriter())
        using (var htmlWriter = new HtmlTextWriter(stringWriter))
        {
            base.Render(htmlWriter);
            var tagText = stringWriter.ToString();

            tagText = Regex.Replace(tagText, "name=\"(\\S+)\"", "name=\"" + GroupName + "\"");

            writer.Write(tagText);
        }
    }

, , .

, :

  • , ,

  • <asp:RadioButton: , , web.config:

  <system.web>
    <pages>
      <tagMapping>
        <add tagType="System.Web.UI.WebControls.RadioButton" mappedTagType="WebformsLibrary.CustomRadioButton" />
      </tagMapping>
    </pages>
  </system.web>

WebformsLibrary.CustomRadioButton - .

+3

"GroupName" RadioButtons .

EDIT: .net 4, , ClientIDMode . , , asp.net( , /listview ).

INamingContainer RadioButtonGroup usercontrol. , javascript .

0

. , , , , , , , , , .

function fixRadioButtons(s, e) {
    var re = /^(?:.*\$)?(.*)$/;
    for (i = 0; i < document.forms[0].elements.length; i++) {
        var elm = document.forms[0].elements[i];
        if (elm.type == 'radio') {
            var match = re.exec(elm.name);
            elm.name = match[1];
        }
    }
}

JS -

// This code runs a function on postback
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(fixRadioButtons);

/: (VB.NET) -

ClientScript.RegisterStartupScript(Me.GetType(), "Javascript", "fixRadioButtons(null, null);", True)

Please note: if you want it to start every time, and not just after the postback, then you probably have to use it ClientScript.RegisterStartupScriptindependently.

0
source

Source: https://habr.com/ru/post/1795326/


All Articles