可重用的数据网格,使用viewstate项目作为SQL语句/生命周期问题 [英] reusable datagrid, using viewstate items as for SQL statements / problems with life cycle

查看:65
本文介绍了可重用的数据网格,使用viewstate项目作为SQL语句/生命周期问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:


我正在尝试在标准数据网格上创建一个变体,只有在按下某些按钮后才会显示

数据网格。这个原因是因为我想为几个表使用相同的数据网格,

并且想法是按钮事件存储SQL select

语句和视图状态项中的SQL更新语句,可以为所有datagrid事件(分页和编辑)重用



但是我似乎在理解

页面的生命周期时遇到了问题,这就是我想在这里寻求帮助的原因。

我一直得到的错误信息是:

对象引用未设置为对象的实例。,总是

指的是:

dgStaffOptions.DataSource = myDataSet.Tables(" mytable"


(包含在名为showdatagrid()的子项中 - ''dgStaffOptions''是ID

的数据网格。


下面是代码,我为了清晰起见而简单(只有第一个

按钮有效)。 :标签和我在bug测试中介绍了各种reponse.write行

元素。


用户控件中包含的HTML端(counter1u.ascx):


<%@ Control Language =" vb" AutoEventWireup =" false"
Codebehind =" counter1u.ascx.vb" Inherits =" qmsnet.counter1u"

TargetSchema =" http://schemas.microsoft.com/intellisense/ie5" %>

< h1>员工管理 - 标准选项< / h1>

< table>

< TBODY>

< tr>

< td vAlign =" top">

< TABLE id =" Table1" CELLSPACING = QUOT 1 QUOT; CELLPADDING = QUOT 1 QUOT; width =" 300"

border =" 0">

< TR>

< TD>< p>选项< / p>< / TD>

< TD>< p>更改< / p>< / TD>

< / TR>

< TR>

< TD>< p>职位< / p>< / TD>

< TD align = " center">< asp:button id =" Btn_Postions" Text =" Edit"

runat =" server">< / asp:button>< / TD>

< / TR>

< TR>

< TD>< p>公司< / p>< / TD>

< TD align =" center">< asp:button id =" Btn_Company" Text =" Edit"

runat =" server">< / asp:button>< / TD>

< / TR>

< / TABLE>

< asp:label id =" lbltest" Runat =" server">< / asp:label>< br>

< asp:label id =" lbltest2" Runat =" server">< / asp:label>

< / td>

< td vAlign =" top" width =" 600">

< asp:datagrid id =" dgStaffOptions" RUNAT = QUOT;服务器" Runat =" server"
OnUpdateCommand =" EditDataGrid_Update"

OnCancelCommand =" EditDataGrid_Cancel" OnEditCommand =" EditDataGrid_Edit"

OnPageIndexChanged =" PageChange" AllowPaging = QUOT;真" PageSize =" 4"

autogeneratecolumns =" False" Width =" 400px">

< Columns>

< asp:BoundColumn DataField =" ID" ReadOnly =" True"

HeaderText ="#">< / asp:BoundColumn>

< asp:BoundColumn DataField =" Options"

HeaderText =" Options">< / asp:BoundColumn>

< asp:EditCommandColumn ItemStyle-CssClass =" myListItem"

ButtonType =" LinkBut​​ton" UPDATETEXT = [更新" HeaderText =" Edit"

CancelText =" Cancel" EditText =" Edit">< / asp:EditCommandColumn>

< / Columns>

< / asp:datagrid>

< / td>

< / tr>

< / TBODY>

< / table>


代码隐藏方(counter1u.ascx.vb):


导入MySql.Data.MySqlClient

导入qmsnet.test2

公共类counter1u

继承System.Web.UI.UserControl


#Region" Web窗体设计器生成的代码


''Web窗体设计器需要此调用。

< System.Diagnostics.DebuggerStepThrough()> Private Sub

InitializeComponent()

End Sub

受保护的事件lbltest As System.Web.UI.WebControls.Label

受保护的事件Btn_Postions为

System.Web.UI.WebControls.Button

受保护的事件Btn_Company为

System.Web .UI.WebControls.Button

受保护的事件dgStaffOptions As

System.Web.UI.WebControls.DataGrid

受保护的事件lbltest2 As System.Web .UI.WebControls.Label


''注意:Web需要以下占位符声明

表单设计器。

''不要删除或移动它。

私有designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Init

''CODEGEN:Web表单设计器需要此方法调用

''不要修改它使用代码编辑器。

InitializeComponent()

结束子


#End Region

将myConnection变暗为MySqlConnection

Dim myDataAdapter As MySqlDataAdapter

Dim myDataSet As DataSet

Dim strSQLSelect As String

Dim strSQLUpdate As String

Private Sub Page_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

myConnection = New MySqlConnection(" server = mysql.mydomain.com;

用户id = root;密码=密码;数据库= mybase; pooling = false;")

如果不是Page.IsPostBack那么

strSQLSelect ="选择ID,选项来自qmsPositions订单

by Options" ;

strSQLUpdate ="更新"


否则

strSQLSelect = ViewState(" StrSQLSelect")

strSQLUpdate = ViewState(" StrSQLUpdate")

结束如果

''Response.Write(" load:"& strSQLSelect)

LoadDataFromDB()

End Sub

Private Sub Page_Prerender(ByVal sender As System.Object,ByVal e

As System.EventArgs)处理MyBase.PreRender

Viewstate(" strSQLSelect")= strSQLSelect

ViewState(" strSQLUpdate")= strSQLUpdate
lbltest.Text = strSQLSelect

lbltest2.Text = strSQLUpdate

End Sub

Sub LoadDataFromDB()

按选项排序"

myDataAdapter = New MySqlDataAdapter(strSQLSelect,
myConnection)

myDataSet =新数据集

Response.Write(" load3:" &安培; strSQLSelect)

myDataAdapter.Fill(myDataSet," mytable")

End Sub

Private Sub Btn_Postions_Click(ByVal sender As System.Object, ByVal
$ b $ as As System.EventArgs)处理Btn_Postions.Click

strSQLSelect ="选择ID,选项来自qmsPositions订购

选项"

strSQLUpdate ="更新qmsPositions设置选项=''columnvalue''

其中ID = idvalue"

Showdatagrid()


End Sub

Private Sub Btn_Company_Click(ByVal sender As System.Object,ByVal
$ b $ as As System.EventArgs)处理Btn_Company.Click
< br $>
End Sub

Sub PageChange(ByVal sender As Object,ByVal e As

DataGridPageChangedEventArgs)

dgStaffOptions.CurrentPageIndex = e.NewPageIndex

Showdatagrid()

End Sub

Sub Showdatagrid()

dgStaffOptions。 DataSource = myDataSet.Tables(" mytable")

dgStaffOptions.DataBind()

End Sub

Sub EditDataGrid_Edit(ByVal Sender As Object,ByVal E As

DataGridCommandEventArgs)

dgStaffOptions.EditItemIndex = E.Item.ItemIndex

Showdatagrid()

End Sub

Sub EditDataGrid_Cancel (ByVal Sender As Object,ByVal E As

DataGridCommandEventArgs)

dgStaffOptions.EditItemIndex = -1

Showdatagrid()

End Sub

Sub EditDataGrid_Update(ByVal Sender As Object,ByVal E As

DataGridCommandEventArgs)

Dim IDint As String = E.Item。单元格(0).Text

Dim Options As TextBox = E.Item.Cells(1).Controls(0)

Dim SQLtemp As String

Dim objCommand As MySqlCommand

SQLtemp = Replace(strSQLUpdate," columnvalue",Options.Text)

SQLtemp = Replace(SQLtemp," idvalue",IDint)

objCommand = New MySqlCommand(SQLtemp,myConnection)

试试

myConnection.Open()

objCommand.ExecuteNonQuery()

myConnection.Close()

Catch Ex As Exception>
Response.Write("< p>< strong>发生错误:< / strong> " &

Ex.ToString()& "< / p为H." &安培; vbCrLf)


最后

myConnection.Close()

结束尝试

LoadDataFromDB()


dgStaffOptions.EditItemIndex = -1

Showdatagrid()

结束子


结束课


=====


提前收到包裹!!


TB

Hi All:

I am trying to create a variation on the standard datagrid, whereby the
datagrid is only shown after pressing some buttons. This reason for
this is that I would like to use the same datagrid for several tables,
and the idea is that the button events store the the SQL select
statement and the SQL update statement in view state items, which can
the be reused for all the datagrid events (paging and editing).

However I seem to have a problem understanding the life cycle of the
page, which is why I would like to ask for some help here.

The error message I keep getting is:
"Object reference not set to an instance of an object.", always
referring to the line:
dgStaffOptions.DataSource = myDataSet.Tables("mytable"

(contained in a sub called showdatagrid() - ''dgStaffOptions'' is the ID
of the datagrid).

Below is the code, which I have simplied for clarity (only the first
button works). The asp:labels and the various reponse.write lines are
elements I have introduced during the bug testing.

HTML side contained in a user control (counter1u.ascx):

<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="counter1u.ascx.vb" Inherits="qmsnet.counter1u"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<h1>Staff Management - Standard options</h1>
<table>
<TBODY>
<tr>
<td vAlign="top">
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300"
border="0">
<TR>
<TD><p>Options</p></TD>
<TD><p>Change</p></TD>
</TR>
<TR>
<TD><p>Positions</p></TD>
<TD align="center"><asp:button id="Btn_Postions" Text="Edit"
runat="server"></asp:button></TD>
</TR>
<TR>
<TD><p>Company</p></TD>
<TD align="center"><asp:button id="Btn_Company" Text="Edit"
runat="server"></asp:button></TD>
</TR>
</TABLE>
<asp:label id="lbltest" Runat="server"></asp:label><br>
<asp:label id="lbltest2" Runat="server"></asp:label>
</td>
<td vAlign="top" width="600">
<asp:datagrid id="dgStaffOptions" runat="server" Runat="server"
OnUpdateCommand="EditDataGrid_Update"
OnCancelCommand="EditDataGrid_Cancel" OnEditCommand="EditDataGrid_Edit"
OnPageIndexChanged="PageChange" AllowPaging="True" PageSize="4"
autogeneratecolumns="False" Width="400px">
<Columns>
<asp:BoundColumn DataField="ID" ReadOnly="True"
HeaderText="#"></asp:BoundColumn>
<asp:BoundColumn DataField="Options"
HeaderText="Options"></asp:BoundColumn>
<asp:EditCommandColumn ItemStyle-CssClass="myListItem"
ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid>
</td>
</tr>
</TBODY>
</table>

Code-behind side (counter1u.ascx.vb):

Imports MySql.Data.MySqlClient
Imports qmsnet.test2
Public Class counter1u
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

''This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents lbltest As System.Web.UI.WebControls.Label
Protected WithEvents Btn_Postions As
System.Web.UI.WebControls.Button
Protected WithEvents Btn_Company As
System.Web.UI.WebControls.Button
Protected WithEvents dgStaffOptions As
System.Web.UI.WebControls.DataGrid
Protected WithEvents lbltest2 As System.Web.UI.WebControls.Label

''NOTE: The following placeholder declaration is required by the Web
Form Designer.
''Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
''CODEGEN: This method call is required by the Web Form Designer
''Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Dim myConnection As MySqlConnection
Dim myDataAdapter As MySqlDataAdapter
Dim myDataSet As DataSet
Dim strSQLSelect As String
Dim strSQLUpdate As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myConnection = New MySqlConnection("server=mysql.mydomain.com;
user id=root; password=password; database=mybase; pooling=false;")
If Not Page.IsPostBack Then
strSQLSelect = "Select ID, Options from qmsPositions order
by Options"
strSQLUpdate = "Update"

Else
strSQLSelect = ViewState("StrSQLSelect")
strSQLUpdate = ViewState("StrSQLUpdate")

End If
''Response.Write("load:" & strSQLSelect)
LoadDataFromDB()

End Sub
Private Sub Page_Prerender(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.PreRender
Viewstate("strSQLSelect") = strSQLSelect
ViewState("strSQLUpdate") = strSQLUpdate
lbltest.Text = strSQLSelect
lbltest2.Text = strSQLUpdate

End Sub
Sub LoadDataFromDB()
Order by Options"
myDataAdapter = New MySqlDataAdapter(strSQLSelect,
myConnection)
myDataSet = New DataSet
Response.Write("load3:" & strSQLSelect)
myDataAdapter.Fill(myDataSet, "mytable")
End Sub
Private Sub Btn_Postions_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Btn_Postions.Click
strSQLSelect = "Select ID, Options from qmsPositions Order by
Options"
strSQLUpdate = "Update qmsPositions set Options = ''columnvalue''
where ID = idvalue"
Showdatagrid()

End Sub
Private Sub Btn_Company_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Btn_Company.Click

End Sub
Sub PageChange(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
dgStaffOptions.CurrentPageIndex = e.NewPageIndex
Showdatagrid()

End Sub
Sub Showdatagrid()
dgStaffOptions.DataSource = myDataSet.Tables("mytable")
dgStaffOptions.DataBind()
End Sub
Sub EditDataGrid_Edit(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
dgStaffOptions.EditItemIndex = E.Item.ItemIndex
Showdatagrid()
End Sub
Sub EditDataGrid_Cancel(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
dgStaffOptions.EditItemIndex = -1
Showdatagrid()
End Sub
Sub EditDataGrid_Update(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs)
Dim IDint As String = E.Item.Cells(0).Text
Dim Options As TextBox = E.Item.Cells(1).Controls(0)
Dim SQLtemp As String
Dim objCommand As MySqlCommand
SQLtemp = Replace(strSQLUpdate, "columnvalue", Options.Text)
SQLtemp = Replace(SQLtemp, "idvalue", IDint)
objCommand = New MySqlCommand(SQLtemp, myConnection)
Try
myConnection.Open()
objCommand.ExecuteNonQuery()
myConnection.Close()
Catch Ex As Exception
Response.Write("<p><strong>An Error Occurred:</strong> " &
Ex.ToString() & "</p>" & vbCrLf)

Finally
myConnection.Close()
End Try
LoadDataFromDB()

dgStaffOptions.EditItemIndex = -1
Showdatagrid()
End Sub

End Class

=====

Thanks a bundle in advance!!

TB

推荐答案

我在这个问题上并没有吸引很多人,也许是因为我有这么多的b $ b包括这么多代码。但没有它,我不觉得我能正确解释我的

案例。


任何回复都会受到高度赞赏。

提前致谢。


Trym

I am not getting a lot of takers on this one, perhaps because I have
included so much code. But without it, I didn''t feel I could explain my
case properly.

Any replies will higly appreciated.

Thanks in advance.

Trym


Hi Trym,


再次欢迎。我简要地看了一下你的代码。你得到的错误是

基本上说myDataSet.Tables(" mytable")在你想要将它绑定到datagrid的时候什么都没有。它与页面无关

生命周期。


如果你有VS使用调试器来逐步查看代码,看看是否

myDataSet.Tables(" mytable")什么都不是,或者只是添加一个if语句:


如果不是myDataSet什么都不是,而且myDataSet.Tables(" mytable")也不是

没有然后

dgStaffOptions.DataSource = myDataSet.Tables(" mytable")

dgStaffOptions.DataBind()

Else

Response.Write(此阶段的数据集为空)

结束如果


如果你得到DataSet为空的消息,再次使用

调试器查看是否在再次指向

之前调用方法LoadDataFromDB。


让我知道你使用调试器的距离。


-

HTH,

Phillip Williams
http://www.s ocietopia.net
http://www.webswapp.com

" TB"写道:
Hi Trym,

Welcome again. I looked briefly at your code. The error you get is
basically saying that myDataSet.Tables("mytable") was nothing at the time you
tried to bind it to the datagrid. It has nothing to do with the page
lifecycle.

If you have VS use the debugger to step through the code to see if
myDataSet.Tables("mytable") is nothing or simply add an if statement:

If not myDataSet is nothing andAlso not myDataSet.Tables("mytable") is
nothing Then
dgStaffOptions.DataSource = myDataSet.Tables("mytable")
dgStaffOptions.DataBind()
Else
Response.Write("The DataSet at this stage is empty")
End If

If you get the message that the DataSet is empty, step again using the
Debugger to see if the method LoadDataFromDB is called before you reached to
that point again.

Let me know how far you get using the debugger.

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"TB" wrote:
我没有在这个上获得很多人,也许是因为我已经包含了这么多代码。但没有它,我不觉得我能正确解释我的
案件。

任何回复都将受到高度赞赏。

提前致谢。

尝试
I am not getting a lot of takers on this one, perhaps because I have
included so much code. But without it, I didn''t feel I could explain my
case properly.

Any replies will higly appreciated.

Thanks in advance.

Trym



非常感谢您回复我的留言。


我已经插入了你提供的一些小改动的代码:

Iinstead of:

"如果不是myDataSet什么也没有,也不是myDataSet.Tables(" mytable" )

什么都没有然后


我写道:

如果不是myDataSet什么都不是myDataSet.Tables(" ; mytable")是

没什么然后


结果就是在第一次运行时,即没有按下

btnPosition按钮,因此没有任何回发,您插入的

条件为假,因此此阶段的数据集是

为空消息被打印出来。


按下按钮和回发后几乎同样的事情发生了

因此是真的,区别在于第一次运行时行

" Response.Write(" load3:"& strSQLSelect)"

里面的LoadDataFromDB确实返回:

" load3:选择ID,选项从qmsPositions顺序选项"

但第二次(按下按钮和Page.IsPostBack = true)

相同的代码行仅返回:

" load3:",

即StrSQLSelect =""。这导致我通过调试器发现,
表示Private Sub Page_Prerender根本没有运行,而不是第一次运行,而不是在回发期间运行。我不是一个ace程序员但是

我应该认为如果Private Sub Page_Prerender被解雇了,那么

Response.Write(" load3:&& amp; ; strSQLSelect)" line应该在回发期间返回相同的

结果,因为StrSQLSelect变量将包含ViewState(StrSQLSelect)项的值




这可能是导致手头解决

问题的路径的开始吗?


TB

Thanks a lot for replying to my message.

I have inserted the code you had provided with one small change:
Iinstead of:
"If not myDataSet is nothing and Also not myDataSet.Tables("mytable")
is nothing Then"

I wrote:
"If Not myDataSet Is Nothing And Not myDataSet.Tables("mytable") Is
Nothing Then"

The result was that on the first run, i.e. without pressing the
btnPosition button and therefore without any postback, your inserted
condition was false and therefore the "The DataSet at this stage is
empty" message was printed.

Almost same thing happened after pressing the button and postback
therefore was true, the difference being that on the first run the line
"Response.Write("load3:" & strSQLSelect)"
inside the LoadDataFromDB did return:
"load3:Select ID, Options from qmsPositions order by Options"
but the second time (pressing the button and Page.IsPostBack = true)
that same code line returned only:
"load3:",
i.e StrSQLSelect = "". That lead me to discover through the debugger,
that that the Private Sub Page_Prerender does not run at all, not the
first time and not during postback. I am not not an ace programmer but
I should think that if the Private Sub Page_Prerender had fired, the
"Response.Write("load3:" & strSQLSelect)" line should returned the same
result during postback because the StrSQLSelect variable would have
contained the value of the ViewState("StrSQLSelect") item.

Could this be the beginning of the path leading to the solution of the
problem at hand?

TB


这篇关于可重用的数据网格,使用viewstate项目作为SQL语句/生命周期问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆