将带有链接按钮的gridview导出为ex​​cel

时间:2014-05-01 21:26:43

标签: asp.net excel gridview export

我有一个带有Onclick事件的Link按钮的gridview来打开.pdf文件。文件路径是在.aspx页面中的Onclick事件调用的代码后面的函数下动态生成的。我可以将它导出到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 个答案:

答案 0 :(得分:0)

实现这一目标的方法很多。

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

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

2.创建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"