Reputation: 4630
i have a dropdownlist in a gridview
<asp:TemplateField HeaderText="Backup Frequency">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Once a Day" Value="86400">Once a Day</asp:ListItem>
<asp:ListItem Text="Once a weak" Value="604800">Once a week</asp:ListItem>
<asp:ListItem Text="As They Change" Value="0">As They Change</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
now it displays Once a Day in the dropdownlist in each row.... but i want that the first row should have its dropdownlist showing "As They Change"...
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow myRow in GridView1.Rows)
{
Label Label1 = (Label)myRow.FindControl("Label1");
if (Label1.Text == "User Data")
{DropDownList DropDownList1 = (DropDownList)myRow.FindControl("DropDownList1");
DropDownList1.SelectedValue = "As They Change";
}
}
}
but this is not showing As they change... Any suggestions??
Upvotes: 0
Views: 723
Reputation: 1946
The value of the drop down list you've shown is "0", so your code would need to set the selectedvalue value of the drop down list to 0. You are setting the selectedvalue to the text of the list item, but since list item values are all strings you will also need to use the string value of 0, like this:
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow myRow in GridView1.Rows)
{
Label Label1 = (Label)myRow.FindControl("Label1");
if (Label1.Text == "User Data")
{DropDownList DropDownList1 = (DropDownList)myRow.FindControl("DropDownList1");
DropDownList1.SelectedValue = "0";
}
}
}
Upvotes: 1
Reputation: 4623
You're setting SelectedValue but your DropDownList Values are "84600", "604800", "0". You need to set the SelectedValue to "0" for "As they change" to show.
Upvotes: 2