Asp.net下拉列表总是返回一个值

时间:2015-04-27 06:07:43

标签: c# asp.net

我在Department表中有两行。

ID  Name  
1   Commerce Department 
3   Human Resource Department 

无论我选择什么,它都会返回 ID=1 。 正确地从数据库中提取和显示数据。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        EmployeeLogic MLEdit = new EmployeeLogic();

        DepartmentLogic DL = new DepartmentLogic();
        DepartmentDropDownList.DataSource = DL.SelectAll();
        DepartmentDropDownList.DataTextField = "DepartmentName";
        DepartmentDropDownList.DataValueField = "DepartmentID";
        DepartmentDropDownList.DataBind(); 
        Employee empp = MLEdit.SelectByID(Convert.ToInt32(Request.QueryString["EID"]));
        MName.Text = empp.EmployeeName;
        DepartmentDropDownList.SelectedValue =empp.DepartmentID.ToString();
    }
}

protected void submit_Click(object sender, EventArgs e)
{
    if (Request.QueryString["EID"] != null)
    {
        EmployeeLogic MLUpdate = new EmployeeLogic();
        Employee emp = MLUpdate.SelectByID(Convert.ToInt32(Request.QueryString["EID"]));
        emp.EmployeeName = MName.Text;
        emp.DepartmentID = Convert.ToInt32(DepartmentDropDownList.SelectedItem.Value);
        emp.EmployeeID = Convert.ToInt32(Request.QueryString["EID"]);
        MLUpdate.Update(emp);
    }
}
<div class="form-group">
    <label for="exampleInputEmail1">Department</label>
    <asp:DropDownList ID="DepartmentDropDownList" runat="server"  OnSelectedIndexChanged="DepartmentDropDownList_SelectedIndexChanged1"> 
    </asp:DropDownList>
</div>

2 个答案:

答案 0 :(得分:0)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        EmployeeLogic MLEdit = new EmployeeLogic();

        DepartmentLogic DL = new DepartmentLogic();
        DepartmentDropDownList.DataSource = DL.SelectAll();//This is the data source for DepartmentDropDownList
        DepartmentDropDownList.DataTextField = "DepartmentName";//This will appear at user side
        DepartmentDropDownList.DataValueField = "DepartmentID";//this the id of selected item 
        DepartmentDropDownList.DataBind();//Finally this will bind the dropdown with source. 
 DepartmentDropDownList.Items.Insert(0, new ListItem("0", "Select Item")); 
        Employee empp = MLEdit.SelectByID(Convert.ToInt32(Request.QueryString["EID"]));
        MName.Text = empp.EmployeeName;
        DepartmentDropDownList.SelectedValue =empp.DepartmentID.ToString();
    }
}

protected void submit_Click(object sender, EventArgs e)
{
    if (Request.QueryString["EID"] != null)
    {
        EmployeeLogic MLUpdate = new EmployeeLogic();
        Employee emp = MLUpdate.SelectByID(Convert.ToInt32(Request.QueryString["EID"]));
        emp.EmployeeName = MName.Text;
        emp.DepartmentID = Convert.ToInt32(DepartmentDropDownList.SelectedItem.Value);
        emp.EmployeeID = Convert.ToInt32(Request.QueryString["EID"]);
        MLUpdate.Update(emp);
    }
}
<div class="form-group">
    <label for="exampleInputEmail1">Department</label>
    <asp:DropDownList ID="DepartmentDropDownList" runat="server"  OnSelectedIndexChanged="DepartmentDropDownList_SelectedIndexChanged1"> 
    </asp:DropDownList>
</div>

答案 1 :(得分:0)

解决。 在索引更改事件中,我将更改的值存储在会话对象&amp;提交时,从会话中存储在db中。 感谢。