控制? [英] Controls?

查看:40
本文介绍了控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码从SQL Server 2005中检索数据

数据库表&在DataGrid中显示它:


< script runat =" server">

Sub Page_Load(ByVal obj As Object,ByVal ea As EventArgs)

Dim dSet As DataSet

Dim sqlConn As SqlConnection

Dim sqlDapter As SqlDataAdapter

sqlConn =新的SqlConnection(数据源= AD \SQLEXPRESS;初始

目录= MyDB;集成安全性=真)

sqlDapter =新的SqlDataAdapter(" SELECT * FROM用户,sqlConn


dSet =新数据集()

sqlDapter.Fill(dSet," Users")


dgUsers.DataSource = dSet.Tables(" Users")。DefaultView

dgUsers.DataBind()

End Sub


Sub EditUsers(ByVal obj As Object,ByVal ea As

DataGridCommandEventArgs)

dgUsers.EditItemIndex = ea.Item.ItemIndex

dgUsers.DataBind()


Response.Write(Control0:")

Response.Write(ea.Item.Cells(0).C ontrols(0))


Response.Write("< br> Control1:")

Response.Write(ea.Item.Cells( 0).Controls(1))


Response.Write("< br> Control2:")

Response.Write(ea.Item .Cells(0).Controls(2))


''Response.Write("< br> Control3:")

'' Response.Write(ea.Item.Cells(0).Controls(3))

End Sub

< / script>


< form runat =" server">

< asp:DataGrid ID =" dgUsers" OnEditCommand =" EditUsers"

AutoGenerateColumns =" false" runat =" server">

< Columns>

< asp:TemplateColumn HeaderText =" NAME">

< ; ItemTemplate>

< asp:Label ID =" lblName" runat =" server"><%#

Container.DataItem(" FirstName")%>& nbsp;<%#

容器。 DataItem(" LastName")%>< / asp:Label>

< / ItemTemplate>

< / asp:TemplateColumn>

< asp:BoundColumn HeaderText =" ADDRESS" DataField =" Address" />

< asp:BoundColumn HeaderText =" CITY" DataField =" City" />

< asp:BoundColumn HeaderText =" STATE" DataField =" State" />

< asp:BoundColumn HeaderText =" ZIP" DataField =" Zip" />

< asp:EditCommandColumn HeaderText =" EDIT" CancelText =" CANCEL"

EditText =" EDIT" UpdateText =" UPDATE" />

< / Columns>

< / asp:DataGrid>

< / form>


假设DataGrid显示5条记录。当我点击编辑

链接对应于,例如,第一条记录,除了所有字段

(可编辑和不可编辑)在DataGrid中显示,前面的3对

的Response.Write行在子EditUsers中。产生

以下输出:


Control0:System.Web.UI.LiteralControl

Control1:System.Web.UI。 WebControls.Label

Control2:System.Web.UI.LiteralControl


有人可以解释一下为什么/前三对怎么做

Response.Write子行中的行EditUsers产生上面的输出?


其次,请注意子地中的最后一对Response.Write行

" EditUsers"被评论。如果这对Response.Write行是

取消注释,则会生成以下错误:


指定的参数超出有效值范围。

参数名称:index


指向这一行:

Response.Write(ea.Item.Cells( 0).Controls(3))


我无法完全理解为什么上面的Response.Write会产生上面提到的

错误。有人可以解释一下

错误的原因吗?


谢谢,


Arpan

Consider the following code which retrieves data from a SQL Server 2005
DB table & displays it in a DataGrid:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection("Data Source=AD\SQLEXPRESS;Initial
Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users", sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

dgUsers.DataSource = dSet.Tables("Users").DefaultView
dgUsers.DataBind()
End Sub

Sub EditUsers(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
dgUsers.EditItemIndex = ea.Item.ItemIndex
dgUsers.DataBind()

Response.Write(Control0: ")
Response.Write(ea.Item.Cells(0).Controls(0))

Response.Write("<br>Control1: ")
Response.Write(ea.Item.Cells(0).Controls(1))

Response.Write("<br>Control2: ")
Response.Write(ea.Item.Cells(0).Controls(2))

''Response.Write("<br>Control3: ")
''Response.Write(ea.Item.Cells(0).Controls(3))
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditUsers"
AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"><%#
Container.DataItem("FirstName") %>&nbsp;<%#
Container.DataItem("LastName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="ADDRESS" DataField="Address"/>
<asp:BoundColumn HeaderText="CITY" DataField="City"/>
<asp:BoundColumn HeaderText="STATE" DataField="State"/>
<asp:BoundColumn HeaderText="ZIP" DataField="Zip"/>
<asp:EditCommandColumn HeaderText="EDIT" CancelText="CANCEL"
EditText="EDIT" UpdateText="UPDATE"/>
</Columns>
</asp:DataGrid>
</form>

Assume that the DataGrid displays 5 records. When I click the "Edit"
link corresponding to the, say, 1st record, apart from all fields
(editable & non-editable) displayed in the DataGrid, the first 3 pairs
of Response.Write lines within the sub "EditUsers" produce the
following output:

Control0: System.Web.UI.LiteralControl
Control1: System.Web.UI.WebControls.Label
Control2: System.Web.UI.LiteralControl

Can someone please explain me why/how do the first 3 pairs of
Response.Write lines in the sub "EditUsers" produce the above output?

Secondly, note that the last pair of Response.Write lines in the sub
"EditUsers" is commented. If this pair of Response.Write lines are
uncommented, then the following error gets generated:

Specified argument was out of the range of valid values.
Parameter name: index

pointing to this line:

Response.Write(ea.Item.Cells(0).Controls(3))

I couldn''t exactly follow why the above Response.Write generates the
above-mentioned error. Can someone please explain me the cause of the
error?

Thanks,

Arpan

推荐答案

首先:

您将返回Object而不是它的值。

假设你知道这一点,Literal Controls通常是HTML标签,而Web

控件是标准的ASP.NET控件。


其次:

因为对象不存在!

如果你想查看下一个单元格,你需要递增单元格

number。

Arpan"写道:
Firstly:
You are returning the Object not its value.
Assuming you know this, Literal Controls are generally HTML Tags while Web
Controls are standard ASP.NET controls.

Secondly:
Because the Object does not exist!
If you are trying to look at the next cell you need to increment the cell
number.
"Arpan" wrote:

考虑以下代码从SQL Server 2005中检索数据

数据库表&在DataGrid中显示它:


< script runat =" server">

Sub Page_Load(ByVal obj As Object,ByVal ea As EventArgs)

Dim dSet As DataSet

Dim sqlConn As SqlConnection

Dim sqlDapter As SqlDataAdapter

sqlConn =新的SqlConnection(数据源= AD \SQLEXPRESS;初始

目录= MyDB;集成安全性=真)

sqlDapter =新的SqlDataAdapter(" SELECT * FROM用户,sqlConn


dSet =新数据集()

sqlDapter.Fill(dSet," Users")


dgUsers.DataSource = dSet.Tables(" Users")。DefaultView

dgUsers.DataBind()

End Sub


Sub EditUsers(ByVal obj As Object,ByVal ea As

DataGridCommandEventArgs)

dgUsers.EditItemIndex = ea.Item.ItemIndex

dgUsers.DataBind()


Response.Write(Control0:")

Response.Write(ea.Item.Cells(0).Controls(0))


Response.Write("< br> Control1:")

Response.Write(ea.Item.Cells(0).Controls(1))


Response.Write("< br> Control2:")

Response.Write(ea.Item.Cells(0).Controls(2 ))


''Response.Write("< br> Control3:")

''Response.Write(ea.Item.Cells (0).Controls(3))

结束子

< / script>


< form runat =" ; server">

< asp:DataGrid ID =" dgUsers" OnEditCommand =" EditUsers"

AutoGenerateColumns =" false" runat =" server">

< Columns>

< asp:TemplateColumn HeaderText =" NAME">

< ; ItemTemplate>

< asp:Label ID =" lblName" runat =" server"><%#

Container.DataItem(" FirstName")%<%#

Container.DataItem(" LastName" )%>< / asp:标签>

< / ItemTemplate>

< / asp:TemplateColumn>

< asp :BoundColumn HeaderText =" ADDRESS" DataField =" Address" />

< asp:BoundColumn HeaderText =" CITY" DataField =" City" />

< asp:BoundColumn HeaderText =" STATE" DataField =" State" />

< asp:BoundColumn HeaderText =" ZIP" DataField =" Zip" />

< asp:EditCommandColumn HeaderText =" EDIT" CancelText =" CANCEL"

EditText =" EDIT" UpdateText =" UPDATE" />

< / Columns>

< / asp:DataGrid>

< / form>


假设DataGrid显示5条记录。当我点击编辑

链接对应于,例如,第一条记录,除了所有字段

(可编辑和不可编辑)在DataGrid中显示,前面的3对

的Response.Write行在子EditUsers中。产生

以下输出:


Control0:System.Web.UI.LiteralControl

Control1:System.Web.UI。 WebControls.Label

Control2:System.Web.UI.LiteralControl


有人可以解释一下为什么/前三对怎么做

Response.Write子行中的行EditUsers产生上面的输出?


其次,请注意子地中的最后一对Response.Write行

" EditUsers"被评论。如果这对Response.Write行是

取消注释,则会生成以下错误:


指定的参数超出有效值范围。

参数名称:index


指向这一行:

Response.Write(ea.Item.Cells( 0).Controls(3))


我无法完全理解为什么上面的Response.Write会产生上面提到的

错误。有人可以解释一下

错误的原因吗?


谢谢,


Arpan

Consider the following code which retrieves data from a SQL Server 2005
DB table & displays it in a DataGrid:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection("Data Source=AD\SQLEXPRESS;Initial
Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users", sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

dgUsers.DataSource = dSet.Tables("Users").DefaultView
dgUsers.DataBind()
End Sub

Sub EditUsers(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
dgUsers.EditItemIndex = ea.Item.ItemIndex
dgUsers.DataBind()

Response.Write(Control0: ")
Response.Write(ea.Item.Cells(0).Controls(0))

Response.Write("<br>Control1: ")
Response.Write(ea.Item.Cells(0).Controls(1))

Response.Write("<br>Control2: ")
Response.Write(ea.Item.Cells(0).Controls(2))

''Response.Write("<br>Control3: ")
''Response.Write(ea.Item.Cells(0).Controls(3))
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditUsers"
AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"><%#
Container.DataItem("FirstName") %<%#
Container.DataItem("LastName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="ADDRESS" DataField="Address"/>
<asp:BoundColumn HeaderText="CITY" DataField="City"/>
<asp:BoundColumn HeaderText="STATE" DataField="State"/>
<asp:BoundColumn HeaderText="ZIP" DataField="Zip"/>
<asp:EditCommandColumn HeaderText="EDIT" CancelText="CANCEL"
EditText="EDIT" UpdateText="UPDATE"/>
</Columns>
</asp:DataGrid>
</form>

Assume that the DataGrid displays 5 records. When I click the "Edit"
link corresponding to the, say, 1st record, apart from all fields
(editable & non-editable) displayed in the DataGrid, the first 3 pairs
of Response.Write lines within the sub "EditUsers" produce the
following output:

Control0: System.Web.UI.LiteralControl
Control1: System.Web.UI.WebControls.Label
Control2: System.Web.UI.LiteralControl

Can someone please explain me why/how do the first 3 pairs of
Response.Write lines in the sub "EditUsers" produce the above output?

Secondly, note that the last pair of Response.Write lines in the sub
"EditUsers" is commented. If this pair of Response.Write lines are
uncommented, then the following error gets generated:

Specified argument was out of the range of valid values.
Parameter name: index

pointing to this line:

Response.Write(ea.Item.Cells(0).Controls(3))

I couldn''t exactly follow why the above Response.Write generates the
above-mentioned error. Can someone please explain me the cause of the
error?

Thanks,

Arpan


因为对象不存在!


但为什么对象不存在?当DataGrid处于可编辑的

模式时,它将显示5列 - NAME,ADDRESS,CITY,STATE& ZIP加上

EDIT列(显示2个链接 - 更新& CANCEL

该行已点击其EDIT链接)即总共有6个

列。现在,如果我添加行


Response.Write(" Control Count:"& ea.Item.Controls.Count)


在EditUsers中sub(将由EditCommand事件引发),

控制计数到达6.那么对象怎么不存在&




Response.Write(ea.Item.Cells(0).Controls(3))


生成错误?请澄清一下。


谢谢,


Arpan


Neverlyn写道:
Because the Object does not exist!

But why doesn''t the object exist? When the DataGrid is in the editable
mode, it will display 5 columns - NAME, ADDRESS, CITY, STATE & ZIP plus
the EDIT column (which will display the 2 links - UPDATE & CANCEL for
that row whose EDIT link has been clicked) i.e. in total there are 6
columns. Now if I add the line

Response.Write("Control Count: " & ea.Item.Controls.Count)

in the "EditUsers" sub (which will be raised by the EditCommand event),
the control count comes to 6. So how come the object doesn''t exist &
the line

Response.Write(ea.Item.Cells(0).Controls(3))

generates the error? Please clarify this.

Thanks,

Arpan

Neverlyn wrote:

首先:

您返回的是Object而不是它的值。

假设您知道这一点,Literal Controls通常是HTML标签而Web

控件是标准的ASP.NET控件。


其次:

因为对象不存在!

如果您正在尝试查看下一个单元格,则需要递增单元格

数字。


Arpan写道:
Firstly:
You are returning the Object not its value.
Assuming you know this, Literal Controls are generally HTML Tags while Web
Controls are standard ASP.NET controls.

Secondly:
Because the Object does not exist!
If you are trying to look at the next cell you need to increment the cell
number.
"Arpan" wrote:

考虑以下代码从SQL Server 2005中检索数据

数据库表&在DataGrid中显示它:


< script runat =" server">

Sub Page_Load(ByVal obj As Object,ByVal ea As EventArgs)

Dim dSet As DataSet

Dim sqlConn As SqlConnection

Dim sqlDapter As SqlDataAdapter

sqlConn =新的SqlConnection(数据源= AD \SQLEXPRESS;初始

目录= MyDB;集成安全性=真)

sqlDapter =新的SqlDataAdapter(" SELECT * FROM用户,sqlConn


dSet =新数据集()

sqlDapter.Fill(dSet," Users")


dgUsers.DataSource = dSet.Tables(" Users")。DefaultView

dgUsers.DataBind()

End Sub


Sub EditUsers(ByVal obj As Object,ByVal ea As

DataGridCommandEventArgs)

dgUsers.EditItemIndex = ea.Item.ItemIndex

dgUsers.DataBind()


Response.Write(Control0:")

Response.Write(ea.Item.Cells(0).Controls(0 ))


Response.Write("< br> Control1:")

Response.Write(ea.Item.Cells(0)。控件(1))


Response.Write("< br> Control2:")

Response.Write(ea.Item.Cells( 0).Controls(2))


''Response.Write("< br> Control3:")

''Response.Write (ea.Item.Cells(0).Controls(3))

End Sub

< / script>


< form runat =" server">

< asp:DataGrid ID =" dgUsers" OnEditCommand =" EditUsers"

AutoGenerateColumns =" false" runat =" server">

< Columns>

< asp:TemplateColumn HeaderText =" NAME">

< ; ItemTemplate>

< asp:Label ID =" lblName" runat =" server"><%#

Container.DataItem(" FirstName")%<%#

Container.DataItem(" LastName" )%>< / asp:标签>

< / ItemTemplate>

< / asp:TemplateColumn>

< asp :BoundColumn HeaderText =" ADDRESS" DataField =" Address" />

< asp:BoundColumn HeaderText =" CITY" DataField =" City" />

< asp:BoundColumn HeaderText =" STATE" DataField =" State" />

< asp:BoundColumn HeaderText =" ZIP" DataField =" Zip" />

< asp:EditCommandColumn HeaderText =" EDIT" CancelText =" CANCEL"

EditText =" EDIT" UpdateText =" UPDATE" />

< / Columns>

< / asp:DataGrid>

< / form>


假设DataGrid显示5条记录。当我点击编辑

链接对应于,例如,第一条记录,除了所有字段

(可编辑和不可编辑)在DataGrid中显示,前面的3对

的Response.Write行在子EditUsers中。产生

以下输出:


Control0:System.Web.UI.LiteralControl

Control1:System.Web.UI。 WebControls.Label

Control2:System.Web.UI.LiteralControl


有人可以解释一下为什么/前三对怎么做

Response.Write子行中的行EditUsers产生上面的输出?


其次,请注意子地中的最后一对Response.Write行

" EditUsers"被评论。如果这对Response.Write行是

取消注释,则会生成以下错误:


指定的参数超出有效值范围。

参数名称:index


指向这一行:

Response.Write(ea.Item.Cells( 0).Controls(3))


我无法完全理解为什么上面的Response.Write会产生上面提到的

错误。有人可以解释一下

错误的原因吗?


谢谢,


Arpan
Consider the following code which retrieves data from a SQL Server 2005
DB table & displays it in a DataGrid:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter

sqlConn = New SqlConnection("Data Source=AD\SQLEXPRESS;Initial
Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users", sqlConn)

dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")

dgUsers.DataSource = dSet.Tables("Users").DefaultView
dgUsers.DataBind()
End Sub

Sub EditUsers(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
dgUsers.EditItemIndex = ea.Item.ItemIndex
dgUsers.DataBind()

Response.Write(Control0: ")
Response.Write(ea.Item.Cells(0).Controls(0))

Response.Write("<br>Control1: ")
Response.Write(ea.Item.Cells(0).Controls(1))

Response.Write("<br>Control2: ")
Response.Write(ea.Item.Cells(0).Controls(2))

''Response.Write("<br>Control3: ")
''Response.Write(ea.Item.Cells(0).Controls(3))
End Sub
</script>

<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditUsers"
AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"><%#
Container.DataItem("FirstName") %<%#
Container.DataItem("LastName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="ADDRESS" DataField="Address"/>
<asp:BoundColumn HeaderText="CITY" DataField="City"/>
<asp:BoundColumn HeaderText="STATE" DataField="State"/>
<asp:BoundColumn HeaderText="ZIP" DataField="Zip"/>
<asp:EditCommandColumn HeaderText="EDIT" CancelText="CANCEL"
EditText="EDIT" UpdateText="UPDATE"/>
</Columns>
</asp:DataGrid>
</form>

Assume that the DataGrid displays 5 records. When I click the "Edit"
link corresponding to the, say, 1st record, apart from all fields
(editable & non-editable) displayed in the DataGrid, the first 3 pairs
of Response.Write lines within the sub "EditUsers" produce the
following output:

Control0: System.Web.UI.LiteralControl
Control1: System.Web.UI.WebControls.Label
Control2: System.Web.UI.LiteralControl

Can someone please explain me why/how do the first 3 pairs of
Response.Write lines in the sub "EditUsers" produce the above output?

Secondly, note that the last pair of Response.Write lines in the sub
"EditUsers" is commented. If this pair of Response.Write lines are
uncommented, then the following error gets generated:

Specified argument was out of the range of valid values.
Parameter name: index

pointing to this line:

Response.Write(ea.Item.Cells(0).Controls(3))

I couldn''t exactly follow why the above Response.Write generates the
above-mentioned error. Can someone please explain me the cause of the
error?

Thanks,

Arpan


我很少使用Microsoft网格(使用第三方网格),但我理解它的方式是:Item equates行和单元格相当于列。

因此,您只返回模板项中包含的控件。


查看地址中的控件你应该使用的字段:

Response.Write(ea.Item.Cells(1).Controls(0))


要查看控件的数量你的单元格:(应为3)

Response.Write(a ??控制计数:a ??& ea.Item.Cells(0).Controls.Count)


你明白我的意思吗?

" Arpan"写道:
I rarely use the Microsoft grid (Use 3rd Party Grids), but the way I
understand it is: Item equates to the Row and Cell equates to the Column.
Therefore you are only returning the controls contained in your template item.

To see the controls in your Address field you should use:
Response.Write(ea.Item.Cells(1).Controls(0))

To see the number of controls in your cell: (Should be 3)
Response.Write(a??Control Count: a?? & ea.Item.Cells(0).Controls.Count)

Do you understand what I am getting at?
"Arpan" wrote:

因为对象不存在!
Because the Object does not exist!



但是为什么这个对象不存在?当DataGrid处于可编辑的

模式时,它将显示5列 - NAME,ADDRESS,CITY,STATE& ZIP加上

EDIT列(显示2个链接 - 更新& CANCEL

该行已点击其EDIT链接)即总共有6个

列。现在,如果我添加行


Response.Write(" Control Count:"& ea.Item.Controls.Count)


在EditUsers中sub(将由EditCommand事件引发),

控制计数到达6.那么对象怎么不存在&




Response.Write(ea.Item.Cells(0).Controls(3))


生成错误?请澄清一下。


谢谢,


Arpan


Neverlyn写道:


But why doesn''t the object exist? When the DataGrid is in the editable
mode, it will display 5 columns - NAME, ADDRESS, CITY, STATE & ZIP plus
the EDIT column (which will display the 2 links - UPDATE & CANCEL for
that row whose EDIT link has been clicked) i.e. in total there are 6
columns. Now if I add the line

Response.Write("Control Count: " & ea.Item.Controls.Count)

in the "EditUsers" sub (which will be raised by the EditCommand event),
the control count comes to 6. So how come the object doesn''t exist &
the line

Response.Write(ea.Item.Cells(0).Controls(3))

generates the error? Please clarify this.

Thanks,

Arpan

Neverlyn wrote:


首先:

您返回的是Object而不是它的值。

假设您知道这一点,Literal Controls通常是HTML标签而Web

控件是标准的ASP.NET控件。


其次:

因为对象不存在!

如果您正在尝试查看下一个单元格,则需要递增单元格

数字。

Arpan写道:
Firstly:
You are returning the Object not its value.
Assuming you know this, Literal Controls are generally HTML Tags while Web
Controls are standard ASP.NET controls.

Secondly:
Because the Object does not exist!
If you are trying to look at the next cell you need to increment the cell
number.
"Arpan" wrote:

考虑以下代码从SQL Server 2005中检索数据

数据库表&在DataGrid中显示它:

>

< script runat =" server">

Sub Page_Load(ByVal obj As Object ,ByBal ea As EventArgs)

Dim dSet As DataSet

Dim sqlConn As SqlConnection

Dim sqlDapter As SqlDataAdapter

>

sqlConn =新的SqlConnection(数据源= AD \SQLEXPRESS;初始

目录= MyDB;集成安全性=真)

sqlDapter =新的SqlDataAdapter(" SELECT * FROM Users",sqlConn)

>

dSet = New DataSet()

sqlDapter .Fill(dSet," Users")

>

dgUsers.DataSource = dSet.Tables(" Users")。DefaultView

dgUsers.DataBind()

End Sub

>

Sub EditUsers(ByVal obj As Object,ByVal ea As

DataGridCommandEventArgs)

dgUsers.E ditItemIndex = ea.Item.ItemIndex

dgUsers.DataBind()

>

Response.Write(Control0:")

Response.Write(ea.Item.Cells(0).Controls(0))

>

Response.Write("< br> ; Control1:")

Response.Write(ea.Item.Cells(0).Controls(1))

>

Response.Write("< br> Control2:")

Response.Write(ea.Item.Cells(0).Controls(2))

> ;

''Response.Write("< br> Control3:")

''Response.Write(ea.Item.Cells(0).Controls (3))

结束子

< / script>

>

< form runat = server>

< asp:DataGrid ID =" dgUsers" OnEditCommand =" EditUsers"

AutoGenerateColumns =" false" runat =" server">

< Columns>

< asp:TemplateColumn HeaderText =" NAME">

< ; ItemTemplate>

< asp:Label ID =" lblName" runat =" server"><%#

Container.DataItem(" FirstName")%<%#

Container.DataItem(" LastName" )%>< / asp:标签>

< / ItemTemplate>

< / asp:TemplateColumn>

< asp :BoundColumn HeaderText =" ADDRESS" DataField =" Address" />

< asp:BoundColumn HeaderText =" CITY" DataField =" City" />

< asp:BoundColumn HeaderText =" STATE" DataField =" State" />

< asp:BoundColumn HeaderText =" ZIP" DataField =" Zip" />

< asp:EditCommandColumn HeaderText =" EDIT" CancelText =" CANCEL"

EditText =" EDIT" UpdateText =" UPDATE" />

< / Columns>

< / asp:DataGrid>

< / form>

>

假设DataGrid显示5条记录。当我点击编辑

链接对应于,例如,第一条记录,除了所有字段

(可编辑和不可编辑)在DataGrid中显示,前面的3对

的Response.Write行在子EditUsers中。产生

以下输出:

>

Control0:System.Web.UI.LiteralControl

Control1: System.Web.UI.WebControls.Label

Control2:System.Web.UI.LiteralControl

>

有人可以解释一下为什么/如何在子EditUsers中的前3对

Response.Write行。产生上面的输出?

>

其次,请注意子中的最后一对Response.Write行

" EditUsers"被评论。如果这对Response.Write行是

取消注释,则会生成以下错误:

>

指定的参数不在有效值范围。

参数名称:index

>

指向此行:

>

Response.Write(ea.Item.Cells(0).Controls(3))

>

我不可能完全按照上面的Response.Write生成

上述错误的原因。有人可以解释一下

错误的原因吗?

>

谢谢,

>

Arpan

>

>
Consider the following code which retrieves data from a SQL Server 2005
DB table & displays it in a DataGrid:
>
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim dSet As DataSet
Dim sqlConn As SqlConnection
Dim sqlDapter As SqlDataAdapter
>
sqlConn = New SqlConnection("Data Source=AD\SQLEXPRESS;Initial
Catalog=MyDB;Integrated Security=True")
sqlDapter = New SqlDataAdapter("SELECT * FROM Users", sqlConn)
>
dSet = New DataSet()
sqlDapter.Fill(dSet, "Users")
>
dgUsers.DataSource = dSet.Tables("Users").DefaultView
dgUsers.DataBind()
End Sub
>
Sub EditUsers(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
dgUsers.EditItemIndex = ea.Item.ItemIndex
dgUsers.DataBind()
>
Response.Write(Control0: ")
Response.Write(ea.Item.Cells(0).Controls(0))
>
Response.Write("<br>Control1: ")
Response.Write(ea.Item.Cells(0).Controls(1))
>
Response.Write("<br>Control2: ")
Response.Write(ea.Item.Cells(0).Controls(2))
>
''Response.Write("<br>Control3: ")
''Response.Write(ea.Item.Cells(0).Controls(3))
End Sub
</script>
>
<form runat="server">
<asp:DataGrid ID="dgUsers" OnEditCommand="EditUsers"
AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="NAME">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"><%#
Container.DataItem("FirstName") %<%#
Container.DataItem("LastName") %></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="ADDRESS" DataField="Address"/>
<asp:BoundColumn HeaderText="CITY" DataField="City"/>
<asp:BoundColumn HeaderText="STATE" DataField="State"/>
<asp:BoundColumn HeaderText="ZIP" DataField="Zip"/>
<asp:EditCommandColumn HeaderText="EDIT" CancelText="CANCEL"
EditText="EDIT" UpdateText="UPDATE"/>
</Columns>
</asp:DataGrid>
</form>
>
Assume that the DataGrid displays 5 records. When I click the "Edit"
link corresponding to the, say, 1st record, apart from all fields
(editable & non-editable) displayed in the DataGrid, the first 3 pairs
of Response.Write lines within the sub "EditUsers" produce the
following output:
>
Control0: System.Web.UI.LiteralControl
Control1: System.Web.UI.WebControls.Label
Control2: System.Web.UI.LiteralControl
>
Can someone please explain me why/how do the first 3 pairs of
Response.Write lines in the sub "EditUsers" produce the above output?
>
Secondly, note that the last pair of Response.Write lines in the sub
"EditUsers" is commented. If this pair of Response.Write lines are
uncommented, then the following error gets generated:
>
Specified argument was out of the range of valid values.
Parameter name: index
>
pointing to this line:
>
Response.Write(ea.Item.Cells(0).Controls(3))
>
I couldn''t exactly follow why the above Response.Write generates the
above-mentioned error. Can someone please explain me the cause of the
error?
>
Thanks,
>
Arpan
>
>



这篇关于控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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