Datagrid with Yes/No Dropdownlist

G

Guest

I have added a dropdownlist to an editable datagrid to allow a user to select
Yes or No:

<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,
"reports") %>' ID="lblReports"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" id="ddlReports">
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Works great.

My problem occurs when the user clicks the edit button. The value always
comes up to "Yes" in the dropdownlist, even if the value in the datagrid
label was "No".

How do I code it to make the value in the dropdownlist match the value in
the datagrid label before Edit was selected ?

Thanks,
Jeff
 
G

Guest

Add a DataBinder expression for the SelectedValue. Assuming that the field
named "Reports" holds a string value of either "Yes" or "No" then:

<asp:DropDownList ID="ddlReports" Runat="server"
SelectedValue='<%# DataBinder.Eval(Container.DataItem, "reports") %>'>
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>

But if the field value can have a blank or any other value, you need to
create a condition, e.g. (C# syntax)
<asp:DropDownList ID="ddlReports" Runat="server"
SelectedValue='<%# DataBinder.Eval(Container.DataItem,
"reports").Equals("Yes")?"Yes","No" %>'>
<asp:ListItem value="Yes">Yes</asp:listItem>
<asp:ListItem value="No">No</asp:listItem>
</asp:DropDownList>
 
N

Nilesh Deshpande

Jeff,

On ItemEdit event of DataGrid add this code

Dim ddlList as DropDownList
ddlList = e.Item.FindControl("ddlReports")

If Not ddlList Is Nothing Then
ddlList.selectedValue = e.Item.DataItem("ddlReports")
END If


HTH

Nilesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top