【问题标题】:How to get text value from Control.Controls(i) - VB.Net如何从 Control.Controls(i) 获取文本值 - VB.Net
【发布时间】:2015-07-13 11:22:14
【问题描述】:

简要说明我想要做什么:我正在将数据从 gridview 导出到 Excel,效果很好,但是 gridview 上隐藏了某些列,这些列没有隐藏在提取中(使用了一些我发现的代码在做研究的时候)。所以我想将列名与控制文本值进行比较,以了解哪个不包含在正在导出的标题中。希望这是有道理的。所以这是我的代码:

Dim sgv As GridView = CType(ContentPlaceHolder_body.FindControl("SummaryGridView"), GridView)
sgv.AllowPaging = False
sgv.DataBind()

sExportFileName = Path.GetFileName(Request.PhysicalPath)
        sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"

Export(sExportFileName, sgv)

Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
    HttpContext.Current.Response.ContentType = "application/ms-excel"
    Dim sw As StringWriter = New StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    Dim HeaderName
    Dim sRowData As String = ""
    '  Create a form to contain the grid
    Dim table As Table = New Table
    table.GridLines = gv.GridLines
    '  add the header row to the table

    If (Not (gv.HeaderRow) Is Nothing) Then
        PrepareControlForExport(gv.HeaderRow, gv)
        table.Rows.Add(gv.HeaderRow)
    End If
    '  add each of the data rows to the table
    For Each row As GridViewRow In gv.Rows
        PrepareControlForExport(row)
        table.Rows.Add(row)
    Next
    '  add the footer row to the table
    If (Not (gv.FooterRow) Is Nothing) Then
        PrepareControlForExport(gv.FooterRow, gv)
        table.Rows.Add(gv.FooterRow)
    End If
    '  render the table into the htmlwriter
    table.RenderControl(htw)
    '  render the htmlwriter into the response
    HttpContext.Current.Response.Write(sw.ToString)
    HttpContext.Current.Response.End()
End Sub

' 用文字替换任何包含的控件

Private Shared Sub PrepareControlForExport(ByVal control As Control, ByVal gv As GridView)
    Dim i As Integer = 0

    Do While (i < control.Controls.Count)
        Dim current As Control = control.Controls(i)
        If (TypeOf current Is LinkButton) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
        ElseIf (TypeOf current Is ImageButton) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
        ElseIf (TypeOf current Is HyperLink) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
        ElseIf (TypeOf current Is DropDownList) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
        ElseIf (TypeOf current Is CheckBox) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
            'TODO: Warning!!!, inline IF is not supported ?
        ElseIf (TypeOf current Is DataControlFieldHeaderCell) Then

''''''''''''所以我想在这里将'当前'控制文本值与 gv.Columns(k).HeaderText 值进行比较 - 如果它们匹配,然后删除

For k As Integer = 0 To gv.Rows.Count - 1
                If (current text value) = gv.Columns(k).HeaderText Then
                    If Not gv.Columns(k).Visible Then
                        control.Controls.Remove(current)
                        control.Controls.AddAt(i, New LiteralControl(CType(current, DataControlFieldHeaderCell).Text))
                    End If
                End If

            Next
        End If
        If current.HasControls Then
            JSInternal_Report_DateOnlyRefined.PrepareControlForExport(current, gv)
        End If
        i = (i + 1)
    Loop
End Sub

希望这是有道理的。

提前感谢您的帮助。

【问题讨论】:

  • 我应该补充一点,我是从 MasterPage 运行它的。 Gridview 在使用母版页的页面上

标签: asp.net vb.net gridview web-controls


【解决方案1】:

找到了解决办法。

场景:我有一个带有导出按钮的母版页,它使用母版页从页面上的 gridview 导出数据。

在带有 Gridview 的页面的 Page_Load 事件中,我使用了以下代码:

Dim myMasterPage As MasterPageName = Page.Master

Dim exportButton As System.Web.UI.WebControls.Button = myMasterPage.FindControl("ButExportExcel")

If (exportButton IsNot Nothing) Then 
    AddHandler exportButton.Click, AddressOf Me.ButExportExcel_Click
End If

然后我创建了公共子:


Private Sub ButExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim sMethod = "ButExportExcel_Click"
    Dim sErrorMessage = ""
    Dim sExportFileName As String = ""

    Try

        sExportFileName = Path.GetFileName(Request.PhysicalPath)
        sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"

        Response.Clear()
        Response.AddHeader("content-disposition", "attachment; filename=" & sExportFileName)
        Response.ContentType = "application/vnd.xls"
        Dim WriteItem As System.IO.StringWriter = New System.IO.StringWriter()
        Dim htmlText As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(WriteItem)
        SummaryGridView.AllowPaging = False
        'Dim dtSupplier As DataTable = CType(ViewState("dtSupplier"), DataTable)
        'SummaryGridView.DataSource = dtSupplier
        SummaryGridView.DataBind()
        SummaryGridView.RenderControl(htmlText)
        Response.Write(WriteItem.ToString())
        Response.End()

    Catch ex As Exception



    End Try
End Sub

然后添加以下内容:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub

不幸的是,这意味着它需要进入您希望从中导出网格视图的每个页面,但它为我完成了这项工作。这只会导出gridview中的数据。

我希望这对遇到同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多