A
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">
<aspataGrid 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>
</aspataGrid>
</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
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">
<aspataGrid 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>
</aspataGrid>
</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