【问题标题】:Edit button not working properly after adding 'Add' button in the gridview footer在 gridview 页脚中添加“添加”按钮后,编辑按钮无法正常工作
【发布时间】:2017-11-19 04:23:13
【问题描述】:

我刚刚在我的网格视图中添加了一个“添加”按钮,之前有一个包含更新按钮的编辑,但是在添加了“添加”按钮之后,该按钮与网格视图中添加的记录查看完美配合,并且在数据库中,“编辑”按钮似乎不再起作用。这是按钮的脚本。

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"   

    OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Height="226px" Width="1172px" ShowFooter="True"  HorizontalAlign="Center">

              <Columns> 
                <asp:TemplateField>  
                <ItemTemplate>  
                    <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" Font-Size="Medium" Height="32px" Width="68px" />          
                   </ItemTemplate> 
                <EditItemTemplate>  
                    <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                    <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                </EditItemTemplate> 
                <FooterTemplate>
                    <asp:Button ID="newAddBtn" runat="server"  
  OnClick="NewAddBtn_Click" Text="Add"  CommandName="Insert" Font-Size="Medium" Height="32px" Width="64px"/>
                        </FooterTemplate>
                </asp:TemplateField>


            <asp:TemplateField HeaderText="Class ID">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_CID" runat="server" 
     Text='<%#Eval("CID") %>'></asp:Label>  

                </ItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="Class Name">  
                <FooterTemplate>
                    <asp:DropDownList ID="newName" runat="server">
                        <asp:ListItem>Select</asp:ListItem>
                        <asp:ListItem>Power Yoga</asp:ListItem>
                        <asp:ListItem>Pilates</asp:ListItem>
                        <asp:ListItem>Cardio Peak</asp:ListItem>
                        <asp:ListItem>Body Attack</asp:ListItem>
                        <asp:ListItem>Corb</asp:ListItem>
                        <asp:ListItem>Boxing</asp:ListItem>
                        <asp:ListItem>Zumba</asp:ListItem>
                        <asp:ListItem>Weights</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="NameErr" runat="server" 
  ControlToValidate="newName" ErrorMessage="*" ForeColor="Red">
       </asp:RequiredFieldValidator>
                </FooterTemplate>
                <ItemTemplate>  
                    <asp:Label ID="lbl_cName" runat="server" Text='<%#Eval("CName") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="cname_txt" runat="server" Text='<%#Eval("CName") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="Class Start Time">  
                <FooterTemplate>
                    <asp:TextBox ID="newStart" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="startErr" runat="server" ControlToValidate="newStart" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </FooterTemplate>
                <ItemTemplate>  
                    <asp:Label ID="lbl_cstart" runat="server" Text='<%#Eval("CStart") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="cstart_txt" runat="server" Text='<%#Eval("cStart") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField> 

                  <asp:TemplateField HeaderText="Class Finish Time">  
                      <FooterTemplate>
                          <asp:TextBox ID="newEnd" runat="server"></asp:TextBox>
                          <asp:RequiredFieldValidator ID="endErr" runat="server" ControlToValidate="newEnd" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                      </FooterTemplate>
                <ItemTemplate>  
                    <asp:Label ID="lbl_cend" runat="server" Text='<%#Eval("cend") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="cend_txt" runat="server" Text='<%#Eval("cend") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>             
        </Columns>  
              <FooterStyle BackColor="#663300" />
        <HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>  
        <RowStyle BackColor="#e7ceb6"/>  
    </asp:GridView>

这是打开数据库连接后“添加”按钮后面的代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowData();
        }
    }
    //ShowData method for Displaying Data in Gridview  
    protected void ShowData()
    {
        dt = new DataTable();
        con = new SqlConnection(cs);
        con.Open();

        // adapt = new SqlDataAdapter("Select * from customers", con);

        adapt = new SqlDataAdapter("Select * from classes", con);

        adapt.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        con.Close();
    }

        Control control = null;
        if (GridView1.FooterRow != null)
        {
            control = GridView1.FooterRow;
        }
        else
        {
            control = GridView1.Controls[0].Controls[0];
        }

       string className = ((DropDownList)GridView1.FooterRow.FindControl("newName")).SelectedItem.Text;

       // string className = (GridView1.FooterRow.FindControl("newName") as TextBox).Text;
        string startTime = (GridView1.FooterRow.FindControl("newStart") as TextBox).Text;
        string endTime = (GridView1.FooterRow.FindControl("newEnd") as TextBox).Text;
        //  string strConnString = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
        //  using (SqlConnection con = new SqlConnection(strConnString))


                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "INSERT INTO classes VALUES(@cName, @cStart, @cEnd)";
                cmd.Parameters.AddWithValue("@cName", className);
                cmd.Parameters.AddWithValue("@cStart", startTime);
                cmd.Parameters.AddWithValue("@cEnd", endTime);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


     //   Response.Redirect(Request.Url.AbsoluteUri);
    }

【问题讨论】:

    标签: c# html asp.net gridview


    【解决方案1】:

    您需要确保在添加操作结束时,您的 gridview 是数据绑定的,如下面的代码 sn-p 所示,即在您的 C# 代码中的 con.Close(); 代码行之后。

    我假设您有获取 gridviewdata 的方法。

    GridView1.DataSource =  GetGridData();
    GridView1.DataBind();
    

    此外,请确保您的页脚中的所有验证器都有一个 ValidationGroup="Add"else,当您单击网格行中的编辑按钮时,它将防止由于页脚验证器而导致页面回发。此外,还要为 Add 按钮添加 ValidationGroup="Add"。

    <FooterTemplate>
         <asp:TextBox ID="newEnd" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="endErr" runat="server" ValidationGroup="Add" 
           ControlToValidate="newEnd" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
    </FooterTemplate>
    

    【讨论】:

    • 编辑按钮仍然无法正常工作,当我点击它时没有任何结果就像点击空白一样
    • 是的,我做了,它添加了记录,但编辑按钮仍然不起作用
    • 你能粘贴完整的网格视图 html 吗?好像少了点什么。确保您订阅了 gridview 的 RowUpdating 事件。
    • 请看我的更新问题,它现在反映了一切。 @Sunil
    • 还显示 RowUpdating 和 RowEditing 事件的代码。那里肯定有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2011-04-24
    相关资源
    最近更新 更多