Asp.net 无法加载viewstate,将控件放入gridview标题中
现在我的更新面板中有一些控件,它们可以更改gridview中的数据。它们工作得很好,但是我想把这些控件放在gridview的标题中会很酷。但是当我将它们添加到标题中并点击按钮/运行函数时,我得到了加载viewstate失败错误。我在网上读了一些文章,但还是没能让它发挥作用Asp.net 无法加载viewstate,将控件放入gridview标题中,asp.net,vb.net,Asp.net,Vb.net,现在我的更新面板中有一些控件,它们可以更改gridview中的数据。它们工作得很好,但是我想把这些控件放在gridview的标题中会很酷。但是当我将它们添加到标题中并点击按钮/运行函数时,我得到了加载viewstate失败错误。我在网上读了一些文章,但还是没能让它发挥作用 Function toptable() Dim reader As SqlDataReader cmd.Parameters.AddWithValue("@yeartoget", DropDow
Function toptable()
Dim reader As SqlDataReader
cmd.Parameters.AddWithValue("@yeartoget", DropDownList1.SelectedValue)
cmd.Parameters.AddWithValue("@mode", RadioButtonList1.SelectedValue)
If TextBox1.Text = "" Then
Dim d As Date = Date.Today
TextBox1.Text = d.AddDays(-1)
End If
Dim pyear As Date
Dim pyear1 As Date
pyear = TextBox1.Text
Dim year1 As Int16
year1 = (pyear.Year - DropDownList1.SelectedValue)
If pyear.Year <> Now.Year Then
pyear1 = (pyear.AddYears(-(year1 + 1)))
Else
pyear1 = (pyear.AddYears(-year1))
End If
cmd.Parameters.AddWithValue("@current", TextBox1.Text)
cmd.Parameters.AddWithValue("@search", pyear1)
cmd.CommandText = "asofworking"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = conn
cmd.CommandTimeout = 300
conn.Open()
reader = cmd.ExecuteReader()
Dim myTable As DataTable = New DataTable()
myTable.Load(reader)
conn.Close()
Dim currentppax As New DataColumn
currentppax = New DataColumn("AVGPPAX", GetType(Double))
currentppax.Expression = "IIf([CurrentPAX] = 0, 0, [CurrentSales] / [CurrentPAX])"
Dim selectedppax As New DataColumn
selectedppax = New DataColumn("AVGpPAX1", GetType(Double))
selectedppax.Expression = "asofSales / asofPAX"
Dim projsales As New DataColumn
projsales = New DataColumn("ProjSales", GetType(Double))
projsales.Expression = "IIF([PercentSales] = 0, [CurrentSales], [CurrentSales] / [PercentSales])"
Dim projpax As New DataColumn
projpax = New DataColumn("ProjPAX", GetType(Double))
projpax.Expression = "IIF([PercentPAX] = 0, [CurrentPAX], [CurrentPAX] / [percentpax] )"
Dim remainingsales As New DataColumn
remainingsales = New DataColumn("remainingsales", GetType(Double))
remainingsales.Expression = "ProjSales - currentsales"
Dim remainingpax As New DataColumn
remainingpax = New DataColumn("remainingpax", GetType(Double))
remainingpax.Expression = "Projpax - currentpax"
Dim movementpax As New DataColumn
movementpax = New DataColumn("movementPAX", GetType(Double))
movementpax.Expression = "IIF([ASOFPAX] = 0, 2,(CurrentPAX / [ASOFPAX]))-1"
Dim movementsales As New DataColumn
movementsales = New DataColumn("movementsales", GetType(Double))
movementsales.Expression = "IIF([ASOFsales] = 0, 2,(Currentsales / [ASOFsales]))-1"
myTable.Columns.Add(selectedppax)
myTable.Columns.Add(currentppax)
myTable.Columns.Add(projsales)
myTable.Columns.Add(projpax)
myTable.Columns.Add(remainingsales)
myTable.Columns.Add(remainingpax)
myTable.Columns.Add(movementsales)
myTable.Columns.Add(movementpax)
GridView1.DataSource = myTable
GridView1.DataBind()
Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
'spanned cell that will span the columns I don't want to give the additional header
Dim compare As TableCell = New TableHeaderCell()
compare.ColumnSpan = 8
row.Cells.Add(compare)
'compare.Text = DropDownList1.Text
compare.controls.add(DropDownList1.Text)
'spanned cell that will span the columns i want to give the additional header
Dim current As TableCell = New TableHeaderCell()
current.ColumnSpan = 3
'current.Text = Year(Now())
row.Cells.Add(current)
current.controls.add(textbox1)
Dim Projection As TableCell = New TableHeaderCell()
Projection.ColumnSpan = 4
Projection.Text = "Projections"
row.Cells.Add(Projection)
Dim Movements As TableCell = New TableHeaderCell()
Movements.ColumnSpan = 2
Movements.Text = "Movement"
row.Cells.Add(Movements)
'Add the new row to the gridview as the master header row
'A table is the only Control (index[0]) in a GridView
DirectCast(GridView1.Controls(0), Table).Rows.AddAt(0, row)
Label2.Text = RadioButtonList1.Text + " - " + TextBox1.Text
If CheckBox1.Checked Then
If Label2.Text <> Label4.Text Then
bottomtable()
End If
End If
End Function
函数toptable()
作为SqlDataReader的Dim读取器
cmd.Parameters.AddWithValue(“@yeartoget”,DropDownList1.SelectedValue)
cmd.Parameters.AddWithValue(“@mode”,RadioButtonList1.SelectedValue)
如果TextBox1.Text=”“,则
Dim d As Date=日期。今天
TextBox1.Text=d.AddDays(-1)
如果结束
日期
Dim pyear1作为日期
pyear=TextBox1.Text
第1年为国际16年
year1=(pyear.Year-DropDownList1.SelectedValue)
如果现在是一年,然后是一年
pyear1=(pyear.AddYears(-(year1+1)))
其他的
pyear1=(pyear.AddYears(-year1))
如果结束
cmd.Parameters.AddWithValue(“@current”,TextBox1.Text)
cmd.Parameters.AddWithValue(“@search”,pyear1)
cmd.CommandText=“asofworking”
cmd.CommandType=CommandType.storedProcess
cmd.Connection=conn
cmd.CommandTimeout=300
康涅狄格州公开赛
reader=cmd.ExecuteReader()
将myTable设置为DataTable=新建DataTable()
myTable.Load(读卡器)
康涅狄格州关闭
Dim currentppax作为新数据列
currentpax=新数据列(“AVGPPAX”,GetType(Double))
CurrentPAX.Expression=“IIf([CurrentPAX]=0,0,[CurrentSales]/[CurrentPAX])”
Dim选择PPAX作为新数据列
selectedppax=newdatacolumn(“AVGpPAX1”,GetType(Double))
selectedppax.Expression=“asofSales/asofPAX”
Dim projsales作为新数据列
projsales=newdatacolumn(“projsales”,GetType(Double))
projsales.Expression=“IIF([PercentSales]=0,[CurrentSales],[CurrentSales]/[PercentSales])”
Dim projpax作为新数据列
projpax=newdatacolumn(“projpax”,GetType(Double))
projpax.Expression=“IIF([PercentPAX]=0,[CurrentPAX],[CurrentPAX]/[PercentPAX])”
Dim remainingsales作为新数据列
remainingsales=newdatacolumn(“remainingsales”,GetType(Double))
remaingsales.Expression=“ProjSales-currentsales”
Dim remainingpax作为新数据列
remainingpax=newdatacolumn(“remainingpax”,GetType(Double))
remainingpax.Expression=“Projpax-currentpax”
Dim movementpax作为新数据列
movementpax=newdatacolumn(“movementpax”,GetType(Double))
movementpax.Expression=“IIF([ASOFPAX]=0,2,(CurrentPAX/[ASOFPAX]))-1”
Dim movementsales作为新数据列
movementsales=新数据列(“movementsales”,GetType(Double))
movementsales.Expression=“IIF([ASOFsales]=0,2,(Currentsales/[ASOFsales])-1”
myTable.Columns.Add(selectedppax)
myTable.Columns.Add(currentppax)
myTable.Columns.Add(项目销售)
myTable.Columns.Add(projpax)
myTable.Columns.Add(remainingsales)
myTable.Columns.Add(remainingpax)
myTable.Columns.Add(移动销售)
myTable.Columns.Add(movementpax)
GridView1.DataSource=myTable
GridView1.DataBind()
将行调整为新的GridViewRow(0,-1,DataControlRowType.Header,DataControlRowState.Normal)
'跨越单元格,该单元格将跨越我不想提供额外标题的列
Dim compare As TableCell=新建TableHeaderCell()
比较.ColumnSpan=8
行.单元格.添加(比较)
'compare.Text=DropDownList1.Text
compare.controls.add(DropDownList1.Text)
'跨越单元格,将跨越我要提供附加标题的列
Dim current As TableCell=新建TableHeaderCell()
current.ColumnSpan=3
'current.Text=Year(Now())
行.单元格.添加(当前)
current.controls.add(textbox1)
作为TableCell的Dim投影=新建TableHeaderCell()
Projection.ColumnSpan=4
Projection.Text=“Projections”
行.单元格.添加(投影)
当TableCell=新建TableHeaderCell()时变暗移动
移动。列跨度=2
Movements.Text=“移动”
行.单元格.添加(移动)
'将新行作为主标题行添加到gridview
'表格是GridView中唯一的控件(索引[0])
DirectCast(GridView1.Controls(0),Table.Rows.AddAt(0,row)
Label2.Text=RadioButtonList1.Text+“-”+TextBox1.Text
如果复选框1.选中,则
如果标签2.Text标签4.Text,则
下表()
如果结束
如果结束
端函数
添加的控件是否需要viewstate?如果没有,可以为添加的控件关闭viewstate,这将解决问题。否则,您需要在页面加载viewstate之前的适当时间添加控件。指
您将看到viewstate是在Page_Load()期间加载的。因此,要正确加载viewstate,您需要在Init状态下加载控件(Page_Init())。我认为您还可以为gridview的load事件创建一个覆盖,并在调用base load方法之前将它们加载到那里。在没有上下文的情况下很难判断发生了什么,但这可能与在数据绑定后以及ViewState恢复后向gridview添加动态控件有关。如果必须动态创建,请将该代码放入PageInit中,稍后再放入数据绑定
DataGrid布局中的任何内容似乎都不是动态的,但是,如果在标记中呈现,您的生活可能会轻松得多。在每次回发时都必须重新创建动态创建的控件,而不仅仅是在将网格绑定到其数据源时。T
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
'add your controls here to `e.Row.Cells`, but you don't have access to the datasource here'
End If
End Sub
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
'here you have access to the grid's datasource and your created controls'
End If
End Sub