gridview 上的下拉列表仅更新数据库的第一个值 ASP.Net

发布于 2024-10-07 04:14:00 字数 2260 浏览 0 评论 0原文

我已经寻找这个问题的解决方案很多天了,请帮忙。 我有一个网格视图,上面有五个下拉列表。我在每一行都有编辑更新和取消编辑按钮。 下拉列表正在更新数据库,但即使用户从下拉列表中选择第二个、第三个或任何其他值,也仅更新下拉列表中的第一个值。

这可能是什么原因。这是我的行编辑/取消编辑和更新事件。

    protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCustomers.EditIndex = e.NewEditIndex;
       BindData();
    }

    protected void gvCustomers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCustomers.EditIndex = -1;
        BindData();
    }

    protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {           
        Label lblCustId = (Label)gvCustomers.Rows[e.RowIndex].FindControl("cust_id");
        DropDownList ddlCsm = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("CSM");
        DropDownList ddlSrCsm = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("SrCSM");
        DropDownList ddlDE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("DE");
        DropDownList ddlAE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("AE");
        DropDownList ddlTE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("TE");

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                string sql = "Update SOME_TABLE SET CSM= @CSM, SrCSM= @SrCSM ,DE=@DE,AE=@AE,TE=@TE where cust_id=@cust_id";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add("@CSM", SqlDbType.VarChar).Value = ddlCsm.SelectedItem.Value;

                    cmd.Parameters.Add("@SrCSM", SqlDbType.VarChar).Value = ddlSrCsm.SelectedItem.Value;

                    cmd.Parameters.Add("@AE", SqlDbType.VarChar).Value = ddlAE.SelectedItem.Value;

                    cmd.Parameters.Add("@DE", SqlDbType.VarChar).Value = ddlDE.SelectedItem.Value;

                    cmd.Parameters.Add("@TE", SqlDbType.VarChar).Value = ddlTE.SelectedItem.Value;

                    cmd.ExecuteNonQuery();
                }                 
            }
            catch
            { }
            gvCustomers.EditIndex = -1;
            BindData();
        }        
    }

任何帮助将不胜感激。 谢谢

I am looking for the solution to this problem from many days, please help.
I have a grid view and have five dropdownlist on it. I have edit update and cancel edit buttons on each row.
Drop down list is updating the database, but only first value in the dropdownlist even when user select second, third or any other value from the dropdownlist.

What could possible be the reason for this. Here is my row Edit/ CancelEdit and Updating Events.

    protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCustomers.EditIndex = e.NewEditIndex;
       BindData();
    }

    protected void gvCustomers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCustomers.EditIndex = -1;
        BindData();
    }

    protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {           
        Label lblCustId = (Label)gvCustomers.Rows[e.RowIndex].FindControl("cust_id");
        DropDownList ddlCsm = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("CSM");
        DropDownList ddlSrCsm = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("SrCSM");
        DropDownList ddlDE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("DE");
        DropDownList ddlAE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("AE");
        DropDownList ddlTE = (DropDownList)gvCustomers.Rows[e.RowIndex].FindControl("TE");

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                string sql = "Update SOME_TABLE SET CSM= @CSM, SrCSM= @SrCSM ,DE=@DE,AE=@AE,TE=@TE where cust_id=@cust_id";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.Add("@CSM", SqlDbType.VarChar).Value = ddlCsm.SelectedItem.Value;

                    cmd.Parameters.Add("@SrCSM", SqlDbType.VarChar).Value = ddlSrCsm.SelectedItem.Value;

                    cmd.Parameters.Add("@AE", SqlDbType.VarChar).Value = ddlAE.SelectedItem.Value;

                    cmd.Parameters.Add("@DE", SqlDbType.VarChar).Value = ddlDE.SelectedItem.Value;

                    cmd.Parameters.Add("@TE", SqlDbType.VarChar).Value = ddlTE.SelectedItem.Value;

                    cmd.ExecuteNonQuery();
                }                 
            }
            catch
            { }
            gvCustomers.EditIndex = -1;
            BindData();
        }        
    }

Any help would be greatly appeciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2024-10-14 04:14:00

只需单击编辑按钮填充下拉列表,然后在 rowupdating 事件上使用并使用 ddl.SelectedValue 而不是 ddl.SelectedItem.Value。您将找到选定的值。

just fill the dropdowns on the click of edit button and then use on rowupdating event and use ddl.SelectedValue instead of ddl.SelectedItem.Value. You will find the selected values.

九八野马 2024-10-14 04:14:00

您的下拉菜单可能会在 Page_Load 事件中重新设置。如果是,请在页面未处于回发状态时加载它们,如下所示:

if(!Page.IsPostBack) {
//set drop downs
}

Your Drop Downs are probably getting re-set in your Page_Load event. If they are, load them when the page is not in postback, like so:

if(!Page.IsPostBack) {
//set drop downs
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文