Kiss Koppány
Kiss Koppány

Reputation: 950

SelectValue on non-asp select list in ASP.NET C# (4.5)

I have the following html code (index.aspx):

<select class="ct-input ct-term" name="term">
   <option value="5">5</option>
   <option value="10">10</option>
</select>

I would like to select the proper item of the select list, regarding to querystring, from the code behind on the page load event. Is it possible? Querystring looks like: index.aspx?term=10 (10, so select the option with the value of 10).

I had the same issue with number input, but that one was easy, all I had to do was make it runat="server", then write name.Value = "something".

Unfortunately that won't work here, because my form doesn't have the attr runat="server", and I don't wanna add it, because then it would add a viewstate too and make the url unreadable. Is there any other solution? Ps.: form's method must be GET.

Upvotes: 0

Views: 102

Answers (1)

iceburg
iceburg

Reputation: 1768

You could set the correct item into ViewBag in your action and in the view set selected based on the value in ViewBag. eg:

public ActionResult (int term){
ViewBag.Term=term;
return View();
}

<select class="ct-input ct-term" name="term">
   <option value="5" <% if(ViewBag.Term==5){<text>selected</text>} %> >5</option>
   <option value="10" <% if(ViewBag.Term==10){<text>selected</text>} %> >10</option>
</select>

Upvotes: 1

Related Questions