ASP.NET - RadioButtonList in Edit mode of Grid using EditItemTemplate in ASP.NET

Asked By Venkatesh Desai on 27-Sep-11 04:36 AM

Hi All,

 

Actually My Problem is, RadioButtons are not selected in edit mode based on value saved in Database.

 

The Below Mentioned is my code in both aspx Page & aspx.cs Page.

 

<asp:TemplateField HeaderText="Billable" SortExpression="Billable">

              <ItemStyle HorizontalAlign ="Left" />

              <HeaderStyle HorizontalAlign ="Left" />

              <ItemTemplate><%#(Boolean.Parse(Eval("Billable").ToString())) ? "Billable" : "Non-Billable"%></ItemTemplate>

              <EditItemTemplate>

              <asp:RadioButtonList ID="rbtnlBillable" runat="server" RepeatDirection="Horizontal">

              <asp:ListItem>Billable</asp:ListItem>

              <asp:ListItem>Non-Billable</asp:ListItem>

              </asp:RadioButtonList>

              </EditItemTemplate>

            </asp:TemplateField>

           

            <asp:TemplateField HeaderText="Taxable" SortExpression="Taxable">

              <ItemTemplate><%#(Boolean.Parse(Eval("Taxable").ToString())) ? "Taxable" : "Non-Taxable"%></ItemTemplate>

              <EditItemTemplate>

              <asp:RadioButtonList ID="rbtnlTaxable" runat="server" RepeatDirection="Horizontal">

              <asp:ListItem>Taxable</asp:ListItem>

              <asp:ListItem>Non-Taxable</asp:ListItem>

              </asp:RadioButtonList>

              </EditItemTemplate>

            </asp:TemplateField>

 

 

protected void gvActivities_RowDataBound(object sender, GridViewRowEventArgs e)

    {

   

    DataRowView DRV = (DataRowView)e.Row.DataItem;

      if (e.Row.RowType == DataControlRowType.DataRow)

      {

        if ((e.Row.RowState & DataControlRowState.Edit) > 0)

        {

          RadioButtonList Billable = (RadioButtonList)e.Row.FindControl("rbtnlBillable");

          Billable.SelectedValue = DRV[2].ToString();

          RadioButtonList Taxable = (RadioButtonList)e.Row.FindControl("rbtnlTaxable");

          Taxable.SelectedValue = DRV[3].ToString();

        }

      }

 

    }

 

 

The Below Image is that after clicking on edit button in gridview.

 

 

Regards,

Venki Desai.
aneesa replied to Venkatesh Desai on 27-Sep-11 04:41 AM

RadioButton and DropDOwnList are selected in edit mode based on value saved in DataBase

http://i543.photobucket.com/albums/gg462/amitjain/csharpBlog/RadioBUttonListInGrid.gif


HTML markup of aspx page is mentioned below
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" 
              AutoGenerateColumns="False" 
              DataSourceID="SqlDataSource1" 
              OnRowDataBound="GridView1_RowDataBound" 
              OnRowUpdated="GridView1_RowUpdated" 
              OnRowUpdating="GridView1_RowUpdating" 
              OnRowEditing="GridView1_RowEditing">
 <Columns>
 <asp:TemplateField HeaderText="ID">
 <ItemTemplate>
 <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'>
 </asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 <asp:BoundField DataField="Name" HeaderText="Name" 
                 SortExpression="Name" />
 <asp:TemplateField HeaderText="Gender">
 <ItemTemplate>
 <asp:Label ID="lblGender" runat="server" 
            Text='<%#Eval("Sex") %>'>
 </asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
 <asp:RadioButtonList ID="rbGenderEdit" runat="server">
 <asp:ListItem>Male</asp:ListItem>
 <asp:ListItem>Female</asp:ListItem>
 </asp:RadioButtonList>
 </EditItemTemplate>
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Marital Status">
 <ItemTemplate>
 <asp:Label ID="lblStatus" runat="server" 
            Text='<%#Eval("MaritalStatus") %>'>
 </asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
 <asp:DropDownList ID="ddlStatusEdit" runat="server">
 <asp:ListItem>Single</asp:ListItem>
 <asp:ListItem>Married</asp:ListItem>
 </asp:DropDownList>
 </EditItemTemplate>
 </asp:TemplateField>
 <asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Sex], [MaritalStatus] 
               FROM [Details]" 
UpdateCommand="Update Details Set [Name]=@Name, [Sex]=@Sex, 
              [MaritalStatus]=@MaritalStauts Where [ID]=@ID">
   <UpdateParameters>
       <asp:Parameter Name="Name" />
       <asp:Parameter Name="Sex" />
       <asp:Parameter Name="ID" />
       <asp:Parameter Name="MaritalStauts" />
   </UpdateParameters>
</asp:SqlDataSource>

C# Code Behind
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 DataRowView dRowView = (DataRowView)e.Row.DataItem;
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
   if ((e.Row.RowState & DataControlRowState.Edit) > 0)
   {
     RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl("rbGenderEdit");
     DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatusEdit");
     rblGender.SelectedValue = dRowView[2].ToString();
     ddlStatus.SelectedValue = dRowView[3].ToString();
   }
 }
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
 RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
 DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
 SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
 SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
}
smr replied to Venkatesh Desai on 27-Sep-11 04:43 AM
HI

here is good example

refer the link

http://www.codeproject.com/KB/webforms/editable_gridview_control.aspx
Reena Jain replied to Venkatesh Desai on 27-Sep-11 05:34 AM
Hi,

just give value attribute to rediobuttonlist control like this

<asp:RadioButtonList ID="rbtnlbillable" runat="server" Width="109px">
  <asp:ListItem Value="1">Billable</asp:ListItem>
  <asp:ListItem Value="0">Non-Billable</asp:ListItem>
   
</asp:RadioButtonList>

Hope this will help you
dipa ahuja replied to Venkatesh Desai on 27-Sep-11 07:26 AM
you don't need to select the radiobutton in editmode. because use will edit it by him/her self. Just put the right value in Itemtemplate.

in editmode write this way: Set one of the radioButton selected

<asp:RadioButtonList ID="rbtnlTaxable" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Selected="True">Taxable</asp:ListItem>
    <asp:ListItem>Non-Taxable</asp:ListItem>
  </asp:RadioButtonList>