下拉选择值问题 [英] dropdown select value problem
问题描述
我在一个页面中有两个下拉列表。
当我从第一个下拉列表中选择一个值时,第二个下拉列表中的值来自第一个下拉列表中的选定值。
但问题是,当我点击提交按钮时,如果我从第二个下拉列表中选择了任何内容,它将获取第一个值并插入它。当我点击提交时,这就像..页面加载问题。
但是我无法理解实际上是什么问题。
这是我的代码。
第一个下拉列表设置为autopostback = true。
I have two dropdown list in one page.
when i select one value from first dropdown,value in second dropdown comes according to selected value in first dropdown.
But problem is ,when i click on submit button ,if i have select anything from second dropdown,it takes first value and insert it. this is somthing like..page load problem when i click on submit.
but i cant understand what is actually problem.
here is my code.
first dropdown is set to autopostback=true.
int Userid;
objdal.Deptid = Convert.ToInt16(ddlName.SelectedValue);
objdal.Name = ddlName.SelectedItem.Text;
dv = objdal.AssignTimetable_SelectDeptid(); //select userid from userinfo where deptid=@deptid AND name=@name
Userid = Convert.ToInt16(dv.Table.Rows[0]["Userid"].ToString());
这里userid总是从第二个下拉列表中获得第一个值。
here userid is always comming of first value from second dropdown.
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter ad = new SqlDataAdapter("select Fname,id from x",con);
DataTable dt = new DataTable();
ad.Fill(dt);
if(dt.Rows.Count>0)
{
uxCountry.DataSource=dt;
uxCountry.DataTextField = "Fname";
uxCountry.DataTextField = "id";
uxCountry.DataBind();
}
}
}
protected void uxCountry_SelectedIndexChanged(object sender, EventArgs e)
{
uxStates.Items.Clear();
SqlDataAdapter ad = new SqlDataAdapter("select Lname,yid from y where xid=" + uxCountry.Items[uxCountry.SelectedIndex].Value, con);
DataTable dt = new DataTable();
ad.Fill(dt);
if (dt.Rows.Count > 0)
{
uxStates.DataSource = dt;
uxStates.DataTextField = "Lname";
uxStates.DataTextField = "yid";
uxStates.DataBind();
}
}
我试过这个填写两个下拉国家及其州,它运作良好我回复提交时。
I tried this to filled the two dropdown Country and its states,it works well for me while postback on submit.
这篇关于下拉选择值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!