简体   繁体   English

在GridView下拉列表中的特定行

[英]dropdownlist in gridview for particular row

i have a dropdownlist in a gridview 我在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?? 但这并没有显示,因为它们发生了变化...有任何建议吗?

You're setting SelectedValue but your DropDownList Values are "84600", "604800", "0". 您正在设置SelectedValue,但是DropDownList的值为“ 84600”,“ 604800”,“ 0”。 You need to set the SelectedValue to "0" for "As they change" to show. 您需要将SelectedValue设置为“ 0”,以显示“随着它们更改”。

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: 您显示的下拉列表的值为“ 0”,因此您的代码需要将下拉列表的selectedvalue值设置为0。您正在将selectedvalue设置为列表项的文本,但由于list项目值是所有字符串,您还需要使用字符串值0,如下所示:

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";
            }
        }
    }

Shouldn't it be: 不应该是:

DropDownList1.SelectedValue = 0; DropDownList1.SelectedValue = 0;

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM