gpt4 book ai didi

c# - 为从数据库中填充的下拉列表设置默认值

转载 作者:行者123 更新时间:2023-11-30 15:31:20 24 4
gpt4 key购买 nike

我正在从数据库中填充我的下拉列表,但我想将“请选择一个项目”与其他选项一起添加到下拉列表中,并使其成为默认选定项目:
我尝试设置writerddl.SelectedValue = "please...";但运行时错误显示:

'writerddl' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
writerddl.SelectedValue = "please...";
if (!IsPostBack)
{
writerddl.DataSource = DS.show_all_writers();
writerddl.DataValueField = "writerid";
writerddl.DataTextField = "writersname";
writerddl.DataBind();
}
}

最佳答案

试试这段代码。

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
writerddl.DataSource = DS.show_all_writers();
writerddl.DataValueField = "writerid";
writerddl.DataTextField = "writersname";
writerddl.DataBind();
writerddl.Items.insert(0, new ListItem("Please select",""));
}
}

关于c# - 为从数据库中填充的下拉列表设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20755549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com