Hi there,
I'm net to C#, .net, visual studio, so please excuse me if my questions are a little stupid.
I am trying to create a webform and am confused as to how the data is collected from a Dropdown list.
The code I have is;
protected void Page_Load(object sender, EventArgs e)
{
string strConn = "Data Source=test-sql-01;Initial Catalog=IEG4; User ID=; Password=";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter daStatus = new SqlDataAdapter("select [Status] from [Council Tax Status]", conn);
DataSet dsStatus = new DataSet();
daStatus.Fill(dsStatus);
lstStatus.DataSource = dsStatus;
lstStatus.DataTextField = "Status";
lstStatus.DataBind();
}
protected void lstStatus_SelectedIndexChanged(object sender, EventArgs e)
{
lblStatus.Text = lstStatus.SelectedValue;
}
When the value selected in the Dropdown box is changed the text value of the label does not change
Can anyone point me in the right direction?
Thanks
Loading
Rajendra DeopuraPosted Oct 16, 2008, 10:22 AM
Please write the following code in appropriate handlers :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindGrid();
}
public void BindGrid()
{
string constr=@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=true;User Instance=true";
string cmdstr = "select * from statustable";
SqlDataAdapter daStatus=new SqlDataAdapter(cmdstr,constr);
DataSet dsStatus=new DataSet();
daStatus.Fill(dsStatus,"table1");
DropDownList1.DataSource=dsStatus;
DropDownList1.DataTextField="Status";
DropDownList1.DataValueField="Status";
DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lstStatus.Text = DropDownList1.Text;
}
Points to note:
1. Set the AutoPostback property of DropDownlist box to true.
2. Here, lstStatus is the name of the label control.
3. DropDownList1 is the name of the dropdownlist box.
4. Database.mdf is the name of the database file with a table named StatusTable.
If you want the zipped project, mail me at [email protected]
Happy Coding.
Jonathan SheedPosted Oct 16, 2008, 10:36 AM
Got it working now.
Cheers