Databind dropdownlist to textbox with label

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 17058 Views - Last Post: 06 June 2011 - 04:20 PM Rate Topic: -----

#1 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Databind dropdownlist to textbox with label

Posted 01 June 2011 - 11:49 AM

Hello Everyone!

I have a Dropdownlist & based on its selection I need to populate data in a textbox that has a label in it. So basically populate the label. This is my code:

Aspx Page: <asp:DropDownList ID="DDLTest" runat="server" AutoPostBack="true" onselectedIndexChanged="DDLTest_SelectedIndexChanged" Width="200px">
<asp:Label id="Label1" AssociatedControlId="textbox" 
    runat="server" /> <asp:TextBox id="textbox1" runat="server" />
code behind: 
 protected void DDLEmployeeNames2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDLTest.SelectedItem != null) 
                Textbox1.Text = DDLTest.SelectedItem.Text;
        }
       



When I execute the code, I get the default value in the drop down displaying in the Textbox which is "Select a Value" whereas the actual selected value doesn't display. Any ideas as to what is happening?

Is This A Good Question/Topic? 0
  • +

Replies To: Databind dropdownlist to textbox with label

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 01 June 2011 - 06:45 PM

that event isn't attached to that control

onselectedIndexChanged="DDLTest_SelectedIndexChanged"



but the event your showing is

protected void DDLEmployeeNames2_SelectedIndexChanged(object sender, EventArgs e)


Was This Post Helpful? 0
  • +
  • -

#3 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 01 June 2011 - 07:15 PM

I made an error while pasting the code, the event is supposed to be DDLTest instead of the EmployeeNames2. Any suggestions on the issue?
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 01 June 2011 - 08:26 PM

Where are you setting the values for the DropDownList at? I see it doesn't have a datasource attached to it so I'm guessing you're setting the values in the code behind. Can you also provide the code where you're setting the dropdown values as well? And during which page event do you set those values?

Also, have you tried running it in debug mode and stepping through the event to see what it's doing?

This post has been edited by Nakor: 01 June 2011 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 07:33 AM

I am setting it at Page Load.

This is my code behind:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DDLTest.DataBind();
                SqlCommand SQLSelectCommand;
                SqlDataReader dtrTestNames;

                string SQLDBConnString = ConfigurationManager.ConnectionStrings["PercutRS"].ToString();
                SqlConnection SQLDBConn = new SqlConnection(SQLDBConnString);

                SQLDBConn.Open();

                SQLSelectCommand = new SqlCommand("dbo.GetFullName", SQLDBConn);
                SQLSelectCommand.CommandType = CommandType.StoredProcedure;


                dtrTestNames = SQLSelectCommand.ExecuteReader();

                DDLTest.DataSource = dtrTestNames;

                DDLTest.DataValueField = "ID";
                DDLTest.DataTextField = "Name";

                DDLTest.DataBind();

                DDLTest.Items.Insert(0, new ListItem("Select A Value", "0"));

              

                SQLDBConn.Close();
            }


I have tried debugging it, it doesn't really do anything for the textboxes that i am trying to populate, as they still populate as blank. Hope you can help me out.
Was This Post Helpful? 0
  • +
  • -

#6 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 11:16 AM

Is that all the code that's in your selected index changed event? I'm trying to duplicate the problem you're having but everything is working like it should for me. This aspx page wasn't copied from another aspx page or anything like that was it?
Was This Post Helpful? 0
  • +
  • -

#7 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 11:33 AM

well the data is populated in my dropdownlist. But when i make a selection, it doesn't populate any data in the textbox. The only thing i have done different now, is tried to put it in an updatePanel, but that isn't helping as well. If it's working for you, what gets populated in the textbox once you make a selection in the dropdown?
Was This Post Helpful? 0
  • +
  • -

#8 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 02:09 PM

the displayed text in the drop down gets put into the textbox. If you try to set the text of the textbox to some generic value from the index changed event does that work or is it not hitting the event at all?

 protected void DDLEmployeeNames2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Textbox1.Text = "TESTING";
        }
       




Was This Post Helpful? 0
  • +
  • -

#9 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 02:27 PM

I tried doing that, and it doesn't do anything at all. So I guess it isn't hitting it at all. I am at loss for ideas. Do you think its because it's in an update panel maybe?
Was This Post Helpful? 0
  • +
  • -

#10 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 02:31 PM

no, you might try removing the control and adding it back to the page or removing the event from the control and then adding it back on.

This post has been edited by Nakor: 02 June 2011 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#11 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 02:37 PM

I removed it from the aspx page & tried addding that control again but still no luck. Do you see anything wrong with the control's code?:

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional"><ContentTemplate><asp:Label id="textboxTestLabel" runat="server" AssociatedControlId="textbox" 
    runat="server" /> <asp:TextBox id="textbox1" runat="server" /></ContentTemplate></asp:UpdatePanel></asp:TableCell>

Was This Post Helpful? 0
  • +
  • -

#12 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 02 June 2011 - 07:13 PM

There's nothing wrong there that I'm seeing as far as something that would affect your textbox value getting set. One thing I do see is that if you're wanting your drop down to asynchronously update the update panel (using ajax) then you'll need to add the drop down control as an async trigger to the update panel since it is not inside the update panel.

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>



Edit: just noticed your last code example ends with a <asp:TableCell> control. Is the drop down list in a different cell in the table or in the table at all? can you show the code for the entire page?

This post has been edited by Nakor: 02 June 2011 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#13 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 03 June 2011 - 06:26 AM

Yeah the dropdownlist is in a table cell, here's the code:

<asp:TableCell ID="TableCell2" runat="server">  <asp:UpdatePanel ID="Update1" runat="server" UpdateMode="Conditional"><ContentTemplate><asp:DropDownList ID="DDLTest" runat="server" AutoPostBack="true" onselectedIndexChanged="DDLTest_SelectedIndexChanged" Width="200px">
            </asp:DropDownList></ContentTemplate>
       </asp:UpdatePanel>

Was This Post Helpful? 0
  • +
  • -

#14 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Databind dropdownlist to textbox with label

Posted 03 June 2011 - 06:12 PM

You're problem is that you're drop down is in a different update panel from the textbox and both update panels UpdateMode's are set to "conditional". You can either remove the UpdateMode="Conditional" from the UpdatePanel's or you can add a trigger to the UpdatePanel that the TextBox control is in.

            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DDLTest_SelectedIndexChanged" EventName="SelectedIndexChanged" />
            </Triggers>



Note: Conditional UpdateMode means that the update panels update independently of each other.
Was This Post Helpful? 0
  • +
  • -

#15 staindromeo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 21-October 10

Re: Databind dropdownlist to textbox with label

Posted 04 June 2011 - 07:12 PM

Thanks, I will give this a try & let you know how it works out. Although the other option for UpdateMode is "Always" would that make a difference? I am going to try putting the dropdownlist & textbox in the same updatepanel & see how that works out.

Hope this works :dozingoff:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2