[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Dropdownlist populates Text Box in ASP.net

I have a query that populates a drop down list. It has two fields NAME | TERRITORY

Textbox is blank. When end user selects the drop down list then the TERRITORY is populated in the textbox.

How can I do this in ASP.NET

Here is my code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name" 
            Height="26px" Width="127px">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ordersConnectionString %>" 
            SelectCommand="SELECT [name], [territory] FROM [names]"></asp:SqlDataSource>
    
    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

Open in new window

0
jseaman12
Asked:
jseaman12
  • 2
1 Solution
 
varungdCommented:
Set the  'TERRITORY' as the valuefield of  that dropdownlist and use below given code
HTML
 
  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name" 
            Height="26px" Width="127px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
           
        </asp:DropDownList>
 
 
 
ASPX.CS
 
 
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
         TextBox1.Text=  DropDownList1.SelectedValue;
        }

Open in new window

0
 
varungdCommented:
One more thing is in this HTML code u should set the 'DataValueField=TERRITORY' .
 
 
 
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now