Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
3.11/5 (3 votes)
See more:
I want to display Name and code in drop down My query is Select Temp_Name,Temp_Code from Temp_Shipper
Drop Down list is ddlshipper in which Shipper name will come from query and other one is textbox8 in which code will come.
Posted
Comments
TrushnaK 5-Dec-13 0:49am    
post,what you have done so far?
Karthik_Mahalingam 5-Dec-13 1:12am    
Please take your time and point out the need clearly..
add some code piece
glmjoy 5-Dec-13 7:38am    
Thanks to all

C#
DataTable shippers = new DataTable();

using (SqlConnection con = new SqlConnection(connectionString))
{

    try
    {
        SqlDataAdapter adapter = new SqlDataAdapter("Select Temp_Name,Temp_Code from Temp_Shipper", con);
        adapter.Fill(shippers);

        ddlshipper.DataSource = shippers;
        ddlshipper.DataTextField = "Temp_Name";
        ddlshipper.DataValueField = "Temp_Code";
        ddlshipper.DataBind();
    }
    catch (Exception ex)
    {
        // Handle the Exception
    }

}

// Add the initial item
ddlSubject.Items.Insert(0, new ListItem("<Select Shipper>", "0"));

Now handle SelectedIndexChanged Event of DropDown and assign the value to TextBox.
C#
void Shipper_Index_Changed(Object sender, EventArgs e) 
{
    textbox8.Text = ddlshipper.SelectedItem.Value;
}

Markup of DropDownList is like...
HTML
<asp:DropDownList id="ddlshipper" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Shipper_Index_Changed">
</asp:DropDownList>
 
Share this answer
 
v2
ASP
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>


In page load, after setting data source,

DropDownList1.SelectedItem.Text=name;
DropDownList1.SelectedItem.Value= code;

In DropDownList1_SelectedIndexChanged method,
You can set the textbox value to DropDownList1.SelectedItem.Value
 
Share this answer
 
hai
just use Solution 2 , in that concate the name and code like
C#
ddlshipper.DataSource = shippers;
        ddlshipper.DataTextField = "Temp_Name" + " " + "Temp_Code";
        ddlshipper.DataValueField = "Temp_Code";
        ddlshipper.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900