Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dropdownlist data bind

Dropdownlist data bind

Scheduled Pinned Locked Moved ASP.NET
databasewpfwcftutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tigertwareg
    wrote on last edited by
    #1

    Hello, I have a dropdowlist that i populate from the database by binding data, when i click on the button to save the form the list is initialized. and i don't know how to save the value of the selected item, its lost after each click on the button. thanks in advance TG

    U M 2 Replies Last reply
    0
    • T tigertwareg

      Hello, I have a dropdowlist that i populate from the database by binding data, when i click on the button to save the form the list is initialized. and i don't know how to save the value of the selected item, its lost after each click on the button. thanks in advance TG

      U Offline
      U Offline
      User 1884058
      wrote on last edited by
      #2

      Hello Have you set the dropdownlist's autopostback property to true in the control's propertis Regards Aenon

      1 Reply Last reply
      0
      • T tigertwareg

        Hello, I have a dropdowlist that i populate from the database by binding data, when i click on the button to save the form the list is initialized. and i don't know how to save the value of the selected item, its lost after each click on the button. thanks in advance TG

        M Offline
        M Offline
        minhpc_bk
        wrote on last edited by
        #3

        Hi there, If you have ViewState enabled, then you only need to bind data to the dropdownlist control just for the first time. So if this is the case, you simply put your data binding code in the IsPostBack conditional statement.

        T 1 Reply Last reply
        0
        • M minhpc_bk

          Hi there, If you have ViewState enabled, then you only need to bind data to the dropdownlist control just for the first time. So if this is the case, you simply put your data binding code in the IsPostBack conditional statement.

          T Offline
          T Offline
          tigertwareg
          wrote on last edited by
          #4

          Hi, Thanks for answering me, in my form, i have : a dropdownlist that has autopostback to false and enableviewstate to true textboxfield and button. " i tried the autopostback to true but it doesn't work" i want to have the value of my dropdownlist after clicking the save button to make the save on the database. to populate the dropdownlist i have : myConnection1.Open() cmb.DataSource = myCommand1.ExecuteReader(CommandBehavior.CloseConnection) cmb.DataTextField = "label" cmb.DataValueField = "code" cmb.DataBind() if i don't bind data like that, it works fine, i mean if i populate the list manually or by the add method, i can have the value after clicking the save button. if u can help thanks a lot TG

          M 1 Reply Last reply
          0
          • T tigertwareg

            Hi, Thanks for answering me, in my form, i have : a dropdownlist that has autopostback to false and enableviewstate to true textboxfield and button. " i tried the autopostback to true but it doesn't work" i want to have the value of my dropdownlist after clicking the save button to make the save on the database. to populate the dropdownlist i have : myConnection1.Open() cmb.DataSource = myCommand1.ExecuteReader(CommandBehavior.CloseConnection) cmb.DataTextField = "label" cmb.DataValueField = "code" cmb.DataBind() if i don't bind data like that, it works fine, i mean if i populate the list manually or by the add method, i can have the value after clicking the save button. if u can help thanks a lot TG

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            Am I correct in thinking that in the event handler of the click event of the button you want to get the selected item of the dropdownlist control to persist in DB? The sample code below demonstrates that thing:

            <%@ Page language="c#" %>
            <%@ Import Namespace="System.Data" %>
            <%@ Import Namespace="System.Data.SqlClient" %>
            <HTML>
            <HEAD>
            <title>WebForm1</title>
            <script runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
            if(!IsPostBack)
            {
            string sqlText = "Select CategoryID, CategoryName from Categories";
            SqlConnection sqlConnection1 = new SqlConnection("server=localhost;uid=sa;pwd=;database=Northwind;Timeout=600;");
            SqlCommand command = new SqlCommand( sqlText, sqlConnection1);

            				sqlConnection1.Open();
            				DropDownList1.DataSource = command.ExecuteReader(CommandBehavior.CloseConnection);;
            				DropDownList1.DataValueField = "CategoryID";
            				DropDownList1.DataTextField = "CategoryName";
            				DropDownList1.DataBind();
            			}
            		}
                                               
            		private void btnSave\_Click(object sender, System.EventArgs e)
            		{
            			string selectedItemText = DropDownList1.SelectedItem.Text;
            			string selectedItemValue = DropDownList1.SelectedItem.Value;
            			
            			//You code here to persist data in DB.
            		}
            
            	</script>
            </HEAD>
            <body MS\_POSITIONING="GridLayout">
            	<form id="Form1" method="post" runat="server">
            		<asp:DropDownList id="DropDownList1" runat="server" ></asp:DropDownList>
            		<br>
            		<asp:TextBox Runat="server" ID="TextBox1"></asp:TextBox>
            		<br>
            		<asp:Button id="btnSave" OnClick="btnSave\_Click" runat="server"	Text="Save"></asp:Button>
            	</form>
            </body>
            

            </HTML>

            T 1 Reply Last reply
            0
            • M minhpc_bk

              Am I correct in thinking that in the event handler of the click event of the button you want to get the selected item of the dropdownlist control to persist in DB? The sample code below demonstrates that thing:

              <%@ Page language="c#" %>
              <%@ Import Namespace="System.Data" %>
              <%@ Import Namespace="System.Data.SqlClient" %>
              <HTML>
              <HEAD>
              <title>WebForm1</title>
              <script runat="server">
              private void Page_Load(object sender, System.EventArgs e)
              {
              if(!IsPostBack)
              {
              string sqlText = "Select CategoryID, CategoryName from Categories";
              SqlConnection sqlConnection1 = new SqlConnection("server=localhost;uid=sa;pwd=;database=Northwind;Timeout=600;");
              SqlCommand command = new SqlCommand( sqlText, sqlConnection1);

              				sqlConnection1.Open();
              				DropDownList1.DataSource = command.ExecuteReader(CommandBehavior.CloseConnection);;
              				DropDownList1.DataValueField = "CategoryID";
              				DropDownList1.DataTextField = "CategoryName";
              				DropDownList1.DataBind();
              			}
              		}
                                                 
              		private void btnSave\_Click(object sender, System.EventArgs e)
              		{
              			string selectedItemText = DropDownList1.SelectedItem.Text;
              			string selectedItemValue = DropDownList1.SelectedItem.Value;
              			
              			//You code here to persist data in DB.
              		}
              
              	</script>
              </HEAD>
              <body MS\_POSITIONING="GridLayout">
              	<form id="Form1" method="post" runat="server">
              		<asp:DropDownList id="DropDownList1" runat="server" ></asp:DropDownList>
              		<br>
              		<asp:TextBox Runat="server" ID="TextBox1"></asp:TextBox>
              		<br>
              		<asp:Button id="btnSave" OnClick="btnSave\_Click" runat="server"	Text="Save"></asp:Button>
              	</form>
              </body>
              

              </HTML>

              T Offline
              T Offline
              tigertwareg
              wrote on last edited by
              #6

              thanks a lot, i tried it and it works fine thank u very much for your help TG

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups