Showing posts with label saved. Show all posts
Showing posts with label saved. Show all posts

Wednesday, March 21, 2012

Problem loading saved data back into dropdownlist

I'm completely new to ASP.NET and Ajax, having quickly inherited a project from a consultant who is no longer with us.

Have a form with C# codebehind, and all this is running inside some Ajax components (tabcontainer and update panels). One dropdownlist (SystemID) determines the filtered values in a second dropdownlist (TypeID). In the page, there's the following to determine the source for each list:

<asp:SqlDataSourceID="sqlSystem"runat="server"

SelectCommand="Select Distinct '0' As SystemID, '(Select)' As Description From CMSSystem UNION

Select SystemID,Description From CMSSystem Where Active='1'">

</asp:SqlDataSource>

<asp:SqlDataSourceID="sqlType"runat="server"

SelectCommand="Select Distinct '0' As TypeID, '(Select)' As Description From CMSType UNION

Select TypeID, Description from CMSType Where SystemID = @dotnet.itags.org.SystemID">

<SelectParameters>

<asp:ControlParameterControlID="cboSystem"Name="SystemID"PropertyName="SelectedValue"/>

</SelectParameters>

</asp:SqlDataSource>

When I complete an empty form, the linking between the lists is fine. Select a value from SystemID, then I can select a value from TypeID that has been filtered.

The problem is on trying to load existing data. I have a procedure call inside Page_Load that loads existing values onto the form. Text boxes and checkboxes are just fine, and standalone dropdownlists are fine. I can load SystemID from a dataset row, but when I try to do the same for TypeID (trying to lookup the saved value in the list of filtered items), I find that the items collection is completely empty - and therefore, I get a null selectedvalue. What I'm guessing is that the SqlDataSource isn't "firing" (?) for the TypeID for some reason at the time of Page_Load, so I have no data to bind to the control.

Is that a good assumption, and if so, how can I get around this? I've been floundering around for a couple of weeks with this, and need any assistance I can get.

How about using the Page's Prerender event for that, instead of Load? Alternatively, you could handle theSqlDataSource's Selected event.


Didn't see any change with Page_PreRender instead of Page_Load.

I have this hunch that the SqlDataSource isn't being re-evaluated when I manually set the first dropdownlist's SelectedIndex (based on the previously saved, now retrieved, data). It must be a matter of timing or something. Trying to execute this codebehind:

if (dsRequestDetails.Tables[0].Rows[0]["SystemID"].ToString() !="")

{

liFind = cboSystem.Items.FindByValue(dsRequestDetails.Tables[0].Rows[0]["SystemID"].ToString());

cboSystem.SelectedIndex = cboSystem.Items.IndexOf(liFind);

}

if (dsRequestDetails.Tables[0].Rows[0]["TypeID"].ToString() !="")

{

liFind = cboType.Items.FindByValue(dsRequestDetails.Tables[0].Rows[0]["TypeID"].ToString());

cboType.SelectedIndex = cboType.Items.IndexOf(liFind);

}


populate the second dropdown at the ondatabound event of the first drop down. This will solve your problem