DropDownList And RadioButtonList In EditItemTemplate Of DetailsView to Edit and Update In Asp.Net.
In This Example i'm going to explain how to put dropdownlist and radiobutton control in EditItemTemplate of DetailsView and Edit or Update Records of DetailsView in asp.net.
For this i have used SqlDataSource To fetch records from database and populate and update detailsview.
We need to write code in DataBound And ItemUpdating Events of Detailsview to get the selected value in dropdownlist from database when edit or update button is pressed.
HTML SOURCE OF ASPX PAGE
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.
Build and run the application.
In This Example i'm going to explain how to put dropdownlist and radiobutton control in EditItemTemplate of DetailsView and Edit or Update Records of DetailsView in asp.net.
For this i have used SqlDataSource To fetch records from database and populate and update detailsview.
We need to write code in DataBound And ItemUpdating Events of Detailsview to get the selected value in dropdownlist from database when edit or update button is pressed.
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
protected void DetailsView2_DataBound(object sender, EventArgs e) { if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit) { DataRowView row = (DataRowView)((DetailsView)sender).DataItem; RadioButtonList rblGender = (RadioButtonList)((DetailsView)sender).FindControl("rbGender"); DropDownList ddlStatus = (DropDownList)((DetailsView)sender).FindControl("ddlStatus"); rblGender.SelectedValue = row[2].ToString(); ddlStatus.SelectedValue = row[3].ToString(); } } protected void DetailsView2_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { RadioButtonList rblGender = (RadioButtonList)((DetailsView)sender).FindControl("rbGender"); DropDownList ddlStatus = (DropDownList)((DetailsView)sender).FindControl("ddlStatus"); SqlDataSource2.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue; SqlDataSource2.UpdateParameters["Stauts"].DefaultValue = ddlStatus.SelectedValue; }
VB.NET CODE
Protected Sub DetailsView2_DataBound(sender As Object, e As EventArgs) If DirectCast(sender, DetailsView).CurrentMode = DetailsViewMode.Edit Then Dim row As DataRowView = DirectCast(DirectCast(sender, DetailsView).DataItem, DataRowView) Dim rblGender As RadioButtonList = DirectCast(DirectCast(sender, DetailsView).FindControl("rbGender"), RadioButtonList) Dim ddlStatus As DropDownList = DirectCast(DirectCast(sender, DetailsView).FindControl("ddlStatus"), DropDownList) rblGender.SelectedValue = row(2).ToString() ddlStatus.SelectedValue = row(3).ToString() End If End Sub Protected Sub DetailsView2_ItemUpdating(sender As Object, e As DetailsViewUpdateEventArgs) Dim rblGender As RadioButtonList = DirectCast(DirectCast(sender, DetailsView).FindControl("rbGender"), RadioButtonList) Dim ddlStatus As DropDownList = DirectCast(DirectCast(sender, DetailsView).FindControl("ddlStatus"), DropDownList) SqlDataSource2.UpdateParameters("Sex").DefaultValue = rblGender.SelectedValue SqlDataSource2.UpdateParameters("Stauts").DefaultValue = ddlStatus.SelectedValue End Sub
Build and run the application.
If you like this post than join us or share
0 comments:
Post a Comment