Asp.net GridView按钮的位置如何确定单元格[?]e.Row.Cells[0]。控件[0];

Asp.net GridView按钮的位置如何确定单元格[?]e.Row.Cells[0]。控件[0];,asp.net,gridview,webforms,Asp.net,Gridview,Webforms,我在GridView上方放置了几个按钮 我读过一篇文章,是关于在没有显式编辑链接按钮的情况下执行更新/取消按钮的,该按钮会变成更新/取消链接 看来这篇文章 提到 因此,e.Cells[0]为您提供了第一个表格单元格。如果您已将此列放置在其他位置(例如,所有其他列的末尾),则应相应地更改索引。控件[0]为您提供了“更新”按钮的引用 我不知道控件[0]是更新按钮吗 我该如何处理更新按钮的单元格[?]呢 错误: Unable to cast object of type 'System.Web.UI.

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

我读过一篇文章,是关于在没有显式编辑链接按钮的情况下执行更新/取消按钮的,该按钮会变成更新/取消链接

看来这篇文章

提到

因此,e.Cells[0]为您提供了第一个表格单元格。如果您已将此列放置在其他位置(例如,所有其他列的末尾),则应相应地更改索引。控件[0]为您提供了“更新”按钮的引用

  • 我不知道控件[0]是更新按钮吗
  • 我该如何处理更新按钮的单元格[?]呢
  • 错误:

    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;
    
    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${1}')", GridView1.ID, e.Row.RowIndex);
    Button2.Attributes["onclick"] = cancelScript;
    
    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">
    
    编辑/更新:

    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;
    
    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${1}')", GridView1.ID, e.Row.RowIndex);
    Button2.Attributes["onclick"] = cancelScript;
    
    如果我从编辑按钮中删除visible=false,这就是gridview中的HTML

     <asp:CommandField ButtonType="Link" ShowEditButton="true"  EditText="edit" ItemStyle-Width="5px">
                                        <ItemStyle Width="5px"></ItemStyle>
                                    </asp:CommandField>
    
    
    
    此代码按预期工作

    但是,网格上方的更新/取消按钮未获得正确的信息


    您应该能够使用代码隐藏中的给定ID访问按钮,因为它们不是gridview的一部分。试着替换

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


    无法将类型“System.Web.UI.WebControls.Button”隐式转换为“System.Web.UI.WebControls.LinkButton”我不确定为什么它是LinkButton。。。您可以在第二行中删除整行和参考按钮1。示例:
    stringupdatescript=ClientScript.GetPostBackClientHyperlink(Button1,“”)我将其更改为Button updateBtn=Button1;但是更新按钮没有更新我现在意识到。。。我从一个按钮更改为一个链接按钮,但这没有帮助-是的,我需要更改以添加一个事件。。我以为代码可以做到这一点,但我可以理解为什么它不能做到这一点,特别是对于更新,取消按钮似乎更容易工作
    
    Button updateBtn = Button1;