Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a ddl
ASP.NET
<asp:DropDownList ID="ddlSUser" runat="server" CssClass="input" Width="145px"></asp:DropDownList>

and Add Button Code
C#
protected void btnAddb_Click(object sender, EventArgs e)
        {
            Form_Enable();
            using (SqlConnection conn = new SqlConnection(conStr))
            {   // Creating insert statement
                string sql = string.Format(@"SELECT UID, UFName FROM UserInfo");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                ddlSUser.DataSource = cmd.ExecuteReader();
                ddlSUser.DataTextField = "UFName";
                ddlSUser.DataValueField="UID";
                ddlSUser.DataBind();                 
                conn.Close();
                cmd = null;
            }
            ddlSUser.Items.Insert(0, new ListItem("--- Select User --", "0"));
            BUID.Text = ddlSUser.DataValueField;            
        }

i want to show UID in the textbox
ASP.NET
<td><asp:TextBox ID="BUID" runat="server" CssClass="input"></asp:TextBox></td>

Need your help
Posted
Updated 11-Aug-15 21:35pm
v2

<asp:dropdownlist id="ddlSUser" runat="server" cssclass="input" width="145px" onselectedindexchanged="ddlSUser_SelectedIndexChanged" autopostback="True" >
C#
protected void ddlSUser_SelectedIndexChanged(object sender, EventArgs e)
{
   BUID.Text = ddlUser.SelectedValue;
//or
BUID.Text = ddlUser.SelectedItem.Value;
//or
BUID.Text = int.Parse(ddlUser.SelectedItem.(Value or Text));
}
 
Share this answer
 
v2
in your designer view of asp.net page, select your dropdown list and doubleclick to add selected index changed event, or press F4 while you selecting control and go to events and add the on selected index changed event. inside that event you can write code to set text box text from selected dropdownlist item value. make sure you have added AutoPostBack="True" property as below to execute event.
ASP.NET
<asp:DropDownList ID="ddlSUser" runat="server" CssClass="input" Width="145px" AutoPostBack="True" onselectedindexchanged="itemSelected">
    </asp:DropDownList>
 
Share this answer
 
Hi Altaful,

The SelectedIndexChanged events of DropDownLists can be used here. I have tried modifying your code.

Please see the below code:
ASP.NET
<asp:dropdownlist id="ddlSUser" runat="server" cssclass="input" width="145px" onselectedindexchanged="ddlSUser_SelectedIndexChanged" xmlns:asp="#unknown"></asp:dropdownlist>


And in the code behind write the event like this:

C#
protected void ddlSUser_SelectedIndexChanged(object sender, EventArgs e)
{
   BUID.Text = Convert.ToString(ddlSUser.SelectedValue);
}


I hope this helps :)
 
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