C# gridview中特定行的dropdownlist
我在gridview中有一个下拉列表C# gridview中特定行的dropdownlist,c#,asp.net,visual-studio,C#,Asp.net,Visual Studio,我在gridview中有一个下拉列表 <asp:TemplateField HeaderText="Backup Frequency"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="Once a Day" Value="86400">Once
<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>
但这并没有随着他们的改变而显现出来。。。
有什么建议吗?您正在设置SelectedValue,但您的下拉列表值是“84600”、“604800”、“0”。您需要将SelectedValue设置为“0”,以显示“更改时”。您正在设置SelectedValue,但下拉列表的值为“84600”、“604800”、“0”。您需要将SelectedValue设置为“0”,以显示“更改时”。您显示的下拉列表的值为“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";
}
}
}
您显示的下拉列表的值为“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";
}
}
}
难道不是:
DropDownList1.SelectedValue=0
是不是应该是:
DropDownList1.SelectedValue=0
?哦,我的天啊。。。真的很抱歉问这个问题,谢谢toughNo,没问题,我经常在问别人之前不会看到我的代码有明显的问题!;)哦,我的上帝。。。真的很抱歉问这个问题,谢谢toughNo,没问题,我经常在问别人之前不会看到我的代码有明显的问题!;)谢谢DropDownList1.SelectedValue=“0”;是的,0.ToString()太蠢了。打字比我想象的要快。谢谢。。。DropDownList1.SelectedValue=“0”;是的,0.ToString()太蠢了。打字比我想象的要快。