Pages

Thursday, 26 August 2010

Dynamically Altering a DropDownList in JavaScript

Using JavaScript to dynamically alter can sometimes be tricky. In 99% of cases *1* will not be an option. This hugely compromises security.
In the web.config
*2* maybe an option, but we need to know the ListItems before we put them into the DropDownList; this is not always possible
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
  base.Render(writer);
  Page.ClientScript.RegisterForEventValidation( cboLookup.ID);
}
*3*, I think is the simplest way, but it does stray from the .Net way of doing things; ah well! Instead of using
<asp:DropDownList runat="server" blah blah blah />
use
<select id="ddl1" runat="server" enableviewstate="true"></select>
You may find that you cannot access the 'selectedindex' is not accessible in your 'button_Click' event (or whatever). In order to get your selected value, simply use:
Request.Form[ddl1.UniqueID]

No comments:

Post a Comment