0

我的下拉列表中只有“是”和“否”作为我的项目列表,但如果我选择“否”3 次,那么它会在我的下拉列表中显示 3 次,因此它会复制我从下拉列表中所做的每个选择。在选择之后,它调用存储过程,根据选择将后端更新为是或否。我在这里很困惑,所以请帮忙。谢谢这是我的 DD 的 ASPX 代码

<asp:DropDownList ID="My_DD" runat="server" 
              AutoPostBack="true" DataTextField="ItemCompleted" AppendDataBoundItems="true" OnSelectedIndexChanged="DD_Changed" 
              DataValueField="ItemCompleted">
              <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
                        <asp:ListItem Text="No" Value="No"></asp:ListItem>
          </asp:DropDownList>

这是后面的代码:

protected void DD_Changed(object sender, EventArgs e)
    {
        DropDownList My_DD = (DropDownList)sender;

        this.Bind_DD();

    }   

    protected void Bind_DD()
    {

        string myVar;
        myVar= My_DD.SelectedValue.ToString();
        string ID;
        ID = Request.QueryString["ID"];
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand("sp_DD");
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@myVAr", SqlDbType.VarChar).Value = myVar;
        cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
        cmd.Connection = con;
        sda.SelectCommand = cmd;

        sda.Fill(dt);
        My_DD.DataSource = dt;

        My_DD.DataBind();

    }

页面加载代码在这里:

 protected void Page_Load(object sender, EventArgs e)
    {
        string Post_ID = Page.RouteData.Values["Post_ID"] as string;

        if (!IsPostBack)
        {
            BindData();
            BindData_ActionItems();

            //load DV_New
            LoadDetailView_New();       

        }

    }
4

1 回答 1

1

据我了解,代码会发生什么:您单击是/否,然后使用来自 db 的值重新绑定同一下拉列表中的数据。我没有收到错误,但您可以在重新添加数据库中的项目之前尝试重新清除下拉列表

protected void Bind_DD()
{

    string myVar;
    myVar= My_DD.SelectedValue.ToString();
    string ID;
    ID = Request.QueryString["ID"];
    DataTable dt = new DataTable();
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlDataAdapter sda = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand("sp_DD");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@My_DD", SqlDbType.VarChar).Value = My_DD;
    cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
    cmd.Connection = con;
    sda.SelectCommand = cmd;

    sda.Fill(dt);

    My_DD.Items.Clear();

    My_DD.DataSource = dt;
    My_DD.DataBind();

}
于 2013-08-14T20:33:00.800 回答