RadioButtonList DropDownList In GridView Edit Mode In ASP.NET
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
Hope this helps.
Download sample code attached
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
<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
01
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
02
{
03
DataRowView dRowView = (DataRowView)e.Row.DataItem;
04
if
(e.Row.RowType == DataControlRowType.DataRow)
05
{
06
if
((e.Row.RowState & DataControlRowState.Edit) > 0)
07
{
08
RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl(
"rbGenderEdit"
);
09
DropDownList ddlStatus = (DropDownList)e.Row.FindControl(
"ddlStatusEdit"
);
10
rblGender.SelectedValue = dRowView[2].ToString();
11
ddlStatus.SelectedValue = dRowView[3].ToString();
12
}
13
}
14
15
}
16
17
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
18
{
19
RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl(
"rbGenderEdit"
);
20
DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl(
"ddlStatusEdit"
);
21
SqlDataSource1.UpdateParameters[
"Sex"
].DefaultValue = rblGender.SelectedValue;
22
SqlDataSource1.UpdateParameters[
"MaritalStauts"
].DefaultValue = ddlStatus.SelectedValue;
23
}
VB.NET Code Behind
01
Protected
Sub
GridView1_RowDataBound(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewRowEventArgs)
02
Dim
dRowView
As
DataRowView =
DirectCast
(e.Row.DataItem, DataRowView)
03
If
e.Row.RowType = DataControlRowType.DataRow
Then
04
If
(e.Row.RowState
And
DataControlRowState.Edit) > 0
Then
05
Dim
rblGender
As
RadioButtonList =
DirectCast
(e.Row.FindControl(
"rbGenderEdit"
), RadioButtonList)
06
Dim
ddlStatus
As
DropDownList =
DirectCast
(e.Row.FindControl(
"ddlStatusEdit"
), DropDownList)
07
rblGender.SelectedValue = dRowView(2).ToString()
08
ddlStatus.SelectedValue = dRowView(3).ToString()
09
End
If
10
11
End
If
12
End
Sub
13
14
Protected
Sub
GridView1_RowUpdating(
ByVal
sender
As
Object
,
ByVal
e
As
GridViewUpdateEventArgs)
15
Dim
rblGender
As
RadioButtonList =
DirectCast
(GridView1.Rows(e.RowIndex).FindControl(
"rbGenderEdit"
), RadioButtonList)
16
Dim
ddlStatus
As
DropDownList =
DirectCast
(GridView1.Rows(e.RowIndex).FindControl(
"ddlStatusEdit"
), DropDownList)
17
SqlDataSource1.UpdateParameters(
"Sex"
).DefaultValue = rblGender.SelectedValue
18
SqlDataSource1.UpdateParameters(
"MaritalStauts"
).DefaultValue = ddlStatus.SelectedValue
19
End
Sub
Hope this helps.
Download sample code attached
0 comments: