Solved

C#.NET - Set textbox = dropdownlist selecteditem

Posted on 2009-07-15
18
678 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
Comment Utility
txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedValue.ToString();
0
 

Author Comment

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

Open in new window

0
 

Author Comment

by:scross1276
Comment Utility
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
 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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
Comment Utility
oh you posted something....let me see that as well...
0
 

Author Comment

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

Expert Comment

by:guru_sami
Comment Utility
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
Comment Utility
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
Comment Utility
can you share your DDL code and the button click where you are trying to set the TB value
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:scross1276
Comment Utility
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
Comment Utility
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
Comment Utility
Nothing is there...
image.jpg
0
 
LVL 41

Expert Comment

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

Author Comment

by:scross1276
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The first one is null.  I'll try that now.
0
 

Author Comment

by:scross1276
Comment Utility
THAT WORKED!!!!!  Thank you so much!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now