Monday, April 13, 2009

Bind your DropDownList item to TextBox



Default.aspx

<body>
<form id="form1" runat="server">
<div>
<div>Bind Your Selected DropDownlist to Textbox.</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [PlaceCode], [PlaceName] FROM [Place]"></asp:SqlDataSource>

<asp:DropDownList ID="mydropdown" runat="server" AutoPostBack="True" OnSelectedIndexChanged="mydropdown_SelectedIndexChanged" DataSourceID="SqlDataSource1" DataTextField="PlaceName" DataValueField="PlaceCode">
</asp:DropDownList>
<asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>

</div>
</form>
</body>

p/s:Remeber set your dropdownlist autopostback to true

Default.aspx.cs

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

protected void mydropdown_SelectedIndexChanged(object sender, EventArgs e)
{
myTextBox.Text = mydropdown.SelectedItem.Text;
}
}

No comments:

Post a Comment