Solved

C#.NET - Set textbox = dropdownlist selecteditem

Posted on 2009-07-15
18
681 Views
Last Modified: 2012-05-07
How do I set a textbox control equal to the value a user selects in a dropdownlist on the same page?
txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedItem.Text;

Open in new window

0
Comment
Question by:scross1276
  • 9
  • 9
18 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24863808
txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedValue.ToString();
0
 

Author Comment

by:scross1276
ID: 24863990
That doesn't save it.
txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedValue.ToString();

Open in new window

0
 

Author Comment

by:scross1276
ID: 24864035
Here's more of the code...
public void approvingSupervisor()
        {
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["***"].ConnectionString);
 
            myConnection.Open();
 
            string sqlApprovingSupervisor = "select displayName, email from *** order by displayName";
            SqlCommand cmdApprovingSupervisor = new SqlCommand(sqlApprovingSupervisor, myConnection);
            SqlDataReader sdrApprovingSupervisor = cmdApprovingSupervisor.ExecuteReader();
 
            ddlApprovingSupervisor.DataSource = sdrApprovingSupervisor;
            ddlApprovingSupervisor.DataValueField = "displayName";
            ddlApprovingSupervisor.DataTextField = "displayName";
            ddlApprovingSupervisor.DataBind();
 
            sdrApprovingSupervisor.Close();
 
            myConnection.Close();
        }

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 41

Expert Comment

by:guru_sami
ID: 24864057
are you getting any error?
Do you want as soon as dropdownlist selection changes?
if so then set AutoPostBack="true" for your DDL...
Or
share your dropdownList code and more info on your requirement.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24864061
oh you posted something....let me see that as well...
0
 

Author Comment

by:scross1276
ID: 24864085
It doesn't have to post immediately, it can do it onClick.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24864137
the code you posted is for DDL databinding....
for inital selection you can explicitly do...

ddlApprovingSupervisor.DataTextField = "displayName";
            ddlApprovingSupervisor.DataBind();
---> ddlApprovingSupervisor.SelectedValue = whatever value you want to select intially Or
---> ddlApprovingSupervisor.SelectedIndex = 0 i.e. first item
            sdrApprovingSupervisor.Close();

Also when are you trying to populate the TextBox...after DDL DataBind or before that...
So provide the DDL markup and the place where you are trying to populate the TB
Or I am misunderstanding your requirement....
0
 

Author Comment

by:scross1276
ID: 24864228
I'd say you understand it perfectly.

I've tried populating the textbox from immediately after calling approvingSupervisor() in the page_load, and putting it in a button click event that takes users to the next page.  It doesn't populate the textbox at either time.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24864289
can you share your DDL code and the button click where you are trying to set the TB value
0
 

Author Comment

by:scross1276
ID: 24864319
protected void Page_Load(object sender, EventArgs e)
        {
            DateTime dtmDate;

            dtmDate = System.DateTime.Now;
            String dtmString;
            dtmString = dtmDate.ToString();
            lblRequestDate.Text = dtmString;
            lblRequestor.Text = GetUserName();

            approvingSupervisor();
        }

public void approvingSupervisor()
        {
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["***"].ConnectionString);

            myConnection.Open();

            string sqlApprovingSupervisor = "select displayName, email from *** order by displayName";
            SqlCommand cmdApprovingSupervisor = new SqlCommand(sqlApprovingSupervisor, myConnection);
            SqlDataReader sdrApprovingSupervisor = cmdApprovingSupervisor.ExecuteReader();

            ddlApprovingSupervisor.DataSource = sdrApprovingSupervisor;
            //ddlApprovingSupervisor.DataValueField = "displayName";
            ddlApprovingSupervisor.DataTextField = "displayName";
            ddlApprovingSupervisor.DataBind();

            sdrApprovingSupervisor.Close();

            myConnection.Close();
        }

        protected void btnSave_Click(Object sender, EventArgs e)
        {
            txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedItem.Text;

            string strInsertReveal;
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["***"].ConnectionString);
            myConnection.Open();

INSERT statements below this point.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24864443
to me its looks it should work...
did you try setting breakpoint at this line:
---> txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedItem.Text;
mouseover SelectedItem.Text and see the value for Text.


0
 

Author Comment

by:scross1276
ID: 24864504
Nothing is there...
image.jpg
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24864597
now thats insane....
the last thing I would like to see is your DDL markup from your .aspx page.
0
 

Author Comment

by:scross1276
ID: 24864617
You bet...
<asp:DropDownList ID="ddlApprovingSupervisor" runat="server" Width="250px" />
 
<asp:TextBox ID="txtApprovingSupervisor" runat="server" />

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24864701
wonderful....that leaves me without a clue....
Let me see if something else strikes....
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24864780
one more question....by any chance is your first item value empty?
how about try putting your function call like this:

if (!Page.IsPostBack)
        {
            approvingSupervisor();
        }
0
 

Author Comment

by:scross1276
ID: 24864803
The first one is null.  I'll try that now.
0
 

Author Comment

by:scross1276
ID: 24864822
THAT WORKED!!!!!  Thank you so much!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question