Search This Blog

Thursday, August 5, 2010

How to add radiobuttons in gridview and return row datakey value on their click

Source code of gridview is given below. In this i have used literal control in place of radiobutton. At runtime i will convert literal control in to radio control

   1:  <asp:GridView ID="GridDisc" runat="server" AutoGenerateColumns="False" AllowSorting="true" 
   2:           DataKeyNames="AdminDisciplineID" onrowdatabound="GridDisc_RowDataBound" 
   3:          onrowcreated="GridDisc_RowCreated" Width="1000px">              
   4:  <Columns>
   5:   
   6:  <asp:TemplateField >
   7:         <ItemTemplate >
   8:                 <asp:Literal ID="RadioButtonMarkup" runat="server">asp:Literal>
   9:         ItemTemplate>
  10:  asp:TemplateField>
  11:  <asp:TemplateField  HeaderText="AdminDisciplineCode" >
  12:  <ItemTemplate>
  13:  <asp:HyperLink ID="AdminDisciplineID" runat="server" Text='<%# Eval("AdminDisciplineCode") %>' />
  14:  ItemTemplate>
  15:  asp:TemplateField>
  16:     <asp:BoundField ItemStyle-CssClass="top"  HeaderText="Discipline Name" DataField="DisciplineName"/>
  17:     <asp:BoundField ItemStyle-CssClass="top"  HeaderText="DisciplineDescription" DataField="DisciplineDescription" />
  18:     <asp:BoundField  ItemStyle-CssClass="top" HeaderText="Active" DataField="Active"/>
  19:    <asp:BoundField  ItemStyle-CssClass="top" HeaderText="ModifiedOn" DataField="ModifiedOn"/>
  20:  Columns>                
  21:  asp:GridView>
 
Below is c# code to generate radio button at run time and
 i  have used property to return datakeyvalue of row on radiobutton click
 




   1:  protected void GridDisc_RowCreated(object sender, GridViewRowEventArgs e
   2:      {
   3:          if (e.Row.RowType == DataControlRowType.DataRow && 
   4:               (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
   5:          {
   6:             
   7:         Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");
   8:         output.Text = string.Format(@"< input type equalto 'radio' name='SuppliersGroup' "
              @"id='RowSelector{0}' value='{0}' />",e.Row.RowIndex);
                  // replace equal to by =  in just above line written
   9:          }
  10:      }
  11:  
  12:   
  13:  private int SuppliersSelectedIndex
  14:      {
  15:          get
  16:          {
  17:              if (string.IsNullOrEmpty(Request.Form["SuppliersGroup"]))
  18:                  return -1;
  19:              else                               
  20:             return Convert.ToInt32(Request.Form["SuppliersGroup"]);
  21:          }
  22:      }
  23:   
  24:   protected void link_Click1(object sender, EventArgs e)
  25:   {
  26:   
  27:  a = Convert.ToString(GridDisc.DataKeys[SuppliersSelectedIndex].Value); 
  28:             //calling property to get datakey value retuned
  29:  }
Here SuppliersSelectedIndex is property Below screenshot of gridview is given
Gridview screen shot
 

No comments:

Post a Comment