Solved

C#.NET - Set textbox = dropdownlist selecteditem

Posted on 2009-07-15
18
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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