如何将属性添加到下拉列表然后执行数据绑定 [英] how do you add an attribute to the dropdownlist then perform a databind

查看:62
本文介绍了如何将属性添加到下拉列表然后执行数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向dropdownlist控件添加一个属性来存储为数据库返回的值。一旦在下拉列表中做出选择,该值就需要获取代码;



我尝试过以下内容:



ddlStatus.DataTextField = dsValues.Tables(0).Columns(Description)。ToString()

ddlStatus.DataValueField = dsValues.Tables(0).Columns(CategoryCode ).ToStrin g()

ddlStatus.Attributes.Add(SysCode,dsValues.Tables(0).Columns(SystemCode)。ToString())

ddlStatus.DataBind()



但该属性未绑定到控件

I am trying to add an attribute to a dropdownlist control to store a value returned for the database. This value is need to get a code once a selection is made on the dropdownlist;

I have tried the following:

ddlStatus.DataTextField = dsValues.Tables(0).Columns("Description").ToString ()
ddlStatus.DataValueField = dsValues.Tables(0).Columns("CategoryCode").ToStrin g()
ddlStatus.Attributes.Add("SysCode", dsValues.Tables(0).Columns("SystemCode").ToString( ))
ddlStatus.DataBind()

but the attribute is not bound to the control

推荐答案

我创建了以下代码并注释内联,以便您可以了解如何将数据绑定到DropDownList以及如何向下拉列表中的每个项添加属性(假设您要为每个项添加属性)。



这是aspx页面中的下拉列表:



I created the following codes and commented inline so that you can understand how to bind data to a DropDownList and how to add an attribute to each item in the drop down list (Assuming that, you wanted to add an attribute to each item).

Here is the Drop-Down list in the aspx page:

<asp:DropDownList ID="ddlStatus" runat="server">
        </asp:DropDownList>





以下是CodeBehind中的代码:





And here is the code in the CodeBehind:

public partial class Default : System.Web.UI.Page
{
    //A simple Person class for example
    class Person
    {
        public string Name
        {
            get;
            set;
        }
        public int Id
        {
            get;
            set;
        }
        public Person(string Name, int Id)
        {
            this.Name = Name;
            this.Id = Id;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Prepare some Person objects

        IList<Person> persons = new List<Person>();

        persons.Add(new Person("John",1));
        persons.Add(new Person("Tom",2));
        persons.Add(new Person("Shubho",3));

        //Specify the Name field of Person object as the text field of an item in the drop-down list
        
        ddlStatus.DataTextField = "Name";
        //Specify the Name field of Person object as the value field of an item in the drop-down list
        ddlStatus.DataValueField = "Id";

        //Assign the persons to the datasource of the drop down list
        ddlStatus.DataSource = persons;
        //Bind the data
        ddlStatus.DataBind();

        //Now, add a "SysCode" attribute to each item in the dropdown list
        for (int i = 0; i < ddlStatus.Items.Count; i++)
        {
            ListItem item = ddlStatus.Items[i];
            item.Attributes["SysCode"] = "Code-" + (i + 1).ToString();
        }

    }
}





以下是HTML标记将在浏览器中生成:





And, following is the HTML markup that will be generated in the browser:

<select id="ddlStatus" name="ddlStatus">
    <option syscode="Code-1" value="1">John</option>
    <option syscode="Code-2" value="2">Tom</option>
    <option syscode="Code-3" value="3">Shubho</option>
</select>





我希望,示例代码是自解释的,您可以理解如何实现您的代码。



我使用示例类来绑定数据(Person)。实际上,您应该从数据库中检索数据,并使用某种对象集合将数据绑定到DropDownList。



I hope, the example codes are self explanatory and you can understand how to implement your code.

I used an example class to bind data (Person). In reality you should retrieve data from the database and bind the data to the DropDownList using some kind of collections of object.


抱歉给旧线程的答案。你可以这样做。



ListItem test = new ListItem {Text = srText,Value = srValue}

test.Attributes.Add( data-imagesrc,xxx);

test.Attributes.Add(data-description,xxx);

dropListUserImages.Items.Add( test);
sorry for giving answer to old thread. you can do this way.

ListItem test = new ListItem { Text = srText, Value = srValue}
test.Attributes.Add("data-imagesrc", "xxx");
test.Attributes.Add("data-description", "xxx");
dropListUserImages.Items.Add(test);




@ Html.LabelFor(model => model.Departamento,htmlAttributes:new {@ class =control-label col-md-2})


@Html.LabelFor(model => model.Departamento, htmlAttributes: new { @class = "control-label col-md-2" })


@ Html.DropDownList(listDepto ,deptos.Select(x => new SelectListItem {Value = x.Id.ToString(),Text = x.Departamento}))

@ Html.ValidationMessageFor(model => model.Departamento ,,新{@class =text-danger})


@Html.DropDownList("listDepto", deptos.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Departamento }))
@Html.ValidationMessageFor(model => model.Departamento, "", new { @class = "text-danger" })



这篇关于如何将属性添加到下拉列表然后执行数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆