Question

I'm trying to assign the selected value in the drop down to text box when ever I hit a button. I'm not able to see the selected value in the TextBox. Can someone please let me know what am I missing?

My code is as follows:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {                       

    }

    protected void Button1_Click1(object sender, EventArgs e)
    {

        string strConn = "Initial Catalog=NavigateV6;Data Source=SVUSRYE-SQL3D1;User ID=dopis_user;Password=dopis_password;Persist Security Info=True;MultipleActiveResultSets=True";
        SqlConnection mycn = new SqlConnection(strConn);
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("Select accountid FROM trn_account ", mycn);
        myda.Fill(ds);
        DDL.DataSource = ds;
        DDL.DataTextField = "AccountID";
        DDL.DataValueField = "AccountID";
        DDL.DataBind();

    }



    protected void DDL_SelectedIndexChanged(Object sender, EventArgs e)
    {
        var selectedValue = ((DropDownList)sender).SelectedValue;
        if (!string.IsNullOrEmpty(TextBox1.Text))
            TextBox1.Text = selectedValue;

    }
}

ASPX:

<asp:DropDownList ID="DDL" runat="server" AutoPostBack="True" OnSelectedIndexChanged = " DDL_SelectedIndexChanged" >

    </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
        Text="sumbit" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
</asp:Content>

No correct solution

OTHER TIPS

jQuery can do that in a snap without a postback:

$('#<%= ButtonID.ClientID %>').click(function() {
    $('#<%= TextBoxID.ClientID %>').val($('#<%= DDL_ID.ClientID %>').val());
    return false;
});

In fact you don't even need to click a button, it can be done when the user selects a new value in the ddl:

$('#<%= DDL_ID.ClientID %>').change(function() {
    $('#<%= TextBoxID.ClientID %>').val($(this).val());
});

Your problem is because when when you check (!string.IsNullOrEmpty(TextBox1.Text)), it always return false because at first stage, value of TextBox is empty and hence never execute inner code of if condition.....If you need to go through above condition, you can put some value in TextBox at the time of declaration.......

Or,

Simply do like this in SelectedIndexChanged of DropDownList

 var selectedValue = ((DropDownList)sender).SelectedValue;
 TextBox1.Text = selectedValue;

Here is working example as like your code:

ASPX:

<body>
  <form runat="server" id="form1">
        <asp:DropDownList ID="DDL" runat="server" AutoPostBack="True" 
        OnSelectedIndexChanged = "DDL_SelectedIndexChanged" >
      <asp:ListItem Value="One">One</asp:ListItem>
      <asp:ListItem Value="Two">Two</asp:ListItem>
    </asp:DropDownList>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" 
        Text="sumbit" onclick="Button1_Click" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
  </form>
</body>

Code Behind:

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedValue = ((DropDownList)sender).SelectedValue;
            TextBox1.Text = selectedValue;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top