添加'添加'添加后,编辑按钮无法正常工作。 gridview页脚中的按钮

时间:2017-11-19 04:23:13

标签: c# html asp.net gridview

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

        <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>

以下是&#34;添加&#34;背后的代码。按钮,在连接打开到数据库之后。

    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);
    }

1 个答案:

答案 0 :(得分:1)

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

我假设你有一个获取gridviewdata的方法。

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

此外,请确保页脚中的所有验证程序都具有ValidationGroup =&#34;添加&#34;否则当您单击网格行中的“编辑”按钮时,它将阻止页脚回发,因为页脚验证程序。另外,添加ValidationGroup =&#34; Add&#34;对于“添加”按钮也是。

<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>