Steve
asked on
C#.NET - Set textbox = dropdownlist selecteditem
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;
txtApprovingSupervisor.Tex t = ddlApprovingSupervisor.Sel ectedValue .ToString( );
ASKER
That doesn't save it.
txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedValue.ToString();
ASKER
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();
}
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.
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.
oh you posted something....let me see that as well...
ASKER
It doesn't have to post immediately, it can do it onClick.
the code you posted is for DDL databinding....
for inital selection you can explicitly do...
ddlApprovingSupervisor.Dat aTextField = "displayName";
ddlApprovingSupervisor.Dat aBind();
---> ddlApprovingSupervisor.Sel ectedValue = whatever value you want to select intially Or
---> ddlApprovingSupervisor.Sel ectedIndex = 0 i.e. first item
sdrApprovingSupervisor.Clo se();
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....
for inital selection you can explicitly do...
ddlApprovingSupervisor.Dat
ddlApprovingSupervisor.Dat
---> ddlApprovingSupervisor.Sel
---> ddlApprovingSupervisor.Sel
sdrApprovingSupervisor.Clo
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....
ASKER
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.
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.
can you share your DDL code and the button click where you are trying to set the TB value
ASKER
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(Configuratio nManager.C onnectionS trings["** *"].Connec tionString );
myConnection.Open();
string sqlApprovingSupervisor = "select displayName, email from *** order by displayName";
SqlCommand cmdApprovingSupervisor = new SqlCommand(sqlApprovingSup ervisor, myConnection);
SqlDataReader sdrApprovingSupervisor = cmdApprovingSupervisor.Exe cuteReader ();
ddlApprovingSupervisor.Dat aSource = sdrApprovingSupervisor;
//ddlApprovingSupervisor.D ataValueFi eld = "displayName";
ddlApprovingSupervisor.Dat aTextField = "displayName";
ddlApprovingSupervisor.Dat aBind();
sdrApprovingSupervisor.Clo se();
myConnection.Close();
}
protected void btnSave_Click(Object sender, EventArgs e)
{
txtApprovingSupervisor.Tex t = ddlApprovingSupervisor.Sel ectedItem. Text;
string strInsertReveal;
SqlConnection myConnection = new SqlConnection(Configuratio nManager.C onnectionS trings["** *"].Connec tionString );
myConnection.Open();
INSERT statements below this point.
{
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(Configuratio
myConnection.Open();
string sqlApprovingSupervisor = "select displayName, email from *** order by displayName";
SqlCommand cmdApprovingSupervisor = new SqlCommand(sqlApprovingSup
SqlDataReader sdrApprovingSupervisor = cmdApprovingSupervisor.Exe
ddlApprovingSupervisor.Dat
//ddlApprovingSupervisor.D
ddlApprovingSupervisor.Dat
ddlApprovingSupervisor.Dat
sdrApprovingSupervisor.Clo
myConnection.Close();
}
protected void btnSave_Click(Object sender, EventArgs e)
{
txtApprovingSupervisor.Tex
string strInsertReveal;
SqlConnection myConnection = new SqlConnection(Configuratio
myConnection.Open();
INSERT statements below this point.
to me its looks it should work...
did you try setting breakpoint at this line:
---> txtApprovingSupervisor.Tex t = ddlApprovingSupervisor.Sel ectedItem. Text;
mouseover SelectedItem.Text and see the value for Text.
did you try setting breakpoint at this line:
---> txtApprovingSupervisor.Tex
mouseover SelectedItem.Text and see the value for Text.
ASKER
Nothing is there...
image.jpg
image.jpg
now thats insane....
the last thing I would like to see is your DDL markup from your .aspx page.
the last thing I would like to see is your DDL markup from your .aspx page.
ASKER
You bet...
<asp:DropDownList ID="ddlApprovingSupervisor" runat="server" Width="250px" />
<asp:TextBox ID="txtApprovingSupervisor" runat="server" />
wonderful....that leaves me without a clue....
Let me see if something else strikes....
Let me see if something else strikes....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The first one is null. I'll try that now.
ASKER
THAT WORKED!!!!! Thank you so much!!