Solved

C#.NET - Set textbox = dropdownlist selecteditem

Posted on 2009-07-15
18
682 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Title # Comments Views Activity
How can I do Unit Testing with Session ? 1 41
Web page design problem 3 41
Textbox autocomplete using jquery in asp.net 13 48
Ajax calendar distorted 4 17
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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