简体   繁体   中英

How to select DropDownList items in cascading design on page_load

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();
}

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];

我最终将第二个下拉列表绑定到第一个下拉列表的DataBind事件上。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM