【问题标题】:ASP.Net Gridview RowCommand Edit, UpdateASP.Net Gridview RowCommand 编辑、更新
【发布时间】:2020-01-13 05:43:43
【问题描述】:

我正在使用 webforms 上的 asp.net gridview 并使用 GridView OnRowCommand 事件的 Gridview RowCommand 方法在 TemplateField 中进行按钮查看、编辑和更新。

if (e.CommandName == "EditContract") 
  {
            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            int rowIndex = ((GridViewRow)((Button)e.CommandSource).NamingContainer).RowIndex;
            gvContract.SelectRow(rowIndex);

            using (SqlCommand cmd = new SqlCommand("spContractEdit", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@RoleName", SqlDbType.Char).Value = Session["RoleName"];
                cmd.Parameters.Add("@UnitName", SqlDbType.Char).Value = Session["UnitName"];
                cmd.Parameters.Add("@ContrSerialNo", SqlDbType.Int).Value = SerialNo;

                dAdapter = new SqlDataAdapter(cmd);

                DataTable DtContract = new DataTable();
                dAdapter.Fill(DtContract);
     }

if (e.CommandName == "UpdateContract") 
           {

            lblMessage.Text = "";
            lblFile.Text = "";

            GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;

            using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;

             }
          } 

第一次加载页面时,此代码运行良好,但之后出现 2 个问题。即 1. 当页面第一次加载时它正在编辑和更新数据,但是当我尝试编辑同一行或任何其他行时它不会。我知道这是因为我在 Page_Load 事件上定义了 !Page.IsPostback 方法,但我不知道如何解决这种情况。 2.问题是如何限制gridview行只更新被选中行的数据?

请给我一个解决方案。

Gridview Sample

【问题讨论】:

    标签: asp.net gridview webforms rowcommand


    【解决方案1】:

    首先,你应该在UpdateContract中绑定Gridview。如下所示

    if (e.CommandName == "UpdateContract") {
    
                lblMessage.Text = "";
                lblFile.Text = "";
    
                GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
                int SerialNo = (int)gvContract.DataKeys[row.RowIndex].Value;
    
                using (SqlCommand cmd = new SqlCommand("spContractUpdate", myObj.DbConnect()))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
    
    ***You need to bind Gridview here then it will not have to go for postback every time ***
                    Gridview.datasource= datatable;
                    Gridview.Databind();
    }
    

    您可以使用 updatepanel 仅更新需要的元素并避免刷新。

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>
    
    
        ***Here put your gridview***
    
       <asp:gridview></asp:gridview>
       </ContentTemplate>
       </asp:UpdatePanel>
    

    【讨论】:

    • 亲爱的@Priom Sarkar,感谢您的帮助。我已经在每个命令上绑定了 gridview,即使用数据表查看、编辑和更新。此外,我正在使用母版页,我已将您的代码放在我的应用程序的母版页上,但现在更新不起作用..
    • 您在母版页上使用 gridview 吗?
    • 亲爱的@Priom 再次感谢您的回复。不,我在内容页面上使用 gridview。我将 scriptmanager 放在母版页上,将 updatepanel+contenttemplate 放在内容模板内的另一个页面上。
    • 将 scriptmanager 放在内容页面上。你在哪里使用 updatepanel+contenttemplate
    猜你喜欢
    • 2013-01-16
    • 2014-10-25
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2018-05-01
    相关资源
    最近更新 更多