我在一个ASPX 页面上放置了一个DATAGRID , DATAGRID 可以输入数据。并且在页面初始化时会给 DataGrid 绑定数据源。在页面的底部放置了几个按钮: Report, Printer, Excel, Chart,Screen 等。当我点击下面的任意按钮时,首先要把 DataGrid 中输入的数据保存到 DataTable 中,然后再 DataTable传递给报表引擎。但是,当我点击下面的按钮时,它并没有进入 ButtonClick 事件处理函数中,而是首先调用 PageLoad 方法,这样一来,在DataGrid 中输入的数据就丢失了。请问我该如何解决这个问题。
{
if (!Page.IsPostBack)
{
code
}
}
<%@ Page Language="VB" AutoEventWireup ="true" Theme="Reports" MasterPageFile="~/masterpages/Reports.master" CodeFile="main.aspx.vb" Inherits="reports_main" %>
<%@ Register TagPrefix="sp1" TagName="ReportHeaderControl" Src="spReportHeader.ascx" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<asp:Content ID="PageContent" runat="server" ContentPlaceHolderID="_mainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="False" Skin="Hay">
</telerik:RadSkinManager>
<font size="3" face="Arial"><b>Selected Report: </b>
<asp:Label ID="RptLbl1" runat="server"></asp:Label>
<br />
</font>
<br />
<asp:DropDownList ID="ReportGrid" runat="server" Width="50%" DataTextField="ReportName"
DataValueField="ReportID" Visible="False">
</asp:DropDownList>
<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Get Criteria & Parameters">
</asp:Button>
<p>
</p>
<h3>
Report Parameters
</h3>
<asp:DataGrid ID="ParameterGrid" runat="server" GridLines="None" Width="90%" InPlaceEditing="True"
OnEditCommand="ParameterGrid_Edit" OnCancelCommand="ParameterGrid_Cancel" OnUpdateCommand="ParameterGrid_Update"
AutoGenerateColumns="False">
<HeaderStyle Font-Size="9px" Font-Names="Arial" Font-Bold="True" ForeColor="White"
BackColor="Black"></HeaderStyle>
<AlternatingItemStyle Font-Size="9px" Font-Names="Arial" ForeColor="Black" BackColor="#F4F4F4">
</AlternatingItemStyle>
<ItemStyle Font-Size="9px" Font-Names="Arial" ForeColor="Black" BackColor="White"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel"
EditText="Edit">
<ItemStyle Font-Size="9px" Width="10%"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="Name" ReadOnly="True" HeaderText="Parameter Name"></asp:BoundColumn>
<asp:BoundColumn DataField="Value" HeaderText="Value"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
<table border="1" width="90%">
<tr>
<td>
<font size="1" face="Arial"><b>Some reports don't have parameters!</b> If the report
does have a parameter, you have to enter parameter values for it to execute properly.
To enter a report Parameter, Click Edit, Edit the Parameter you wish, then click
update. Press one of the buttons below to run a report.</font></td>
</tr>
</table>
<h3>
Report Columns</h3>
<asp:DataGrid ID="ColumnGrid" runat="server" Width="90%" OnItemCreated = "OnGridItemCreated"
GridLines="None" AutoGenerateColumns="False" PageSize="120">
<HeaderStyle Font-Size="9px" Font-Names="Arial" Font-Bold="True" ForeColor="White"
BackColor="Black"></HeaderStyle>
<AlternatingItemStyle Font-Size="9px" Font-Names="Arial" ForeColor="Black" BackColor="#F4F4F4">
</AlternatingItemStyle>
<ItemStyle Font-Size="9px" Font-Names="Arial" ForeColor="Black" BackColor="White"></ItemStyle>
<Columns>
<asp:BoundColumn DataField="ColumnName" ReadOnly="True" HeaderText="Column Name" HeaderStyle-Width ="150" ItemStyle-Width ="150"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Show" HeaderStyle-Width ="40">
<ItemTemplate>
<asp:CheckBox ID="chkShow" Checked='<%# DataBinder.Eval(Container, "DataItem.Show") %>'
runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Total" HeaderStyle-Width ="40">
<ItemTemplate>
<asp:CheckBox ID="chkTotal" Checked='<%# DataBinder.Eval(Container, "DataItem.Total") %>'
Enabled='<%# DataBinder.Eval(Container, "DataItem.IsNumeric") %>' runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sort Order" HeaderStyle-Width ="60">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:TextBox ID="sortOrderDataList" runat="server" Width ="60px" Text='<%# DataBinder.Eval(Container, "DataItem.SortOrder") %>' />
</ItemTemplate>
<FooterStyle HorizontalAlign="Center"></FooterStyle>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sort Direction" HeaderStyle-Width ="80">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="sortDirectionList" runat="server" Width ="95%" >
<asp:ListItem Value="ASC">Ascending</asp:ListItem>
<asp:ListItem Value="DESC">Descending</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Search Operator" HeaderStyle-Width ="100">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:DropDownList ID="searchOperatorList" runat="server" Oninit="OnSearchOperatorListInit" Width ="95%">
<asp:ListItem Value="None">None</asp:ListItem>
<asp:ListItem Value=">">></asp:ListItem>
<asp:ListItem Value=">=">>=</asp:ListItem>
<asp:ListItem Value="<"><</asp:ListItem>
<asp:ListItem Value="<="><=</asp:ListItem>
<asp:ListItem Value="=">=</asp:ListItem>
<asp:ListItem Value="!=">!=</asp:ListItem>
<asp:ListItem Value="LIKE">LIKE</asp:ListItem>
<asp:ListItem Value="BETWEEN">BETWEEN</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Search Criteria">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<p>
<asp:CheckBox ID="TopCheck" runat="server" AutoPostBack="True" OnCheckedChanged="OnTopCheckedChanged">
</asp:CheckBox>Return top
<asp:TextBox ID="TopText" runat="server" Columns="5">10</asp:TextBox>
<asp:Label ID="TopLabel" runat="server"></asp:Label> rows</p>
<br>
<hr>
<h3>
Run Report</h3>
<table id="Table1" cellspacing="0" cellpadding="0" align="center" valign="middle" rules="none"
width="60%" border="0" runat="server">
<tbody>
<tr>
<td width="33%" align="center" valign="middle">
<asp:LinkButton ID="ExcelLink" runat="server" Font-Size="8pt" Font-Names="Arial" Font-Bold="True"
Width="100%" OnClick="OnLinkButtonClick"><img src="excel.jpg" border="0" />
</asp:LinkButton>
</td>
<td width="33%" align="center" valign="middle">
<asp:LinkButton ID="PrintLink" runat="server" Font-Size="8pt" Font-Names="Arial" Font-Bold="True"
Width="100%" OnClick="OnLinkButtonClick"><img src="printer.jpg" border="0" />
</asp:LinkButton>
</td>
<td width="33%" align="center" valign="middle">
<asp:LinkButton ID="ScreenLink" runat="server" Font-Size="8pt" Font-Names="Arial" Font-Bold="True"
Width="100%" OnClick="OnLinkButtonClick"><img src="screen.jpg" border="0" />
</asp:LinkButton>
</td>
</tr>
<tr>
<td width="33%" align="center" valign="middle">
<asp:LinkButton ID="TextLink" runat="server" Font-Size="8pt" Font-Names="Arial" Font-Bold="True"
Width="100%" OnClick="OnLinkButtonClick" ><img src="text.jpg" border="0" />
</asp:LinkButton>
</td>
<td width="33%" align="center" valign="middle">
<asp:LinkButton ID="XMLLink" runat="server" Font-Size="8pt" Font-Names="Arial" Font-Bold="True"
Width="100%" OnClick="OnLinkButtonClick"><img src="XML.jpg" border="0" />
</asp:LinkButton>
</td>
<td width="33%" align="center" valign="middle">
<asp:LinkButton ID="ChartLink" runat ="Server" Font-Size ="8px" Font-Names ="Arial" Font-Bold ="True"
Width ="100%" OnClick ="OnLinkButtonClick" ><img src="chart.jpg" border="0" /></asp:LinkButton>
</td>
</tr>
</tbody>
</table>
<br />
<hr />
</asp:Content>
{
第一次加载时执行的代码段,像DataGrid数据绑定
}执行按钮事件是属于网页回发,所以不执行if(!IsPostBack)代码段,从而避免重新绑定数据
实际上当再次执行到这里时, DataGrid 的数据源已经为 Nothing 了。如果我要重新绑定,那么在 DataGrid 中输入的数据就会丢失。
是这样做的。这样做的目的只是让数据绑定一次,但是当我点击按钮时,它又回到PageLoad方法,而不是ButtonClick 方法。我这时不能再重新绑定数据源,如果绑定的话,在DATAGRID中已经输入的数据就会丢失。但我不明白的是为什么DataGrid 的数据源也变成 Nothing 了。ASP从来没用过也没有学过。但是公司就让我修改BUG,真是头痛。
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports Telerik.Web.UIPartial Class reports_main
Inherits EnterpriseASPClient.Core.PageBase ' Private ReadOnly SQL_SELECTITEMID As String = "SELECT ITEMID,ITEMNAME FROM INVENTORYITEMS WHERE CompanyID = '{0}' AND DivisionID = '{1}' AND DepartmentID = '{2}' AND ITEMTYPEID = 'STOCK' AND ISNULL(ISACTIVE,0) = 1 " Dim ConnectionString As String = ""
Dim CompanyID As String = "DINOS"
Dim DivisionID As String = "DEFAULT"
Dim DepartmentID As String = "DEFAULT"
Dim EmployeeID As String = "" Public Sub New()
MyBase.New()
End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SessionKey As Hashtable = CType(Session("SessionKey"), Hashtable)
If Session("SessionKey") Is Nothing Then
' This is a problem, if this string is null then report was called
' from outside an Enterprise session and we redirect to error page
Response.Redirect("reporterror.htm")
End If ' Change this line to be the security setting from the access permissions table
' for example, if you want the conditions to test against ARReports in Access permissions,
' then put that value here as is the following line
Dim Allowed As Boolean = Session("ARReports")
' Ok, now do security check. If it fails, then exit to security error page
If Not Allowed Then
Response.Redirect("securityerror.htm")
End If ' Ok so everything is fine! Let's run our report now!
ConnectionString = EnterpriseCommon.Configuration.ConfigSettings.ConnectionString
CompanyID = SessionKey("CompanyID")
DivisionID = SessionKey("DivisionID")
DepartmentID = SessionKey("DepartmentID")
EmployeeID = SessionKey("EmployeeID") Dim ReportDescriptionParts() As String = Request.QueryString("ReportID").Split("*"c)
If ReportDescriptionParts.Length <> 3 Then
Response.Redirect("reporterror.htm")
End If
Session("ReportName") = ReportDescriptionParts(1)
Session("ReportID") = GetReportID(Session("ReportName"))
Session("ReportDescription") = ReportDescriptionParts(2)
RptLbl1.Text = ReportDescriptionParts(2) If Not Page.IsPostBack Then
Try
BindReportGrid()
'Initialize Report BindParameterGrid()
BindColumnGrid() Catch ex As Exception
Page.Response.Write("<Font Color='Red'>Can't load data: " + ex.Message + "</Font>")
Debug.Write("Report Engine error:" + ex.Message)
End Try
End If
End Sub 'Call this method to use single connection instance
Function Connection() As SqlConnection
Dim context As HttpContext = HttpContext.Current
Dim myConnection As SqlConnection = context.Items("SqlConnection")
If Not myConnection Is Nothing Then
If myConnection.State = ConnectionState.Open Then
Return myConnection
End If
End If
myConnection = New SqlConnection(ConnectionString)
myConnection.Open()
context.Items("SqlConnection") = myConnection
Return myConnection
End Function Function GetReportID(ByVal ReportName As String) As Integer
' prepare SQL command
' "GL" is omitted since they are pre-defined reports, Check are omitted since they are check datasources, and Doc's are omitted since they
' drive orders, invoices, etc.
Dim command As New SqlCommand( _
"SELECT ID " & _
"FROM sysobjects " & _
"WHERE Name=@Name and type in (N'P', N'PC')", _
Connection())
command.Parameters.AddWithValue("@Name", ReportName)
Dim ReportID As Object = command.ExecuteScalar()
If Not ReportID Is Nothing Then Return CInt(ReportID)
Return 0
End Function Sub BindReportGrid()
' prepare SQL command
' "GL" is omitted since they are pre-defined reports, Check are omitted since they are check datasources, and Doc's are omitted since they
' drive orders, invoices, etc.
Dim myAdapter As New SqlDataAdapter( _
"SELECT Name as ReportName, ID as ReportID " & _
"FROM sysobjects " & _
"WHERE Type = 'P' and left(Name,3) <> 'dt_' and left(Name,2) <> 'ti' and left(Name,3) = 'Rpt' and left(Name,5) <> 'RptGL' and left(Name,8) <> 'RptCheck' and left(Name,6) <> 'RptDoc' and left(Name,6) <> 'RptBan' Order by Name ", _
Connection())
Dim ds As New DataSet()
myAdapter.Fill(ds) ' fill ReportGrid
ReportGrid.DataSource = ds.Tables(0).DefaultView
ReportGrid.DataBind()
End Sub Sub BindParameterGrid()
Dim myAdapter As New SqlDataAdapter( _
"SELECT Name, xtype, Name as Value " & _
"FROM Syscolumns " & _
"WHERE ID = @ReportID and Name <> '@CompanyID' and Name <> '@DivisionID' and Name <> '@DepartmentID' ORDER BY Name DESC ", _
Connection()) myAdapter.SelectCommand.Parameters.AddWithValue("@ReportID", Session("ReportID"))
Dim ds As New DataTable()
myAdapter.Fill(ds) Session(ReportName & ":Parameters") = ds ParameterGrid.DataSource = ds
ParameterGrid.DataBind() If Session(ReportName & ":Top") = Nothing Then Session(ReportName & ":Top") = -1
End Sub Sub GenerateReportColumns()
Dim myCommand As New SqlCommand("enterprise." & ReportName, Connection()) myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Add("@CompanyID", SqlDbType.NVarChar).Value = CompanyID
myCommand.Parameters.Add("@DivisionID", SqlDbType.NVarChar).Value = DivisionID
myCommand.Parameters.Add("@DepartmentID", SqlDbType.NVarChar).Value = DepartmentID Dim ParameterTable As DataTable = Session(ReportName & ":Parameters")
Dim ParameterRow As DataRow
Dim Index As Integer = 0
Dim RecordCount As Integer = 0
Dim RecordType As String = "" If Not ParameterTable Is Nothing Then
RecordCount = ParameterTable.Rows.Count
If RecordCount >= 1 Then
For Index = 0 To (RecordCount - 1)
ParameterRow = ParameterTable.Rows(Index)
Select Case ParameterRow(1)
Case "231"
RecordType = "SQlDbType.NVarChar"
Case "60"
RecordType = "SQlDbType.Money"
Case "61"
RecordType = "SQlDbType.DateTime"
Case "62"
RecordType = "SQlDbType.Int"
Case "59"
RecordType = "SQlDbType.Float"
Case "104"
RecordType = "SQlDbType.Bit"
Case Else
RecordType = "SQlDbType.NVarChar"
End Select
myCommand.Parameters.AddWithValue(ParameterRow(0), RecordType).Value = (ParameterRow(2))
Next Index
End If
End If Dim myAdapter As New SqlDataAdapter(myCommand) Dim schema As New DataTable(), cols As New DataTable() Try
myAdapter.FillSchema(schema, SchemaType.Source)
Catch ex As Exception
EnterpriseASPClient.Core.ASPError.ShowError(New EnterpriseCommon.Core.ErrorBase("Report Error - Error Filling Adapter in Main.aspx", ex))
'Throw New ApplicationException("Error getting grid schema", ex)
End Try cols.Columns.Add("ColumnIndex", Type.GetType("System.Int32"))
cols.Columns.Add("ShowIndex", Type.GetType("System.Int32"))
cols.Columns.Add("ColumnName", Type.GetType("System.String"))
cols.Columns.Add("Show", Type.GetType("System.Boolean"))
cols.Columns.Add("Total", Type.GetType("System.Boolean"))
cols.Columns.Add("Type", Type.GetType("System.String"))
cols.Columns.Add("IsNumeric", Type.GetType("System.Boolean"))
cols.Columns.Add("SortOrder", Type.GetType("System.Int32"))
cols.Columns.Add("SortDirection", Type.GetType("System.String"))
cols.Columns.Add("SearchOperator", Type.GetType("System.String"))
cols.Columns.Add("SearchCriteria", Type.GetType("System.String"))
cols.Columns.Add("Width", Type.GetType("System.Int32"))
cols.Columns.Add("AutoIncremented", Type.GetType("System.Boolean")) Dim numeric As Boolean
Dim colType As Type
Dim typBool As Type = Type.GetType("System.Boolean")
Dim typChar As Type = Type.GetType("System.Char")
Dim typDate As Type = Type.GetType("System.DateTime")
Dim typString As Type = Type.GetType("System.String")
Dim typTimeSpan As Type = Type.GetType("System.TimeSpan") For Index = 0 To schema.Columns.Count - 1
colType = schema.Columns(Index).DataType
numeric = Not (colType Is typBool Or colType Is typChar Or colType Is typDate Or colType Is typString Or colType Is typTimeSpan)
cols.Rows.Add(New Object() {Index, -1, schema.Columns(Index).ColumnName, True, numeric, colType.Name, numeric, IIf(Index = 0, 1, -1), "ASC", "None", "", -1, schema.Columns(Index).AutoIncrement})
Next Index
Columns = cols
End Sub
ColumnGrid.DataSource = Columns
ColumnGrid.DataBind()
CalculateShowIndex()
Session(ReportName & ":ReportData") = Nothing
End Sub Sub CalculateShowIndex()
'Calculate the show position on all visible columns
'This makes it easier to show totals
Dim Index As Integer, ShowIndex As Integer = 0
Dim cols As DataTable = Columns
For Index = 0 To cols.Rows.Count - 1
If cols.Rows(Index)("Show") Then
cols.Rows(Index)("ShowIndex") = ShowIndex
ShowIndex += 1
Else
cols.Rows(Index)("ShowIndex") = -1
End If
Next Index
End Sub Property Columns() As DataTable
Get
If ReportName Is Nothing Then
Return Nothing
ElseIf Session(ReportName & ":Columns") Is Nothing Then
GenerateReportColumns()
End If
Return Session(ReportName & ":Columns")
End Get Set(ByVal Value As DataTable)
Session(ReportName & ":Columns") = Value
End Set
End Property 'The report being shown by this grid
Public ReadOnly Property ReportName() As String
Get
Return Session("ReportName")
End Get
End Property Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Session(ReportName & ":ReportData") = Nothing
BindParameterGrid()
BindColumnGrid()
End Sub
Sub ParameterGrid_Edit(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs) ParameterGrid.EditItemIndex = E.Item.ItemIndex ParameterGrid.DataSource = Session(ReportName & ":Parameters")
ParameterGrid.DataBind() End Sub
Sub ParameterGrid_Update(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs) Dim ParameterTable As DataTable
Dim ParameterRow As DataRow ParameterTable = Session(ReportName & ":Parameters")
ParameterRow = ParameterTable.Rows(E.Item.ItemIndex)
Convert.ChangeType(CType(E.Item.Cells(2).Controls(0), TextBox).Text, Type.GetType(ParameterRow("Type")))
ParameterRow("Value") = CType(E.Item.Cells(2).Controls(0), TextBox).Text ParameterGrid.EditItemIndex = -1 ParameterGrid.DataSource = ParameterTable
ParameterGrid.DataBind() BindColumnGrid() End Sub
Sub ParameterGrid_Cancel(ByVal Sender As Object, ByVal E As DataGridCommandEventArgs) ParameterGrid.EditItemIndex = -1 ParameterGrid.DataSource = Session(ReportName & ":Parameters")
ParameterGrid.DataBind() End Sub
End Class
''' 当 SearchOperator DropDownList 控件创建时调用。
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <res></res>
Sub OnSearchOperatorListInit(ByVal sender As Object, ByVal e As EventArgs)
Dim list As DropDownList = sender
Dim item As DataGridItem = CType(list.Parent.Parent, DataGridItem) ' 设置 DropDownList 的默认值。
list.SelectedValue = "="
End Sub ''' <summary>
''' 当 DataGrid 控件中的项创建时调用。
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <res>不论是在往返行程中还是在将数据绑定到控件时都会调用该方法</res>
Sub OnGridItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Dim item As DataGridItem = e.Item ' 如果正在创建的项是列头(第一行),则不处理。
If item.ItemIndex < 0 Then
Exit Sub
End If Dim control As Control
Dim list As DropDownList = item.Cells(5).FindControl("searchOperatorList")
Dim colName As String = Columns.Rows(item.ItemIndex)("ColumnName")
If colName = "Item ID" Then
For i As Integer = 1 To 2
control = New RadComboBox()
control.ID = "radComboBox" & i
With CType(control, RadComboBox)
.Width = Unit.Percentage(40)
.EnableVirtualScrolling = True
End With
FillItemCombobox(CType(control, RadComboBox))
item.Cells(6).Controls.Add(control)
Next
Else
Dim type As String = Columns.Rows(item.ItemIndex)("Type")
If type.Equals("DateTime") Or type.Equals("TimeSpan") Then
For i As Integer = 1 To 2
control = New RadDatePicker()
control.ID = "radDatePicker" & i
With CType(control, RadDatePicker)
.Width = Unit.Percentage(40)
.MinDate = New Date(1900, 1, 1)
.MaxDate = New Date(2099, 12, 16)
End With
item.Cells(6).Controls.Add(control)
Next
Else
For i As Integer = 1 To 2
control = New RadTextBox()
control.ID = "radTextBox" & i
With CType(control, RadTextBox)
.Width = Unit.Percentage(40)
End With
item.Cells(6).Controls.Add(control)
Next
End If
End If
End Sub ''' <summary>
''' 设置 Item ID 组合框中的项。
''' </summary>
''' <param name="radCombobox"></param>
''' <res></res>
Sub FillItemCombobox(ByVal radCombobox As RadComboBox)
Dim commandText As String = String.Format(SQL_SELECTITEMID, CompanyID, DivisionID, DepartmentID)
Dim ds As DataSet = ReadData(commandText)
If ds.Tables.Count > 0 Then
Dim rows As DataRowCollection = ds.Tables(0).Rows
If rows.Count > 0 Then
For i As Integer = 0 To rows.Count - 1
Dim item As New RadComboBoxItem(rows(i)("ItemID").ToString(), rows(i)("ItemID").ToString())
radCombobox.Items.Add(item)
Next
End If
End If
End Sub ''' <summary>
''' LinkButton Click 事件处理。
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <res></res>
Sub OnLinkButtonClick(ByVal sender As Object, ByVal e As EventArgs) ' 保存 DataGrid 中的数据
SaveData() Dim linkButton As LinkButton = CType(sender, LinkButton)
Select Case linkButton.ID
Case "ScreenLink"
Response.Redirect("ASPXReportEngineScreenTemplate.aspx?ReportName=" & ReportName)
Case "PrintLink"
Response.Redirect("ASPXReportEnginePrintTemplate.aspx?ReportName=" & ReportName)
Case "ExcelLink"
Response.Redirect("ASPXReportEngineExcelTemplate.aspx?ReportName=" & ReportName)
Case "TextLink"
Response.Redirect("ASPXReportEngineTextTemplate.aspx?ReportName=" & ReportName)
Case "XMLLink"
Response.Redirect("ASPXReportEngineXMLTemplate.aspx?ReportName=" & ReportName)
Case "ChartLink"
Response.Redirect("ASPXReportEngineChartTemplate.aspx?ReportName=" & ReportName)
End Select
End Sub ''' <summary>
''' 读取数据库中的数据。
''' </summary>
''' <param name="commandText">SQL 文本</param>
''' <returns></returns>
''' <res></res>
Private Function ReadData(ByVal commandText As String) As DataSet
Dim connect As SqlConnection = Connection()
Dim dataAdapter As SqlDataAdapter = Nothing
Dim command As SqlCommand = Nothing
Dim ds As DataSet = Nothing
Try
command = connect.CreateCommand()
command.CommandText = commandText
command.CommandType = CommandType.Text
dataAdapter = New SqlDataAdapter(command)
ds = New DataSet
dataAdapter.Fill(ds)
Catch ex As Exception
'
' Response.Redirect("???????????.html")
Finally
If Not IsNothing(ds) Then
ds.Dispose()
End If
If Not IsNothing(dataAdapter) Then
dataAdapter.Dispose()
End If
If Not IsNothing(command) Then
command.Dispose()
End If
End Try
Return ds
End Function ''' <summary>
''' 保存 DataGird 中的数据
''' </summary>
''' <res></res>
Private Sub SaveData()
Dim row As DataRow
Dim type As String
Dim op As String
Dim colName As String
Dim control As Control
Dim criteria As String For Each item As DataGridItem In ColumnGrid.Items
row = Columns.Rows(item.ItemIndex)
row("Show") = CType(item.Cells(1).Controls(1), CheckBox).Checked
row("Total") = CType(item.Cells(2).Controls(1), CheckBox).Checked
row("SortOrder") = CType(item.Cells(3).Controls(1), TextBox).Text
row("SortDirection") = CType(item.Cells(4).Controls(1), DropDownList).SelectedValue
op = CType(item.Cells(5).Controls(1), DropDownList).SelectedValue
row("SearchOperator") = op type = Columns.Rows(item.ItemIndex)("Type")
colName = Columns.Rows(item.ItemIndex)("ColumnName")
criteria = String.Empty If colName.Equals("Item ID") Then
control = item.Cells(6).FindControl("radComboBox1")
If Not IsNothing(control) Then
criteria = CType(control, RadComboBox).SelectedValue
End If
If op.ToUpper().Equals("BETWEEN") Then
control = item.Cells(6).FindControl("radComboBox2")
If Not IsNothing(control) Then
criteria = criteria & "~" & CType(control, RadComboBox).SelectedValue
End If
End If
Else
If type.Equals("DateTime") Or type.Equals("TimeSpan") Then
control = item.Cells(6).FindControl("radDatePicker1")
If Not IsNothing(control) Then
If Not IsNothing(CType(control, RadDatePicker).SelectedDate) Then
criteria = CType(control, RadDatePicker).SelectedDate.Value.ToLongDateString
End If
End If
If op.ToUpper().Equals("BETWEEN") Then
control = item.Cells(6).FindControl("radDatePicker2")
If Not IsNothing(control) Then
If Not IsNothing(CType(control, RadDatePicker).SelectedDate) Then
criteria = criteria & "~" & CType(control, RadDatePicker).SelectedDate.Value.ToLongDateString
End If
End If
End If
Else
control = item.Cells(6).FindControl("radTextBox1")
If Not IsNothing(control) Then
criteria = CType(control, RadTextBox).Text.Trim
End If
If op.ToUpper().Equals("BETWEEN") Then
control = item.Cells(6).FindControl("radTextBox2")
If Not IsNothing(control) Then
criteria = criteria & "~" & CType(control, RadTextBox).Text.Trim
End If
End If
End If
End If If Not String.IsNullOrEmpty(criteria) Then
row("SearchCriteria") = criteria
End If
Next
End Sub Sub OnTopCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
If TopCheck.Checked Then
Session(ReportName & ":Top") = TopText.Text
TopText.Visible = False
TopLabel.Text = Session(ReportName & ":Top")
TopLabel.Visible = True
Else
TopLabel.Visible = False
TopText.Text = Session(ReportName & ":Top")
TopText.Visible = True
Session(ReportName & ":Top") = -1
End If
Session(ReportName & ":ReportData") = Nothing
End Sub
加了没。。
Try
BindReportGrid()
'Initialize Report BindParameterGrid()
BindColumnGrid() Catch ex As Exception
Page.Response.Write("<Font Color='Red'>Can't load data: " + ex.Message + "</Font>")
Debug.Write("Report Engine error:" + ex.Message)
End Try
End If
ASP.NET之所以可以保持视图状态,就是靠了VIEWSTATE
你用了系统的PAGE后,VIEWSTATE有了,所以正常了。
VIEWSTATE并不是你要明确使用VIEWSTATE["XXX"]。
你了解下页面加载的过程就知道了。为什么页面POST后,没有赋值,但页面的值还是能够保持。你的DATAVIEW也是一回事。
具个简单的例子,你页面1个按钮,1个DATAGridVIEW,你在PAGE_LOAD中的!IsPostBack里绑定DATAGridVIEW控件,然后你点击按钮,你发现页面POST后流程里并没有绑定的操作,但DATAGRIDVIEW的数据还在。这就是由于VIEWSTATE的存在。
具个简单的例子,你页面1个按钮,1个DATAGridVIEW,你在PAGE_LOAD中的!IsPostBack里绑定DATAGridVIEW控件,然后你点击按钮,你发现页面POST后流程里并没有绑定的操作,但DATAGRIDVIEW的数据还在。这就是由于VIEWSTATE的存在。