views:

49

answers:

3

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??

+2  A: 

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.

DavidGouge
oh my god... really sorry for askin this questionthanks tough
No problem, I quite regularly can't see a blatant problem with my code until I've asked someone about it! ;)
DavidGouge
+1  A: 

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";
            }
        }
    }
kscott
thanks... DropDownList1.SelectedValue = "0";does the trick too
yeah, 0.ToString() is just stupid. Typing faster than I was thinking.
kscott
haha..its k..i think even i was thinkin too hard to not see that..thanks budd..
A: 

Shouldn't it be:

DropDownList1.SelectedValue = 0;

?

willvv
thanks.. this worked