[英]Edit button not working properly after adding 'Add' button in the gridview footer
[英]Add button on gridview footer
我有一個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" />
<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);
}
我知道這是一個舊線程,但是為了幫助其他有問題的人,我猜答案是將GridView DataBind()
事件移到If(!IsPostBack)
語句之外。
其背后的原因是在databind上創建了按鈕。 回發發生,並且按鈕和click命令不再在那里,因此什么也沒有發生。
只需將databind移動到Page_Load
意味着它總是綁定數據,因此總是創建鏈接按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.