Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<asp:TemplateField HeaderText="New Position">                       
     <headertemplate>
          <asp:Label ID="lblpos" runat="server">New Position
          <asp:RadioButtonList ID="rblYesNoPos" runat="server" AutoPostBack="true">
                <asp:ListItem Selected="True" Value="Yes">Yes
                 <asp:ListItem Value="No">No
          
     
     <itemtemplate>
        <asp:Label ID="lblPosTot" runat="server" Text='<%# Bind("PosTotal")%>'>                            
     

<asp:TemplateField HeaderText="Replacement">
     <headertemplate>
         <asp:Label ID="lblre" runat="server">Replacement
         <asp:RadioButtonList ID="rblYesNoRe" runat="server" AutoPostBack="true">
         <asp:ListItem Selected="True" Value="Yes" Text="Yes">Yes
         <asp:ListItem Value="No" Text="No">No
         
     
     <itemtemplate>
          <asp:Label ID="lblReTot" runat="server" Text='<%# Bind("ReTotal")%>'>
      
<


What I have tried:

C#
RadioButtonList rbl = (RadioButtonList)e.Row.Cells[1].FindControl("rblYesNoPos");
RadioButtonList rblR = (RadioButtonList)e.Row.Cells[1].FindControl("rblYesNoRe");
Posted
Updated 12-Nov-18 5:01am
v2

Try doing FindControl on the Row

(RadioButtonList)e.Row.FindControl("rblYesNoPos");
 
Share this answer
 
That depends on where do you want to access it. If you want it at RowDataBound event, then you need to make sure you are checking for the appropriate DataControlRowType first before using FindControl method. For example:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //access header template controls
    if (e.Row.RowType == DataControlRowType.Header)
    {
         RadioButtonList rblHeader = (RadioButtonList)e.Row.FindControl("rblYesNoPos");

    }

    //access row template controls
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         RadioButtonList rblRow = (RadioButtonList)e.Row.FindControl("rblYesNoRe");

    }
} 


If want to reference them at Button Click event where your Button is inside the GridView template, then you can do something like this:

C#
protected void Button1_Click(object sender, EventArgs e)
{
        Button b = (Button)sender;
        GridViewRow row = (GridViewRow)b.NamingContainer;
        if (row != null)
        {
            //access header template controls
            if (row.RowType == DataControlRowType.Header)
            {
                      RadioButtonList rblHeader = (RadioButtonList)row.FindControl("rblYesNoPos");

            }

           //access row template controls
           if (row.RowType == DataControlRowType.DataRow)
           {
                     RadioButtonList rblRow = (RadioButtonList)row.FindControl("rblYesNoRe");

           }
      }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900