简体   繁体   English

绑定到缓存的字符串数组时如何操作DropDownList DataTextField

[英]How to manipulate a DropDownList DataTextField when bound to a cached string array

I have a dropdownlist bound to a cached string[], like so... 我有一个绑定到缓存的字符串[]的下拉列表,就像这样...

Cache["elems"] = items.elems;   //typeOf(items.elems)=string[]
DropDownList1.DataSource = Cache["elems"];
DropDownList1.DataBind();

I want to limit the length of text displayed in DropDownList1 eg an element called "Manufacturing" would display "Manufact..." and have a value "Manufacturing" 我想限制DropDownList1中显示的文本的长度,例如,名为“ Manufacturing”的元素将显示“ Manufact ...”并具有值“ Manufacturing”

How to do this? 这个怎么做?

Thanks to Mihai Caracostea I ended up with this... 多亏了Mihai Caracostea,我才结束了这个...

    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
        foreach (ListItem myItem in DropDownList1.Items)
        {
            try
            {
                if (myItem.Text.Length > 8)
                    myItem.Text = myItem.Text.Substring(0, 11) + "...";
            }
            catch (ArgumentOutOfRangeException ex) 
            { 
                //do nothing
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM