Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Setting HtmlInputRadioButton name in GridView from code behind

Tags:

asp.net

On the RowDataBound event of a gridview I am setting the name of the HtmlInputRadioButton inside the GridViewRow. The problem is that asp.net automatically makes the name unique therefore ungrouping the radio buttons.

Is there a way that I disable this?

Edit - Here's how I am setting 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 of

<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">

Due to the numbers "106" and "107" in the name of the respective radiobuttons it ungroups them.

like image 532
Drahcir Avatar asked Dec 07 '25 09:12

Drahcir


1 Answers

You can override the name of the radio button by overriding the getter of the UniqueID

    private class MyHtmlInputRadioButton : HtmlInputRadioButton
    {
        public override string UniqueID
        {
            get
            {
                return string.IsNullOrEmpty(Name) ? base.UniqueID : Name;
            }
        }
    }
like image 105
Igor Z Avatar answered Dec 10 '25 00:12

Igor Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!