获取listBox项的自定义属性 [英] get custom attribute of listBox items

查看:78
本文介绍了获取listBox项的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi guys



i在获取列表框项目的自定义属性时遇到问题



i有选择采石场选择行并保存在DataTable中命名为dt



i添加列表框中的所有项目并设置自定义属性type



  foreach (DataRow ro  in  dt2。行)
{
li = new ListItem();
li.Value = ro [ fileOuId]。ToString();
li.Text = ro [ EsTittleF]。ToString();
li.Attributes.Add( type 论文);
ListBox1.Items.Add(li);
}





i希望获得列表框中的类型属性选择项目更改



  protected   void  ListBox1_SelectedIndexChanged( object  sender,EventArgs e)
{




string type = ;
type = ListBox1.SelectedItem.Attributes [ type];


if (type == 预订
// 预订
if (type == Essay
// essay
}





问题是:字符串类型在项目更改中为空,我可以在执行模式中看到每个项目的类型;



 <  选项    type   = 论文    value   =   242 >  Essssssssssssssssssay测试<   / option  >  

解决方案

实际上这是各种网站和论坛上记录的常见问题。动态添加ListItems并设置在回发期间不保留属性标记的属性。



我们需要从ListBox的基础派生一个控件并实现渲染的方法它是正确的。这篇文章描述了如何做到这一点并涵盖了这个问题(对于下拉列表)。



请点击这里....



本文涵盖DropdownBox,但ListItem类与下拉列表相同,与列表框控件类相同。



希望这对你有帮助。

hi guys

i have problems in getting custom attribute of list box items

i have select quarry that select rows and save in DataTable Named "dt"

i add all of items in list box and set custom attribute "type"

foreach (DataRow ro in dt2.Rows)
            {
                li = new ListItem();
                li.Value = ro["fileOuId"].ToString();
                li.Text = ro["EsTittleF"].ToString();
                li.Attributes.Add("type", "Essay");
                ListBox1.Items.Add(li);
            }



i want to get type attribute on list box select item change

 protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       

       

        string type = "";
        type=ListBox1.SelectedItem.Attributes["type"];

        
        if (type=="Book" )
             // Book
        if (type=="Essay")
             //essay
}



the problem is : "string type" is null in item change ,,, i can see type in each item in execute mode;

<option type="Essay" value="242">Esssssssssssssssssay test </option>

解决方案

Actually this is a common issue documented on various sites and forums. When adding ListItems dynamically and setting attributes that the attribute tags are not retained during postback.

We need to derive a control from the base of ListBox and implement the methods to render it properly.Here is an article that describes how to do this and covers this issue(for dropdownlist).

Please Click here....

The article covers the DropdownBox but the ListItem class is the same class for dropdownlists as it is for the listbox control.

Hope this will definitely of help for you.


这篇关于获取listBox项的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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