我在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>
现在它在每一行的下拉列表中显示每天一次....但我希望第一行的下拉列表显示“正如他们改变”......
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";
}
}
}
但这并未显示随着他们的变化...... 有什么建议??
答案 0 :(得分:2)
您正在设置SelectedValue,但您的DropDownList值为“84600”,“604800”,“0”。您需要将SelectedValue设置为“0”以显示“As they changed”。
答案 1 :(得分:1)
您显示的下拉列表的值为“0”,因此您的代码需要将下拉列表的selectedvalue值设置为0.您正在将selectedvalue设置为列表项的文本,但由于列表项值都是字符串,因此您还需要使用字符串值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";
}
}
}
答案 2 :(得分:0)
不应该是:
DropDownList1.SelectedValue = 0;