views:

726

answers:

2

I have 2 dropdownlists on my aspx page, second is filtered by the selection in the first list.

How do I pre-select the two dropdown lists with the data saved in the database for the record when the page loads, it does not work when I bind data on the page on Page_Load.

I am using ObjectDataSource to bind the two dropdownlists.

<asp:DropDownList  ID="ddlStatus" runat="server" Enabled="False"  
 onselectedindexchanged="ddlStatus_SelectedIndexChanged" 
 AutoPostBack="True" Width="100px" DataSourceID="ObjectDataSource1" 
 DataTextField="Status_Desc" DataValueField="Status_Id" />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
 OldValuesParameterFormatString="original_{0}" SelectMethod="GetStatusAll" 
 TypeName="MyDALTableAdapters.StatusTableAdapter"></asp:ObjectDataSource>    

<asp:DropDownList ID="ddlSubStatus" runat="server" Enabled="False" 
 EnableViewState="False" Width="230px" DataSourceID="ObjectDataSource2" 
 DataTextField="Sub_Status_Desc" DataValueField="Sub_Status_Id" />

<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" 
OldValuesParameterFormatString="original_{0}" SelectMethod="GetSubStatusData"
TypeName="MyDALTableAdapters.MyStatusSubGetAllTableAdapter">
<SelectParameters>
 <asp:ControlParameter ControlID="ddlStatus" DefaultValue="-1" Name="StatusId"
 PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
      // How to I select the values stored for this record?
    }
}

protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
    ObjectDataSource2.DataBind();
}
A: 

it should just be a case of setting the SelectedIndex property on each DropDownList control

ddlStatus.SelectedIndex = [your selected index];
ddlSubStatus.SelectedIndex = [your selected index];
Russ Cam
I tried that, problem is that the second list populates based on the selection in the first list. So, the items in the second list are not yet there.
Picflight
setting ddlSubStatus.SelectedIndex should happen outside of "if (!Page.IsPostBack)"
Russ Cam
A: 

I ended up binding the second dropdownlist on the DataBind event of the first dropdownlist.

Picflight