how to retrieve data from a sqldatasource with a selected value from a dropdownlist to a textbox RRS feed

  • Question

  • User-880522632 posted

    Hi, hope you can help me.

    I have a dropdowlist that obtain its values from a sqlDataSource with the following query:

    SELECT Row_Id, Concept, Price FROM Default_Values

    I would like to fill a textbox with the "Price" field from the query upon the selectedValue of the dropdowlist, all through the sqlDataSource proper functions.

    I don't want to call the same query in codebehind, just need to retrieve the thridsqlDataSource value into the textbox.

    Thanks in advance. Best regards.

    Friday, November 4, 2011 6:28 PM


  • User-1226263862 posted

    You don't want to receive the value from the datasource,  you want to retrieve the selected value in the dropdownlist.  So create a SelectedIndexChanged event for the dropdownlist and set AutoPostBack="true" on it.  Inside the SelectedIndexChanged event, set TextBox1.Text = DropDownList1.SelectedValue.ToString()  This will work as long as the dropdownlist's DataValue property is set to the Price field you mention.  Otherwise in the SelectedIndexChanged event you will have to run another query to get the values based on the selectedvalue of the ddl.  OR you could put another sql datasource on the page and configure the select parameter to use a control (dropdownlist).  The only problem with this approach is you cannot directly bind a textbox to a datasource, it has to be in a FormView, GridView, etc in order to work properly.

    I also suggest checking out some of the data access videos and how-to pages on this site as they have a lot of info.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 4, 2011 8:29 PM