Wednesday, November 16, 2011
RadioButtonList DropDownList In GridView Edit Mode In ASP.NET
In this example i am going to describe how to implement RadioButtonList and DropDOwnList in Edit mode of GridVIew using EditItemTemaplate in ASP.NET C# and VB.NET.
RadioButton and DropDOwnList are selected in edit mode based on value saved in DataBase
HTML markup of aspx page is mentioned below
VB.NET Code Behind
Hope this helps.
Download sample code attached
RadioButton and DropDOwnList are selected in edit mode based on value saved in DataBase
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;
}
VB.NET Code Behind
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim dRowView As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim rblGender As RadioButtonList = DirectCast(e.Row.FindControl("rbGenderEdit"), RadioButtonList)
Dim ddlStatus As DropDownList = DirectCast(e.Row.FindControl("ddlStatusEdit"), DropDownList)
rblGender.SelectedValue = dRowView(2).ToString()
ddlStatus.SelectedValue = dRowView(3).ToString()
End If
End If
End Sub
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim rblGender As RadioButtonList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("rbGenderEdit"), RadioButtonList)
Dim ddlStatus As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("ddlStatusEdit"), DropDownList)
SqlDataSource1.UpdateParameters("Sex").DefaultValue = rblGender.SelectedValue
SqlDataSource1.UpdateParameters("MaritalStauts").DefaultValue = ddlStatus.SelectedValue
End Sub
Download sample code attached