[RESOLVED] listbox with optgroup

is there way to do listbox with optgroup?
i tried just making a header row with different styles but it is still selectable. enabled for that row makes it invisible.
i dont know of a way to use just a plain <select with runat=server and populate the items from code behind.

I did find someones dll that has this ability in their user control but i get a security trust fail when trying to run it. I cannot figure out how to tell the web.config that the dll is safe to run.

 

Doug 

http://weblogs.asp.net/jeff/archive/2006/12/27/dropdownlist-with-optgroup.aspx

 

Check Comment of -  Ben Sloan

The answer is not to use attributes as you suggest, but instead to set the "value" of the listitem which is to be the group header. Set it to something very unlikely to be used again like "$$GROUPHEADER$$GROUPHEADER$$"

Your test against the attribute (to see whether it is null) then becomes: "If item.Value = "$$GROUPHEADER$$GROUPHEADER$$" Then"

Next is to make it a bit easier for the programmer to add groups without having to remember this value string. To do this, I created a class called "OptionGroup" (I'm sure you can work out the contents of it) and then added a public sub on the control you wrote:

       Public Sub AddOptionGroup(ByVal og As OptionGroup)

           Dim optGrpTitle As New ListItem(og.GroupName, "$$GROUPHEADER$$GROUPHEADER$$")

           Me.Items.Add(optGrpTitle)

           For Each li As ListItem In og.Items

               Me.Items.Add(New ListItem(li.Text, li.Value))

           Next

       End Sub

To then add groups in an .aspx page:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       If Not IsPostBack Then

           Dim G1Items As New ListItemCollection

           G1Items.Add(New ListItem("G1V1", "G1V1"))

           G1Items.Add(New ListItem("G1V2", "G1V2"))

           G1Items.Add(New ListItem("G1V3", "G1V3"))

           Dim G2Items As New ListItemCollection

           G2Items.Add(New ListItem("G2V1", "G2V1"))

           G2Items.Add(New ListItem("G2V2", "G2V2"))

           G2Items.Add(New ListItem("G2V3", "G2V3"))

           Dim og1 As New OptionGroup("Group1", G1Items)

           Dim og2 As New OptionGroup("Group2", G2Items)

           Me.ddlTest.AddOptionGroup(og1)

           Me.ddlTest.AddOptionGroup(og2)

       End If

   End Sub

Except for that the code remains the same, and works perfectly across postbacks.

This is because the value property of the items does persist in viewstate, I suppose.

thx for that suggestion. I finally found out my problems with adding in someones dll. I had to change the app pool to run under NetworkService :) 

If you want to work w/ optgroup -- you need to use CustomValidator for Validation

Request.Form to get that value on server end

And throw JS from codebehind to update that control

Something along this line

<select name="opt1" id="opt1" >
        <option selected="selected" value="1">Select One</option>
        <optgroup label="aaaa">
        <option value="2">z1</option>
        <option value="3">z2</option>
        <option value="4">Z3</option></optgroup>
        <optgroup label="bbbb">
        <option value="5">X1</option>
        <option value="6">X2</option>
        </optgroup>
       
    </select>


//To get value at server end
int y1  = Int32.Parse(Request.Form["opt1"].ToString());


//To update that control from codebehind
ScriptManager.RegisterStartupScript(this, this.GetType(), "starScript", "Updateopt('" + x1 + "');", true);

thoughts on "[RESOLVED] listbox with optgroup "

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>