4

is there way thats i can preselect an item when the page loads or posts to the server..

this is what i have right now..

<%=Html.DropDownList("dllMonths", 
    new SelectList(new List<string>() {
       "", "January", "Feburary", "March", "April", "June", 
       "July", "August", "September", "October", "November", 
       "December"}), 
    new { onchange="this.form.submit();" })%>

1 Answer 1

12

Set the SelectedValue property of the SelectList, or pass it as second parameter to SelectList constructor.

<% = Html.DropDownList ( "dllMonths", 
                           new SelectList ( new List ( ) 
                                          { "", "January", "Feburary", "March", 
                                            "April", "June", "July", "August", 
                                            "September", "October", "November", 
                                            "December" },
                                          "April" ), 
                           new { onchange = "this.form.submit();" } 
                       )%>
Sign up to request clarification or add additional context in comments.

4 Comments

I would go a little further and say that you should use an overload with the selected item argument.
i think that was what i said :)
He gave two choices; I'm saying the first is really the only acceptable one. But I did up-vote since he got it right.
yea, actually i would agree on that. It will probably save a function call as well i would think

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.