我需要在DropDownList DDL的开头添加一行为空:
<asp:ListItem Text="------" Value=""></asp:ListItem>
我尝试过使用这两种不同的解决方案,但没有成功。
这是我的代码。
解决方案#1
<FooterTemplate>
<asp:DropDownList ID="DDL" runat="server" Font-Bold="true" Font-Size="X-Small">
<asp:ListItem Text="------" Value=""></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
解决方案#2
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList DDL = (DropDownList)e.Row.FindControl("DDL");
sql = " SELECT DISTINCT FROM .....; ";
OdbcCommand cmd = new OdbcCommand(sql);
DDL.DataSource = GetData(cmd);
DDL.DataTextField = "combo";
DDL.DataValueField = "combo";
DDL.DataBind();
DDL.Items.Add(new ListItem("------", ""));
}
您可以将AppendDataBoundItems
-property设置为true:
<asp:DropDownList ID="DDL" runat="server" AppendDataBoundItems="true" Font-Bold="true" Font-Size="X-Small">
<asp:ListItem Text="------" Value=""></asp:ListItem>
</asp:DropDownList>
这可以声明性地(如上所述)或以编程方式工作:
DropDownList DDL = (DropDownList)e.Row.FindControl("DDL");
DDL.Items.Add(new ListItem("------", ""));
sql = " SELECT DISTINCT FROM .....; ";
OdbcCommand cmd = new OdbcCommand(sql);
DDL.DataSource = GetData(cmd);
DDL.DataTextField = "combo";
DDL.DataValueField = "combo";
DDL.DataBind();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句