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>
<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;
}
}