ASP.NET Configuring the HtmlInputRadioButton Name in a GridView from Code

In the RowDataBoundgridview event , I set the name HtmlInputRadioButtoninside GridViewRow. The problem is that asp.net automatically makes the name unique, so ungroup the radio buttons.

Is there any way to disable this?

Edit - This is how I set the name:

  Private Sub gvFoo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSecPos.RowDataBound
If Not (e.Row.RowType = DataControlRowType.DataRow) OrElse (e.Row.Cells(0) Is Nothing) Then
  Return
End If
Dim drFoo As DataRowView = CType(e.Row.DataItem, DataRowView)

Dim rbFoo As HtmlInputRadioButton = CType(e.Row.FindControl("rbFoo"), HtmlInputRadioButton)
rbFoo.Name = "Foo" 'ASP.NET makes this unique which I do not want
rbFoo.Value = "A Value"

  End Sub

Produces this html

<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl06_rbFoo" name="ctl00$ContentPlaceHolder1$ucFoo$gvFoo$ctl06$Foo" value="A Value">

<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl07_rbFoo" name="ctl00$ContentPlaceHolder1$ucFoo$gvFoo$ctl07$Foo" value="A Value">

Instead

<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl06_rbFoo" name="Foo" value="A Value">

<input type="radio" id="ctl00_ContentPlaceHolder1_ucFoo_gvFoo_ctl07_rbFoo" name="Foo" value="A Value">

Because of the numbers "106" and "107" in the name of the corresponding radio objects, he groups them.

+3
source share
4 answers

You can override the switch name by overriding the uniqueID receiver

    private class MyHtmlInputRadioButton : HtmlInputRadioButton
    {
        public override string UniqueID
        {
            get
            {
                return string.IsNullOrEmpty(Name) ? base.UniqueID : Name;
            }
        }
    }
+5
source

I think you should use a simple switch. <input type="radio" name="blahblah"/>

+1
source
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
     HtmlInputRadioButton control;
    if (e.Row.RowType = DataControlRowType.DataRow)
    {
        control = e.FindControl("ControlID") as HtmlInputRadioButton;

    }
    control.name ="xyz"
}
0

, , RadioButton, HtmlInputRadioButton Repeater. RadioButtonList , HTML , RadioButtonList ( Twitter Bootstrap ASP.NET WebForms).

JavaScript w/jQuery UniqueID, name , . , PostBack , ASP.NET .

This workaround worked fine in my scenario, but beware of any possible side effects or cases of edges. The page I applied this to was pretty simple. I did not check if it works with client side validation.

Here is a sample JavaScript code I used:

$(function(){

    $('input.custom-radio-name').each(function(i, radio){
        var name = radio.name,
            groupNameIndex = radio.name.lastIndexOf('$') + 1,
            groupName = radio.name.substring(groupNameIndex);
        // Preserve the original name, so that it can be restored
        $(this).data('aspnetname', name); 
        radio.name = groupName;
    });

    $('form').submit(function(){
        // Restore the name of the radio buttons,
        // otherwise errors will ensue upon PostBack.
        $('input.custom-radio-name').each(function(i, control){
            var aspnetname = $(this).data('aspnetname');
            control.name = aspnetname;
        });
    });

});

See an example at http://jsfiddle.net/yumN8/

0
source

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


All Articles