Display Dropdown When Editing GridView
I would like to display a field in my GridView as a DropDownList when a user is editing the row. The DropDownList would be pre-populated with two values, "Yes" and "No", and depending on which value a user selects I would like to set a variable.
Example:
I have a field called active. 1 = Active, 0 = Not Active. Although for the user I would like them to either set Active as "Yes" (1) or "No" (0). When when editing a row they could either choose "Yes" or "No" from the drop down and it would set a variable to either 1 or 0, so I can send that back in the SQL update.
I found this MSDN article.
but it only tells me how to populate the DropDownList from a DataSource, which won't work for me since every field has either a yes or no for active. Also it displays a drop down even when just viewing the GridView, not just when editing.
I hope that makes sense, thanks for the help.
EDIT
Here is the code I have now, it almost works like I'd like it to. All I need to do now is change the label text to "Yes" if the value of Active is "True" and change the text to "No" if the value of Active is "False".
<asp:TemplateField HeaderText="Active" SortExpression="Active" >
<ItemTemplate>
<asp:Label ID="lblActive" runat="server" text='<%# Eval("Active") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Active") %>'>
<asp:ListItem Text="Yes" Value="True"></asp:ListItem>
开发者_开发问答<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
You can add the listitem like...
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:DropDownList>
And you can bind the SelectedValue
and it will automatically pass the DB a 1 or o
.
Use the Dropdown in EditTemplate
and use a Label to show selection in ItemTemplate
See this example in msdn post (scroll all the way to the end for dropdownlist scenario).
put a label control (to show a current value in the database) and dropdownlist in edititem template
<asp:Label ID="lbl" runat="server" Text='<%#Eval("authostatus") %>' Visible="false"></asp:Label> <asp:DropDownList ID="Autharisationddl" runat="server"> <asp:ListItem Text="Yes" Value="1"></asp:ListItem> <asp:ListItem Text="No" Value="0"></asp:ListItem> </asp:DropDownList>
In gridview rowdatabound event set the dropdown value according to label control value.
if ((e.Row.RowState & DataControlRowState.Edit) > 0) { Label lbl= (Label)e.Row.FindControl("lbl"); DropDownList ddl= (DropDownList)e.Row.FindControl("ddl"); if (lbl!= null) { if (lbl.Text == "1") ddl.SelectedValue = "1"; else if (lbl.Text == "0") ddl.SelectedValue = "0"; } }
thanks
You don't need the rowdatabound, and just need to convert your column into a template field.
This is late reply, But it will help other people who come across same scenario.
codezone4 tutorial
精彩评论