ASP.net下拉列表不返回正确的索引

时间:2018-04-17 09:15:52

标签: c# asp.net

我正在尝试将数据插入SQL Server数据库。但我依赖下拉列表中的选定项来插入相应的ID。当我运行代码它运行正常但我总是得到相同的索引0.如何在下拉列表中获得所选项目的正确索引。 这是插入按钮的代码:

protected void btnInsert_Click(object sender, EventArgs e)
    {
        int x= DropDownList2.SelectedIndex;
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = @"INSERT INTO RatingsAndComments (PID,RaterEmail,RaterName,Rating,Comment) VALUES (@PID,@email,@name,@rating,@comment) ";
        cmd.Parameters.AddWithValue("@PID", ids[x]);
        cmd.Parameters.AddWithValue("@email", txtmail.Text);
        cmd.Parameters.AddWithValue("@name", name.Text);
        cmd.Parameters.AddWithValue("@rating", txtrating.Text);
        cmd.Parameters.AddWithValue("@comment", comment.Text);
        cmd.ExecuteNonQuery();

        con.Close();

    }

这是下拉列表的asp代码

<asp:DropDownList ID="DropDownList2" runat="server"  Width="130" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
            </asp:DropDownList>

这是下拉列表中显示的数据:

protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            string FetchData = "Select * from Physicians";
            cmd = new SqlCommand(FetchData, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();
            sda.Fill(dt);
            ids = new string[dt.Rows.Count];
            names = new string[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ids[i] = dt.Rows[i][0].ToString();
                names[i] = dt.Rows[i][1].ToString();
            }
            DropDownList2.DataSource = names;
            DropDownList2.DataBind();
            con.Close();
        }

2 个答案:

答案 0 :(得分:3)

问题的根源是page_load事件。你不会检查是否有回发。在这种情况下,像按钮一样控制按钮自然点击页​​面将重新填充您的组合框,索引将始终为0。

试试这个:

protected void Page_Load(object sender, EventArgs e)
    {

       if (!IsPostBack)
      {
        con.Open();
        string FetchData = "Select * from Physicians";
        cmd = new SqlCommand(FetchData, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();
        sda.Fill(dt);
        ids = new string[dt.Rows.Count];
        names = new string[dt.Rows.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ids[i] = dt.Rows[i][0].ToString();
            names[i] = dt.Rows[i][1].ToString();
        }
        DropDownList2.DataSource = names;
        DropDownList2.DataBind();
        con.Close();
     }
}

另外,你可以配置你的下拉列表,如thi:

 <asp:DropDownList ID="DropDownList2" runat="server"  Width="130" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" DataTextField="PhysicianID_Field_In_Your_Physicians_Table" DataValueField="Physician_Name_Field_In_Your_Physicians_Table">
        </asp:DropDownList>

您可以获得下拉列表的选定值,如:

 cmd.Parameters.AddWithValue("@PID", DropDownList2.SelectedValue);

答案 1 :(得分:0)

试试这个

DropDownList2.DataSource = names;
DropDownListMonth.DataTextField = i.ToString();
DropDownListMonth.DataValueField = i.ToString();
DropDownList2.DataBind();