英文:
ASP.NET - How to add options in dropdown at server side
问题
我想通过服务器端添加options
,并且我已经在ASP.NET中尝试了下面的逻辑。
List<string> vendorname_list = null;
vendorname_list = (List<string>)Session["vendornamelist"];
if (vendorname_list.Count > 0)
{
ddlvendornames.Items.Clear();
string vnlst = "";
for (int i = 0; i < vendorname_list.Count; i++)
{
if (Session["vendornameselect"] != null && Session["vendornameselect"].ToString() == vendorname_list[i])
vnlst = "<option Selected='True' value=" + vendorname_list[i] + ">" + vendorname_list[i] + "</option>";
else
vnlst += "<option value=" + vendorname_list[i] + ">" + vendorname_list[i] + "</option>";
}
// ddlvendornames.Items.Add(vnlst);
ddlvendornames.DataSource = vnlst; // 它没有正确地追加
ddlvendornames.DataBind();
}
在Session["vendornamelist"]
中,我通过循环添加了值的列表。我正在将它们添加到字符串变量和下拉变量(ddlvendornames
)中。
请告诉我是否可以使用服务器端从<option>
标记添加下拉框值。
抱歉,我的英文不太好。
英文:
I want to add options
through the server side and I have tried the below logic in ASP.NET.
List<string> vendorname_list = null;
vendorname_list = (List<string>)Session["vendornamelist"];
if (vendorname_list.Count > 0)
{
ddlvendornames.Items.Clear();
string vnlst = "";
for (int i = 0; i < vendorname_list.Count; i++)
{
if (Session["vendornameselect"] != null && Session["vendornameselect"].ToString() == vendorname_list[i])
vnlst = "<option Selected='True' value=" + vendorname_list[i] + ">" + vendorname_list[i] + "</option>";
else
vnlst += "<option value=" + vendorname_list[i] + ">" + vendorname_list[i] + "</option>";
}
// ddlvendornames.Items.Add(vnlst);
ddlvendornames.DataSource = vnlst; // it is not appending properly
ddlvendornames.DataBind();
}
In the Session["vendornamelist"]
, I have added the list of values through a loop. I'm adding to the string variable and the dropdown variable (ddlvendornames
).
Please let me know if adding dropdown values using the <option>
tag from the server side is possible.
Sorry for my bad English.
答案1
得分: 2
如评论中所述,建议将选项列表与 ListItem 绑定,而不是编写和添加 <option>
元素。
List<ListItem> vnlst = new List<ListItem>();
int? selectedIndex = null;
for (int i = 0; i < vendorname_list.Count; i++)
{
if (Session["vendornameselect"] != null && Session["vendornameselect"].ToString() == vendorname_list[i])
{
selectedIndex = i;
vnlst.Add(new ListItem(vendorname_list[i], vendorname_list[i], true));
}
else
{
vnlst.Add(new ListItem(vendorname_list[i], vendorname_list[i]));
}
}
ddlvendornames.DataSource = vnlst;
ddlvendornames.DataTextField = "Text";
ddlvendornames.DataValueField = "Value";
if (selectedIndex.HasValue)
ddlvendornames.SelectedIndex = selectedIndex.Value;
ddlvendornames.DataBind();
英文:
As mentioned in the comment, would recommend binding the option list with ListItems instead of writing and adding the <option>
element.
List<ListItem> vnlst = new List<ListItem>();
int? selectedIndex = null;
for (int i = 0; i < vendorname_list.Count; i++)
{
if (Session["vendornameselect"] != null && Session["vendornameselect"].ToString() == vendorname_list[i])
{
selectedIndex = i;
vnlst.Add(new ListItem(vendorname_list[i], vendorname_list[i], true));
}
else
{
vnlst.Add(new ListItem(vendorname_list[i], vendorname_list[i]));
}
}
ddlvendornames.DataSource = vnlst;
ddlvendornames.DataTextField = "Text";
ddlvendornames.DataValueField = "Value";
if (selectedIndex.HasValue)
ddlvendornames.SelectedIndex = selectedIndex.Value;
ddlvendornames.DataBind();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论