Tuesday, March 27, 2012

Datasource to Populate DDL - then choose item

I've got a page with a SQLDataSource control successfully populating a Dropdownlist...

However, now I find I need extended functionality - so I've got another page with links to this page, using querystrings - I need to hit the page, check the querystring, and if it's blank, just continue, but if the querystring is populated, then choose that item.

I've tried the page_load and the page_prerender, but so far, it's not working:
CC = Request.QueryString("center")
If CC <>""Then
' DropDownList1.Items.FindByText(CC).Selected = True
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(CC))
End if

I've double checked, and yes, every time CC is populated, that item is definitely in the dropdownlist - it just doesn't get changed to that item.

ideas?

EndIf

Here's an example for you:

ASPX

<asp:dropdownlist id="ddlPets" runat="server" ondatabound="ddlPets_DataBound"></asp:dropdownlist><br /><br /><asp:hyperlink id="hypDog" runat="server" navigateurl="1131233.aspx?pet=Dog" text="Pre-select Dog" /><br /><asp:hyperlink id="hypCat" runat="server" navigateurl="1131233.aspx?pet=Cat" text="Pre-select Cat" /><br /><asp:hyperlink id="hypGoldfish" runat="server" navigateurl="1131233.aspx?pet=Goldfish"text="Pre-select Goldfish" /><br />

CODE-BEHIND

protected void Page_Load(object sender, EventArgs e){if (!this.IsPostBack){string[] pets = {"Dog","Cat","Goldfish" };ddlPets.DataSource = pets;ddlPets.DataBind();}}protected void ddlPets_DataBound(object sender, EventArgs e){if (this.Request["Pet"] ==null) {return; }ddlPets.SelectedIndex = ddlPets.Items.IndexOf(ddlPets.Items.FindByValue(this.Request["Pet"]));}
|||

using the databound even worked

sql