0

I have this code below:

<select size="1" name="sel1">
    <option value="c1">Choice 1</option>
    <option value="c2">Choice 2</option>
    <option value="c3">Choice 3</option>
    <option value="c4">Choice 4</option>
</select>

Then what I want to happen, is to hide for example the 1st option in a method (for example below) in the .aspx.cs file. How to do that?

protected void RptFaqsAnswer_ItemDataBound(object sender, RepeaterItemEventArgs e){
     // codes here...
}

Thanks

RJ

user1120260
  • 375
  • 4
  • 12
  • 29

1 Answers1

1

You have to use runat=server to make the code-behind aware of the HTML markup. There are two approaches you could take.

Approach #1 Make the select an HTML server control.

This will hide the option on the client side (ie, it still renders the HTML, just tells the browser not to display it).

<select runat="server" id="myselect">
    <option value="c1">Choice 1</option>
    ...
</select>

Then you can modify the options however you like, eg:

myselect.Items[0].Attributes.Add("display", "none");

Approach #2 Make the option elements server controls.

This will not render the option to the client at all.

<select>
    <option value="c1" runat="server" id="option1">Choice 1</option>
</select>

And set Visible to false in the code behind:

option1.Visible = false;
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • I need to add my code in a method with this parameter (object sender, RepeaterItemEventArgs e) in the method body, i need to get the id of that option. var optionID = e.Item.FindControl("option1").ClientID; but my problem now, I cannot find the optionID.Visible attribute. I guess i still need to parse the optionID object to "option" object. how to do that? thanks – user1120260 Aug 30 '12 at 01:28
  • @user1120260 that's a completely different question -- I'd suggest creating a new question or revising this one. The short answer, though, is that you need to cast it as an *HtmlGenericControl*: **(e.Item.FindControl("option1") as HtmlGenericControl).Visible = false**. (**Note** for this to work, you'll also have to add **runat="server" to the *option* tag.) – McGarnagle Aug 30 '12 at 01:31
  • What if take the Approach #1. var optionID = e.Item.FindControl("myselect").ClientID as HtmlGenericControl will still work? I tried this one, but its not working. – user1120260 Aug 30 '12 at 01:37
  • @user1120260 You could -- but it should be like this: **(e.Item.FindControl("myselect") as HtmlSelect).Items[0].Attributes.Add("display", "none");** – McGarnagle Aug 30 '12 at 01:44
  • @user1120260 Actually, I'm not sure this will work. Is *myselect* a child of the repeater? The ID might not be "myselect", it could be something else. – McGarnagle Aug 30 '12 at 01:45
  • yes, it's a child of the repeater... i tried to use this one but I am getting an error.... this is the code that I have... (e.Item.FindControl("myselect").ClientID as HtmlSelect).Items[3].Attributes.Add("display", "none"); – user1120260 Aug 30 '12 at 01:53