导出一个带有链接button的gridview到excel

我有一个与Onclick事件链接button的GridView打开.pdf文件。 文件path是在.aspx页面的Onclick事件调用的后台代码中dynamic生成的。 我能够出口到excel罚款。 该列作为超链接而来。 但我的问题是Onclick事件没有启用,或者没有得到与其他数据一起导出到Excel文件。

在我的GridView(名为GVActiveMEOs)中,我将DataKeyNames设置为DataKeyNames="MEO_NR" 。 而在asp:TemplateField里面我有这个asp:LinkButton

 <asp:linkbutton id="lnk" runat="server" onclick="OpenMEO" commandargument='<%# Eval("MEO_NR") %>' text='<%# Eval("MEO_NR") %>'> </asp:linkbutton> 

代码隐藏

 Protected Sub OpenMEO(ByVal sender As Object, ByVal e As EventArgs) Dim strConnString As String = ConfigurationManager.ConnectionStrings("AEM_ESS001_DEVConnectionString").Connect‌​ionString Dim con As New SqlConnection(strConnString) Dim cmd As New SqlCommand Dim MPath As String 'Dim index As Integer = GVActiveMEOs.SelectedRow.RowIndex 'Dim meonum As String = GVActiveMEOs.SelectedDataKey.Value Dim meonum As String = DirectCast(sender, LinkButton).CommandArgument cmd.CommandType = CommandType.Text cmd.CommandText = "Select MEOPATH from dbo.udf_GetMEOpath(@myid)" cmd.Parameters.AddWithValue("@myid", meonum) cmd.Connection = con Try con.Open() MPath = cmd.ExecuteScalar() 'MsgBox(MPath) ' Dim script As String = "window.open('" & MPath & "', 'Popup', '_newtab');" 'Page.ClientScript.RegisterStartupScript(Me.[GetType](), "open", script, True) Response.Redirect(MPath) End Try End Sub 

任何人都可以请给我一些帮助。 Rgds,Suma

许多方法来实现这一点。

1.更改asp:LinkButton标记并指定CommandName

 <asp:linkbutton id="lnk" runat="server" onclick="OpenMEO" commandargument='<%# Eval("MEO_NR") %>' commandname="Select" text='<%# Eval("MEO_NR") %>'> </asp:linkbutton> 

创build一个RowCommandEvent

标记

 OnRowCommand="GVActiveMEOs_OnRowCommand" 

代码隐藏

 Protected Sub gridview1_RowCommand(sender As Object, e As GridViewCommandEventArgs) 'custom code Dim meonum As String = DirectCast(e.CommandSource, LinkButton).CommandArgument 'custom code End Sub 

请记住, 单击的行不是SelectedRow除非您将CommandName指定为CommandName="Select"