简体   繁体   中英

Set label text based on dropdown list selection from datasource

I have not been able to find a solution to what would seem to be a simple common goal - I want to select a item number from a drop down list and set an adjacent label1.text to display the associated item name. The item number and item name are returned from a object datasource query and dataset that fills the dropdown list. The dropdown list DataTextField and DataValuefield values are set to the item number as the item number is what is being displayed and what is used to supply a parameter to a stored procedure call to fill the data on the form.

All the examples I could find either used a second query to the database with the dropdown selected item number as a parameter to a query to get the associated item name and fill the label1.text, or set the label1.text to the drop down selected value (in this case item number not item name). I would hope to be able to accomplish this without a second query to the database since the item name value is already in the dataset table that is used to fill the dropdown list item numbers.

I expect somehow I would have to get the dropdown selected item and use that to pull the associated name value from the dataset. But how?

Code extract follows:

...<asp:DropDownList ID="itemno" runat="server" DataSourceID="ObjectDatasource4" DataTextField="itemno" DataValueField="itemno" AppendDataBoundItems="true" OnSelectedIndexChanged="itemno_selectedchanged" Width="65" TabIndex="4">
                        <asp:ListItem Value="ALL" Selected="True">ALL</asp:ListItem>
                    </asp:DropDownList><asp:Label ID="Label1" runat="server" />...

and ...<asp:ObjectDataSource ID="ObjectDataSource4" runat="server" SelectMethod="GetData" TypeName="DepositLookup.App_Code.Dataset1TableAdapters._itemnoTableAdapter">
            <SelectParameters>
                <asp:SessionParameter SessionField="accesslevel" DefaultValue="" Name="usraccess" Type="String" />
            </SelectParameters>...

Sets ItemName to ThirdValue property in ListItem

<asp:DropDownList ID="ddlDropDown" runat="server">
    <asp:ListItem Text="ItemID" Value="ItemID" ThirdValue="ItemName" />
</asp:DropDownList>

Gets value of ItemName

ListItem item = ddlDropDown.Items.FindByValue("ItemID");
string itemName = item.Attributes["ThirdValue"];

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