开发者

How to disable certains items in listbox control when binding

i have an datatable with valuees like this

names
------
kumar
kiran
ram
bala
----
anu
sita
geetha
-----
asha
a开发者_StackOverflow社区bc
tsdf
dsf
sdf
sdfrt
sdfsdfd
sdfdsfsdf
--------

i am binding my table to listview like this

ListItem ddlItem;
foreach (DataRow dr in dt.Rows)
{
ddlItem = new ListItem(dr["names"].ToString())
lbx.Items.Add(ddlItem);
}

here even i need to bind my datatable wih all the values except"-----" these values . it should be in disable mode even though user tries to selected it will not get selected how can we get it achived. any help on this would be great thank you


The standard listbox control does not have the ability to do this. You can set the Enabled property on the ListItem to False, but that removes the item from the list. The best solution is to use some JavaScript to change the selectedIndex of the listbox whenever one of the dashes is selected.

Add an OnClick handler to your listbox control:

<asp:ListBox ID="lbTesting" runat="server" OnClick="testSelectedItem();">

Then add the following JavaScript function to check for the selected item:

<script language="javascript" type="text/javascript">
    function testSelectedItem() {
        var lbTesting = document.getElementById('lbTesting');
        if (lbTesting[lbTesting.selectedIndex].innerText == '----') {
            lbTesting.selectedIndex = 0;
        }
    }
</script>

Obviously, you can play with the JavaScript function a bit if you need to test for different numbers of dashes, or if you want different functionality when a user selects a dashed entry - currently it just resets to the first item in the list.

Finally, you should add an edit on the server side to ensure that a dashed entry was not selected, as the user might have JavaScript disabled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜