在gridview页脚上添加按钮

时间:2015-12-02 18:27:42

标签: c# asp.net gridview

我有一个gridview,我想在gridview页脚的字段上添加记录。

<asp:GridView ID="gvDocs" runat="server" AutoGenerateColumns="false" CssClass = "table" DataKeyNames="id" ShowFooter="true"  
        OnRowCommand="gvDocs_RowCommand" OnRowDataBound="gvDocs_RowDataBound" OnRowDeleting="gvDocs_RowDeleting" >
        <Columns>
            <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="  Name" HeaderStyle-BackColor="Gray" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderColor="Black" HeaderStyle-BorderWidth="1px" HeaderStyle-ForeColor="White" HeaderStyle-Font-Names="Verdana, Geneva, Tahoma, sans-serif" HeaderStyle-Font-Size="X-Small" ItemStyle-Font-Names="Verdana, Geneva, Tahoma, sans-serif" ItemStyle-Font-Size="X-Small" />
            <asp:BoundField ItemStyle-Width="150px" DataField="last_name" HeaderText="  Last Name" HeaderStyle-BackColor="Gray" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderColor="Black" HeaderStyle-BorderWidth="1px" HeaderStyle-ForeColor="White" HeaderStyle-Font-Names="Verdana, Geneva, Tahoma, sans-serif" HeaderStyle-Font-Size="X-Small" ItemStyle-Font-Names="Verdana, Geneva, Tahoma, sans-serif" ItemStyle-Font-Size="X-Small" />
            <asp:BoundField ItemStyle-Width="100px" DataField="valor_documento" HeaderText="  Valor" DataFormatString="{0:#,##.00 €}" HeaderStyle-BackColor="Gray" HeaderStyle-BorderStyle="Solid" HeaderStyle-BorderColor="Black" HeaderStyle-BorderWidth="1px" HeaderStyle-ForeColor="White" HeaderStyle-Font-Names="Verdana, Geneva, Tahoma, sans-serif" HeaderStyle-Font-Size="X-Small" ItemStyle-Font-Names="Verdana, Geneva, Tahoma, sans-serif" ItemStyle-Font-Size="X-Small" />
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button runat="server" ID="delbutton" CommandArgument='<%# Eval("id") %>' CommandName="Delete" Text="Delete" CssClass="btn btn-default pad" />
                    &nbsp;
                    <asp:Button runat="server" ID="editbutton" CommandArgument='<%# Eval("id") %>' Text="Edit" UseSubmitBehavior="false" CssClass="btn btn-default pad" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" CssClass="pad" Width="250px" />
                <HeaderStyle BackColor="Gray" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" ForeColor="White" />
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

在代码隐藏中我填充页脚

protected void gvDocs_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.Footer)
    {
        TextBox txbname = new TextBox();
        txbname.ID="txbname ";
        txbname.Columns = 10;
        txbname.AutoCompleteType=AutoCompleteType.None;
        e.Row.Cells[0].Controls.Add(txbname);

        TextBox txblname = new TextBox();
        txblname.ID = "txblname";
        txblname.Columns = 10;
        txblname.AutoCompleteType = AutoCompleteType.None;
        e.Row.Cells[1].Controls.Add(txblname);

        Button bt = new Button();
        bt.ID = "svdoc";
        bt.Text = "Save";
        bt.CommandName = "Save";
        bt.CommandArgument = "1";
        bt.CssClass = "btn btn-default pad";
        bt.UseSubmitBehavior = false;
        e.Row.Cells[2].Controls.Add(bt);
        e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
        bt.Click += new EventHandler(save_click);
    }
}

但是在生成的代码上,它会执行click事件。

这就是按钮在html上的显示方式:

<input type="button" name="ctl00$MainContent$gvDocs$ctl04$svdoc" value="Save" onclick="javascript:__doPostBack('ctl00$MainContent$gvDocs$ctl04$svdoc','')" id="MainContent_gvDocs_svdoc" class="btn btn-default pad">

任何消化? 感谢。

更新

protected void save_click(object sender, EventArgs e)
{
    TextBox tbn = (TextBox)this.FindControl("txbname");
    String nm= tbn.Text;
    TextBox tbln = (TextBox)this.FindControl("txblname");
    String ln= tbln .Text;
    Response.Write("HERE! " + nm+ "##" + tbln);
    Response.End();

    ScriptManager.RegisterStartupScript(Page, typeof(Page), "Close", "__doPostBack('','Insert');", true);
}

1 个答案:

答案 0 :(得分:0)

我知道这是一个旧线程,但是为了帮助其他有问题的人,我想答案是将.setCoder(KvCoder.of( StringUtf8Coder.of(), combiner.getDefaultOutputCoder(pipeline.getCoderRegistry, VarLongCoder.of())))``` 事件移到GridView DataBind()语句之外。

其背后的原因是在databind上创建了按钮。回发发生,并且按钮和click命令不再在那里,因此什么也没有发生。

仅将数据绑定移动到If(!IsPostBack)意味着它总是绑定数据,因此总是创建链接按钮。