Dropdownlist data bind
-
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
-
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
Hello Have you set the dropdownlist's autopostback property to true in the control's propertis Regards Aenon
-
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
-
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.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
-
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
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>
-
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>
thanks a lot, i tried it and it works fine thank u very much for your help TG