ASP.NET – 如何在服务器端向下拉菜单添加选项

huangapple go评论62阅读模式
英文:

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&lt;string&gt; vendorname_list = null;
vendorname_list = (List&lt;string&gt;)Session[&quot;vendornamelist&quot;];

if (vendorname_list.Count &gt; 0)
{
    ddlvendornames.Items.Clear();
    string vnlst = &quot;&quot;;

    for (int i = 0; i &lt; vendorname_list.Count; i++)
    {
        if (Session[&quot;vendornameselect&quot;] != null &amp;&amp; Session[&quot;vendornameselect&quot;].ToString() == vendorname_list[i])
            vnlst = &quot;&lt;option Selected=&#39;True&#39; value=&quot; + vendorname_list[i] + &quot;&gt;&quot; + vendorname_list[i] + &quot;&lt;/option&gt;&quot;;
        else
            vnlst += &quot;&lt;option value=&quot; + vendorname_list[i] + &quot;&gt;&quot; + vendorname_list[i] + &quot;&lt;/option&gt;&quot;;
    }

    // ddlvendornames.Items.Add(vnlst);

    ddlvendornames.DataSource = vnlst; // it is not appending properly 
    ddlvendornames.DataBind();
}

In the Session[&quot;vendornamelist&quot;], 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 &lt;option&gt; tag from the server side is possible.

Sorry for my bad English.

答案1

得分: 2

如评论中所述,建议将选项列表与 ListItem 绑定,而不是编写和添加 &lt;option&gt; 元素。

List&lt;ListItem&gt; vnlst = new List&lt;ListItem&gt;();

int? selectedIndex = null;
for (int i = 0; i &lt; vendorname_list.Count; i++)
{
    if (Session[&quot;vendornameselect&quot;] != null &amp;&amp; Session[&quot;vendornameselect&quot;].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 = &quot;Text&quot;;
ddlvendornames.DataValueField = &quot;Value&quot;;

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 &lt;option&gt; element.

List&lt;ListItem&gt; vnlst = new List&lt;ListItem&gt;();

int? selectedIndex = null;
for (int i = 0; i &lt; vendorname_list.Count; i++)
{
    if (Session[&quot;vendornameselect&quot;] != null &amp;&amp; Session[&quot;vendornameselect&quot;].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 = &quot;Text&quot;;
ddlvendornames.DataValueField = &quot;Value&quot;;

if (selectedIndex.HasValue)
    ddlvendornames.SelectedIndex = selectedIndex.Value;

ddlvendornames.DataBind();

huangapple
  • 本文由 发表于 2023年7月10日 16:02:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定