Solved

C#.NET - Set textbox = dropdownlist selecteditem

Posted on 2009-07-15
18
679 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Achieve json result 2 65
Getting multiple values from a dataset ASP.NET(VB) 3 37
Syntax error 9 45
IIS issues - Convert to Application - APS.NET v4.0 3 19
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

18 Experts available now in Live!

Get 1:1 Help Now