HTML SOURCE OF ASPX PAGE
<asp:DetailsView ID="DetailsView2" runat="server"
AllowPaging="True" DataKeyNames="ID"
AutoGenerateRows="False"
DataSourceID="SqlDataSource2" Height="50px"
Width="125px"
onDataBound="DetailsView2_DataBound"
onItemUpdating="DetailsView2_ItemUpdating">
<Fields>
<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="rbGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%#Eval("Status") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server">
<asp:ListItem>Single</asp:ListItem>
<asp:ListItem>Married</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Sex], [Status] FROM [Details]"
UpdateCommand="UPDATE Details SET Name = @Name, Sex = @Sex,
MaritalStatus = @Stauts WHERE (ID = @ID)">
<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="Sex" />
<asp:Parameter Name="Stauts" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>
Now to pouplate value from database in selected index of dropdown and then set the new selected value in sqldatasource parameter write below mentioned code in DataBound and ItemUpdating Event of DetailsView respectively.
C# CODE
01
protected
void
DetailsView2_DataBound(
object
sender, EventArgs e)
02
{
03
if
(((DetailsView)sender).CurrentMode == DetailsViewMode.Edit)
04
{
05
DataRowView row = (DataRowView)((DetailsView)sender).DataItem;
06
RadioButtonList rblGender = (RadioButtonList)((DetailsView)sender).FindControl(
"rbGender"
);
07
DropDownList ddlStatus = (DropDownList)((DetailsView)sender).FindControl(
"ddlStatus"
);
08
rblGender.SelectedValue = row[2].ToString();
09
ddlStatus.SelectedValue = row[3].ToString();
10
}
11
}
12
protected
void
DetailsView2_ItemUpdating(
object
sender, DetailsViewUpdateEventArgs e)
13
{
14
RadioButtonList rblGender = (RadioButtonList)((DetailsView)sender).FindControl(
"rbGender"
);
15
DropDownList ddlStatus = (DropDownList)((DetailsView)sender).FindControl(
"ddlStatus"
);
16
SqlDataSource2.UpdateParameters[
"Sex"
].DefaultValue = rblGender.SelectedValue;
17
SqlDataSource2.UpdateParameters[
"Stauts"
].DefaultValue = ddlStatus.SelectedValue;
18
}
VB.NET CODE
01
Protected
Sub
DetailsView2_DataBound(sender
As
Object
, e
As
EventArgs)
02
If
DirectCast
(sender, DetailsView).CurrentMode = DetailsViewMode.Edit
Then
03
Dim
row
As
DataRowView =
DirectCast
(
DirectCast
(sender, DetailsView).DataItem, DataRowView)
04
Dim
rblGender
As
RadioButtonList =
DirectCast
(
DirectCast
(sender, DetailsView).FindControl(
"rbGender"
), RadioButtonList)
05
Dim
ddlStatus
As
DropDownList =
DirectCast
(
DirectCast
(sender, DetailsView).FindControl(
"ddlStatus"
), DropDownList)
06
rblGender.SelectedValue = row(2).ToString()
07
ddlStatus.SelectedValue = row(3).ToString()
08
End
If
09
End
Sub
10
Protected
Sub
DetailsView2_ItemUpdating(sender
As
Object
, e
As
DetailsViewUpdateEventArgs)
11
Dim
rblGender
As
RadioButtonList =
DirectCast
(
DirectCast
(sender, DetailsView).FindControl(
"rbGender"
), RadioButtonList)
12
Dim
ddlStatus
As
DropDownList =
DirectCast
(
DirectCast
(sender, DetailsView).FindControl(
"ddlStatus"
), DropDownList)
13
SqlDataSource2.UpdateParameters(
"Sex"
).DefaultValue = rblGender.SelectedValue
14
SqlDataSource2.UpdateParameters(
"Stauts"
).DefaultValue = ddlStatus.SelectedValue
15
End
Sub
0 comments:
Post a Comment