Cells[?]e.Row.Cells[0].Controls[0]; 如何确定GridView按钮的位置

GridView location of button how to determine the Cells[?] e.Row.Cells[0].Controls[0];

我在 GridView 上方放置了几个按钮

我读过一篇关于在没有显式侵入式编辑 link 按钮的情况下执行此 Update/Cancel 按钮的文章,该按钮变成更新/取消 links

文章好像是http://www.binaryintellect.net/articles/4afd676c-693b-40e2-ac86-129429c71078.aspx

提到

" So, e.Cells[0] gives you the first table cell. If you have placed this column at some other position (end of all the other columns, for example) then you should change the index accordingly. Controls[0] gives you reference of the Update button. "

  1. 我不知道 Controls[0] 是更新按钮?
  2. 怎么我的Cells[?]更新按钮也是这样?

错误:

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton'.


  LinkButton updateBtn = (LinkButton)e.Row.Cells[0].Controls[0];
  Line 339:                 string updateScript =      ClientScript.GetPostBackClientHyperlink(updateBtn, "");
  Line 340:                 Button1.Attributes["onclick"] = updateScript;

HTML

<asp:Button ID="Button1" runat="server" Text="Update" />
                        <asp:Button ID="Button2" runat="server" Text="Cancel" />
                        <asp:GridView ID="GridView1" CssClass="table" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowCommand="GridView1_RowCommand" Caption='<table border="1" width="100%" cellpadding="0" cellspacing="0" bgcolor="CornflowerBlue"><tr><td><B>NORTH</B></td></tr></table>'
                            CaptionAlign="Top" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" Width="100%" OnSelectedIndexChanged="OnSelectedIndexChanged">

代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)            

if (e.Row.RowIndex == GridView1.EditIndex) {  

LinkButton updateBtn = (LinkButton)e.Row.Cells[0].Controls[0];
string updateScript = ClientScript.GetPostBackClientHyperlink(updateBtn, "");
Button1.Attributes["onclick"] = updateScript;

string cancelScript = string.Format("javascript:__doPostBack('{0}','Cancel')", GridView1.ID, e.Row.RowIndex);
Button2.Attributes["onclick"] = cancelScript;

EDIT/UPDATE:

如果我从“编辑”按钮中删除一个 visible=false,这就是 gridviewHTML

 <asp:CommandField ButtonType="Link" ShowEditButton="true"  EditText="edit" ItemStyle-Width="5px">
                                    <ItemStyle Width="5px"></ItemStyle>
                                </asp:CommandField>

Chrome 浏览器在编辑模式下查看一行

<a href="javascript:__doPostBack('GridView1$_ctl12$_ctl0','')">Update</a>
<a href="javascript:__doPostBack('GridView1','Cancel')">Cancel</a>

此代码按预期工作...

但是网格上方的更新/取消按钮没有获取正确的信息

<a onclick="javascript:__doPostBack('Button11','');" id="Button11" href="javascript:__doPostBack('Button11','')">Update</a>
<a onclick="javascript:__doPostBack('GridView1','Cancel');" id="Button22" href="javascript:__doPostBack('Button22','')">Cancel</a>

您应该能够在后面的代码中访问具有给定 ID 的按钮,因为它们不是 gridview 的一部分。尝试替换

LinkButton updateBtn = (LinkButton)e.Row.Cells[0].Controls[0];

Button updateBtn = Button1;