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]

Tuesday, 24 August 2010

Sunday, 25 April 2010

Multiple controls with the same ID '***' were found

I have come across this issue a few times. I have experienced two causes:

  1. You have two or more controls on the page. - Sorry for stating the obvious.
    • This could mean that you have two user controls that each contain a control with the same ID. 
  2. The Page_Load event is getting called twice.
    • If you have migrated to a newer version of .Net, you may have experienced that Visual Studio has very kindly added a second call to the event handler - so look for this and get rid!
    • Set the AutoEventWireup to false.
      • (msdn) the ASP.NET page framework also supports an automatic way to associate page events and methods. If the AutoEventWireup attribute of the Page directive is set to true (or if it is missing, since by default it is true), the page framework calls page events automatically, specifically the Page_Init and Page_Load methods. In that case, no explicit Handles clause or delegate is needed.