GridView控件的“编辑”,“删除”不可用的问题

来源:互联网 发布:裤子品牌 知乎 编辑:程序博客网 时间:2024/06/06 14:27

公司网站,前一个工程师留下一个奇怪的问题,一直没有解决,博客上说到的办法也没能解决问题,最终自己找出了小bug,做个记录,也供大家参考!

原因:页面中可能含有<input>标签,使GridView变成文本数据,失去“编辑”,“删除”的功能(猜测,原因其实到底是什么并不能确定,但这样做问题解决了,欢迎知道原因的前辈指出原因)。

解决办法:将<input>的功能用<asp:Button>代替。

 

错误案例:

Shopping.aspx

                            <asp:GridView ID="gvqy" runat="server" AutoGenerateColumns="False" DataKeyNames="id"

                                Style="margin: auto;" OnRowCancelingEdit="gvqy_RowCancelingEdit" OnRowEditing="gvqy_RowEditing"

                                OnRowUpdating="gvqy_RowUpdating" 

                                Width="99.9%" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="Both">

                                <AlternatingRowStyle BackColor="White" />

                                <Columns>

                                    <asp:BoundField DataField="id" HeaderText="编号" SortExpression="id" ReadOnly="True" ItemStyle-Width="7%" />

                                    <asp:BoundField DataField="proname" HeaderText="产品名称" SortExpression="id" ReadOnly="True" ItemStyle-Width="36%" />

                                    <asp:BoundField DataField="permoney" HeaderText="单价(¥)" SortExpression="id" ReadOnly="True" ItemStyle-Width="14%" />

                                    <asp:TemplateField HeaderText="数量" SortExpression="id" ItemStyle-Width="7%">

                                        <ItemTemplate>

                                            <asp:Label ID="lblqypx" runat="server" Text='<%# Bind("nums")%>'></asp:Label>

                                        </ItemTemplate>

                                        <EditItemTemplate>

                                            <asp:TextBox ID="txtNums" runat="server" Text='<%# Bind("nums")%>' Width="50px"></asp:TextBox>

                                            <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtNums"

                                                Display="Dynamic" ErrorMessage="Please enter an integer!" Operator="DataTypeCheck" Type="Integer"

                                                ValidationGroup="nums">Please enter an integer!</asp:CompareValidator>

                                        </EditItemTemplate>

                                        <HeaderStyle BorderColor="#CBD8AC" CssClass="gvheaderbg" />

                                        <ItemStyle BorderColor="#CBD8AC" />

                                    </asp:TemplateField>

                                    <asp:CommandField HeaderText="编辑" ShowEditButton="True" ValidationGroup="nums" ItemStyle-Width="15%">

                                        <HeaderStyle BorderColor="#CBD8AC" CssClass="gvheaderbg"  />

                                        <ItemStyle BorderColor="#CBD8AC"  />

                                    </asp:CommandField>

                                    <asp:TemplateField HeaderText="删除" ItemStyle-Width="8%">

                                        <ItemTemplate>

                                            <asp:LinkButton ID="button1" runat="server" CommandArgument='<%#Eval("id")%>' OnClientClick="return confirm('确定要删除吗?')"

                                                Text="删除" OnClick="buttondel_Click" ValidationGroup="nums" />

                                        </ItemTemplate>

                                        <HeaderStyle BorderColor="#CBD8AC" CssClass="gvheaderbg" />

                                        <ItemStyle BorderColor="#CBD8AC" />

                                    </asp:TemplateField>

                                </Columns>

                                <FooterStyle BackColor="#3C86C5" Font-Bold="True" ForeColor="White" />

                                <HeaderStyle BackColor="#3C86C5" Font-Bold="True" ForeColor="White" />

                                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

                                <RowStyle BackColor="#EFF3FB" />

                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

                            </asp:GridView>

                        <div>

                            <table border="0" cellspacing="3" cellpadding="0" width="70%" style="text-align:center;">

                             <tr>

    <tdcolspan="2" style="text-align:right;">

                                        <input class='MyBt' type='submit' name='submit' value='创建订单 ' />

                                    </td>

                             </tr>

                            </table>

 

Shopping.aspx.cs

        //处于编辑状态

        protected void gvqy_RowEditing(object sender,GridViewEditEventArgs e)

        {

            gvqy.EditIndex = e.NewEditIndex;

            BindGridView();

        }

 

        //取消编辑

        protected void gvqy_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)

        {

            gvqy.EditIndex = -1;

            BindGridView();

        }

 

        //执行编辑

        protected void gvqy_RowUpdating(object sender,GridViewUpdateEventArgs e)

        {

            int qyid = gvqy.DataKeys[e.RowIndex][0].GetString().GetInt();

            TextBox txtNums = gvqy.Rows[e.RowIndex].FindControl("txtNums")as TextBox;

 

            BLL.TcOrder bbk = new BLL.TcOrder();

            Model.TcOrder mbk = new Model.TcOrder();

            mbk = bbk.GetModel(qyid);

            mbk.Nums = txtNums.Text.GetInt();

            bbk.UpdateNum(mbk);

            gvqy.EditIndex = -1;

            BindGridView();

        }

 

        //删除

        protected void buttondel_Click(object sender,EventArgs e)

        {

            LinkButton lb = sender as LinkButton;

            int id = lb.CommandArgument.Trim().GetInt();

 

            int[] ids = { 0 };

            if (!ids.Contains(id))

            {

                BLL.TcOrder bbk = new BLL.TcOrder();

                bbk.Delete(id);

                BindGridView();

            }

        }

 

 

正确代码:

Shopping.aspx

                            <asp:GridView ID="gvqy" runat="server" AutoGenerateColumns="False" DataKeyNames="id"

                                Style="margin: auto;" OnRowCancelingEdit="gvqy_RowCancelingEdit" OnRowEditing="gvqy_RowEditing"

                                OnRowUpdating="gvqy_RowUpdating" 

                                Width="99.9%" CellPadding="4" EnableModelValidation="True" ForeColor="#333333" GridLines="Both">

                                <AlternatingRowStyle BackColor="White" />

                                <Columns>

                                    <asp:BoundField DataField="id" HeaderText="编号" SortExpression="id" ReadOnly="True" ItemStyle-Width="7%" />

                                    <asp:BoundField DataField="proname" HeaderText="产品名称" SortExpression="id" ReadOnly="True" ItemStyle-Width="36%" />

                                    <asp:BoundField DataField="permoney" HeaderText="单价(¥)" SortExpression="id" ReadOnly="True" ItemStyle-Width="14%" />

                                    <asp:TemplateField HeaderText="数量" SortExpression="id" ItemStyle-Width="7%">

                                        <ItemTemplate>

                                            <asp:Label ID="lblqypx" runat="server" Text='<%# Bind("nums")%>'></asp:Label>

                                        </ItemTemplate>

                                        <EditItemTemplate>

                                            <asp:TextBox ID="txtNums" runat="server" Text='<%# Bind("nums")%>' Width="50px"></asp:TextBox>

                                            <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtNums"

                                                Display="Dynamic" ErrorMessage="Please enter an integer!" Operator="DataTypeCheck" Type="Integer"

                                                ValidationGroup="nums">Please enter an integer!</asp:CompareValidator>

                                        </EditItemTemplate>

                                        <HeaderStyle BorderColor="#CBD8AC" CssClass="gvheaderbg" />

                                        <ItemStyle BorderColor="#CBD8AC" />

                                    </asp:TemplateField>

                                    <asp:CommandField HeaderText="编辑" ShowEditButton="True" ValidationGroup="nums" ItemStyle-Width="15%">

                                        <HeaderStyle BorderColor="#CBD8AC" CssClass="gvheaderbg"  />

                                        <ItemStyle BorderColor="#CBD8AC"  />

                                    </asp:CommandField>

                                    <asp:TemplateField HeaderText="删除" ItemStyle-Width="8%">

                                        <ItemTemplate>

                                            <asp:LinkButton ID="button1" runat="server" CommandArgument='<%#Eval("id")%>' OnClientClick="return confirm('确定要删除吗?')"

                                                Text="删除" OnClick="buttondel_Click" ValidationGroup="nums" />

                                        </ItemTemplate>

                                        <HeaderStyle BorderColor="#CBD8AC" CssClass="gvheaderbg" />

                                        <ItemStyle BorderColor="#CBD8AC" />

                                    </asp:TemplateField>

                                </Columns>

                                <FooterStyle BackColor="#3C86C5" Font-Bold="True" ForeColor="White" />

                                <HeaderStyle BackColor="#3C86C5" Font-Bold="True" ForeColor="White" />

                                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

                                <RowStyle BackColor="#EFF3FB" />

                                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

                            </asp:GridView>

                        <div>

                            <table border="0" cellspacing="3" cellpadding="0" width="70%" style="text-align:center;">

                             <tr>

    <tdcolspan="2" style="text-align:right;">

                                        <asp:Button ID="Next" runat="server" Text="创建订单" CssClass="MyBt" 

                                        onclick="Next_Click" />

                                    </td>

                             </tr>

                            </table>

 

 

Shopping.aspx.cs

        //处于编辑状态

        protected void gvqy_RowEditing(object sender,GridViewEditEventArgs e)

        {

            gvqy.EditIndex = e.NewEditIndex;

            BindGridView();

        }

        

        //取消编辑

        protected void gvqy_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)

        {

            gvqy.EditIndex = -1;

            BindGridView();

        }

 

        //执行编辑

        protected void gvqy_RowUpdating(object sender,GridViewUpdateEventArgs e)

        {

            int qyid = gvqy.DataKeys[e.RowIndex][0].GetString().GetInt();

            TextBox txtNums = gvqy.Rows[e.RowIndex].FindControl("txtNums")as TextBox;

            BLL.TcOrder bbk = new BLL.TcOrder();

            Model.TcOrder mbk = new Model.TcOrder();

            mbk = bbk.GetModel(qyid);

            mbk.Nums = txtNums.Text.GetInt();

            bbk.UpdateNum(mbk);

            gvqy.EditIndex = -1;

            BindGridView();

        }

 

        //删除

        protected void buttondel_Click(object sender,EventArgs e)

        {

            LinkButton lb = sender as LinkButton;

            int id = lb.CommandArgument.Trim().GetInt();

 

            int[] ids = { 0 };

            if (!ids.Contains(id))

            {

                BLL.TcOrder bbk = new BLL.TcOrder();

                bbk.Delete(id);

                BindGridView();

            }

        }

        protected void Next_Click(object sender,EventArgs e)

        {

            Response.Redirect("Step4.aspx",true);

        }

阅读全文
1 0