如何在单击时引用asp:grid内的链接按钮

时间:2012-05-05 13:49:49

标签: c# asp.net linkbutton

我有一个ASP:网格,它有一个链接按钮,我需要一些如何在其点击后面的代码上引用但我在语法上挣扎

继承我的ASP:网格我需要在后面的代码中执行代码,当按下“Re-Take”链接按钮并且还能够知道它被点击的行,因为我将需要引用用户的电子邮件和输入名称,然后发送包含相关信息的电子邮件....

<asp:GridView ID="GrdViewUsers" runat="server" AutoGenerateColumns="false" GridLines="None"
                    EnableViewState="false" class="tablesorter">
    <AlternatingRowStyle></AlternatingRowStyle>
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton Text="Re-Take" runat="server" ID="Edit" CommandName="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Full Name">
            <ItemTemplate>
                <asp:HyperLink ID="HyperFullName" CssClass="gvItem" runat="server" NavigateUrl='<%#Eval("UserID","/ExamPaper.aspx?uid={0}") %>'
                    Text='<%# DataBinder.Eval(Container,"DataItem.FullName") %>'></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email">
            <ItemTemplate>
                <asp:Label ID="lblSurname" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Exam Taken">
            <ItemTemplate>
                <asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExamTaken") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date Taken">
            <ItemTemplate>
                <asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DateTaken") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Exam Total">
            <ItemTemplate>
                <asp:Label ID="lblUsername" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExamTotal") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

如果有人可以用我的片段来帮助我,我会非常感激

4 个答案:

答案 0 :(得分:1)

你可以稍微改变一下。您可以看到,当控件放置在gridview中时,从该控件引发的任何事件也会引发GridView上的RowCommand。

要获得您想要的内容,您可以将CommandName和CommandArgument添加到LinkBut​​ton,然后在GridView的RowCommand中捕获它。

<asp:LinkButton id="LinkButton1" runat="server" commandName="LinkButtonClicked" commandArgument='Eval("myObjectID")' />

其中myObjectID是将网格绑定到对象的ID列的名称。

然后

void GridView1_RowCommand( object sender, GridViewCommandEventArgs e )
{
    if ( e.CommandName == "LinkButtonClicked" )
    {
        string id = e.CommandArgument; // this is the ID of the clicked item
    }
}

请参阅:ASP.net GridView: get LinkItem's row

答案 1 :(得分:0)

FindControl应该适用于这种情况。

protected void GrdViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        HyperLink myHyperLink = e.Row.FindControl("Edit") as HyperLink;
    }
}

答案 2 :(得分:0)

假设网格有链接按钮,我们想要获取行索引

 <asp:LinkButton ID="lnkbtnAdd " runat="server" CommandName="cmdAdd" ImageUrl="~/Images/add.gif" ></asp:LinkButton>

在后面的代码中,在OnRowCreated事件中,我们将行的行号附加到行的每个按钮,以便在RowCommand事件中单击它时将其返回

    protected void gvListing_RowCreated(object sender, GridViewRowEventArgs e)
    {           
        if (e.Row.RowType == DataControlRowType.DataRow)
        {                
          System.Web.UI.WebControls.LinkButton lnkbtnAdd = new System.Web.UI.WebControls.LinkButton();    
                lnkbtnAdd = (System.Web.UI.WebControls.LinkButton)e.Row.FindControl("lnkbtnAdd");                    
                if (lnkbtnAdd != null)
                    lnkbtnAdd .CommandArgument = e.Row.RowIndex.ToString();
         }          
    }

在RowCommand事件中,我们将返回当前行索引并设置所选的网格索引

protected void gvListing_RowCommand(object sender, GridViewCommandEventArgs e)
{      
   if (e.CommandName.ToString() == "cmdAdd")
   { 
       int RowIndex = int.Parse(e.CommandArgument.ToString());// Current row

   }
}

答案 3 :(得分:0)

首先:您在TemplateFields中重复了ID,例如lblUsername不允许的内容,因为它是相同的NamingContainer

您可以将RowIndex作为CommandArgument传递给RowCommand

on aspx:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton 
           Text="Re-Take" 
           runat="server" 
           ID="Edit" 
           CommandName="Edit"
           CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">
        </asp:LinkButton>
    </ItemTemplate>
</asp:TemplateField>

处理GridView的RowCommand

  void GrdViewUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="Edit")
    {
        int index = Convert.ToInt32(e.CommandArgument);      
        GridViewRow row = GrdViewUsers.Rows[index];
        // now you can get all of your controls like:
        Label lblSurname = (Label)row.FindControl("lblSurname");
        String email = lblSurname.Text // you noticed that DataItem.Email is bound to lblSurname?
    }
  }