R
RN1
A Form has a Label & a DataGrid. Note that the DataGrid comes after
the Label. The DataGrid has 11 columns - the 1st column is a
TemplateColumn, the 2nd column is a HyperLinkColumn, the 3rd column is
again a TemplateColumn (with a Label), columns 4 to 9 are
BoundColumns, the 10th column is a ButtonColumn (whose CommandName is
set to Delete) & finally the 11th column is a EditCommandColumn. The
1st column just displays the row no. (Container.ItemIndex + 1), the
2nd column displays the UserID of users (which are rendered as links
since it's a HyperLinkColumn), the 3rd column displays their full
name. Columns 4 to 9 display the address, city, state, country, zip &
phone no. of the users respectively.
When the Edit link in the 11th column is clicked (say, in the first
row of the DataGrid), columns 4 to 9 change to TextBoxes & the Edit
link gets replaced by Update & Cancel links. This is the
OnUpdateCommand event handler of the DataGrid:
--------------------------------------------------------------------------------
Sub UpdateUser(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
Dim txtZip As TextBox
Dim txtCity As TextBox
Dim txtPhone As TextBox
Dim txtState As TextBox
Dim txtCountry As TextBox
Dim txtAddress As TextBox
txtAddress = CType(ea.Item.Cells(3).Controls(0), TextBox)
txtCity = CType(ea.Item.Cells(4).Controls(0), TextBox)
txtState = CType(ea.Item.Cells(5).Controls(0), TextBox)
txtCountry = CType(ea.Item.Cells(6).Controls(0), TextBox)
txtZip = CType(ea.Item.Cells(7).Controls(0), TextBox)
txtPhone = CType(ea.Item.Cells(8).Controls(0), TextBox)
strSQL = "UPDATE tblUser.....WHERE UserID = '" &
CType(ea.Item.Cells(1).Controls(0), HyperLink).Text & "'"
Call FillDataGrid(ea.CommandName, strSQL)
dgUsers.EditItemIndex = -1
dgUsers.DataBind()
End Sub
--------------------------------------------------------------------------------
Note the 6 lines in the above code which are using CType. I am using
Controls(0) to retrieve the Text property of the TextBoxes. Those
lines retrieve the Text of the TextBoxes correctly but what I don't
understand is how does Controls(0) retrieve the correct Text of the
TextBoxes. If I use Controls(1) instead of Controls(0), then an error
gets generated. Why? Isn't Controls(0) the Label control & Controls(1)
the DataGrid in the Form?
When I add the following code (which loops through all the Controls in
the Form) in the Page_Load event function:
--------------------------------------------------------------------------------
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim ctrl As Control
Dim strSQL As String
strSQL = "SELECT * FROM tblUsers"
Call FillDataGrid(Nothing, strSQL)
For Each ctrl In Form.Controls
Response.Write(ctrl.GetType.ToString & " ---- " &
ctrl.ClientID & "<br>")
Next
End Sub
--------------------------------------------------------------------------------
The output of the above code is
--------------------------------------------------------------------------------
System.Web.UI.LiteralControl ---- ctl01
System.Web.UI.WebControls.Label ---- lblMessage
System.Web.UI.LiteralControl ---- ctl02
System.Web.UI.WebControls.DataGrid ---- dgUsers
System.Web.UI.LiteralControl ---- ctl03
--------------------------------------------------------------------------------
So why do I have to refer to the DataGrid as Controls(0) in the
OnUpdateCommand event handler of the DataGrid? Why not as Controls(1)?
Please note that I haven't shown the sub FillDataGrid for brevity.
Thanks,
Ron
the Label. The DataGrid has 11 columns - the 1st column is a
TemplateColumn, the 2nd column is a HyperLinkColumn, the 3rd column is
again a TemplateColumn (with a Label), columns 4 to 9 are
BoundColumns, the 10th column is a ButtonColumn (whose CommandName is
set to Delete) & finally the 11th column is a EditCommandColumn. The
1st column just displays the row no. (Container.ItemIndex + 1), the
2nd column displays the UserID of users (which are rendered as links
since it's a HyperLinkColumn), the 3rd column displays their full
name. Columns 4 to 9 display the address, city, state, country, zip &
phone no. of the users respectively.
When the Edit link in the 11th column is clicked (say, in the first
row of the DataGrid), columns 4 to 9 change to TextBoxes & the Edit
link gets replaced by Update & Cancel links. This is the
OnUpdateCommand event handler of the DataGrid:
--------------------------------------------------------------------------------
Sub UpdateUser(ByVal obj As Object, ByVal ea As
DataGridCommandEventArgs)
Dim txtZip As TextBox
Dim txtCity As TextBox
Dim txtPhone As TextBox
Dim txtState As TextBox
Dim txtCountry As TextBox
Dim txtAddress As TextBox
txtAddress = CType(ea.Item.Cells(3).Controls(0), TextBox)
txtCity = CType(ea.Item.Cells(4).Controls(0), TextBox)
txtState = CType(ea.Item.Cells(5).Controls(0), TextBox)
txtCountry = CType(ea.Item.Cells(6).Controls(0), TextBox)
txtZip = CType(ea.Item.Cells(7).Controls(0), TextBox)
txtPhone = CType(ea.Item.Cells(8).Controls(0), TextBox)
strSQL = "UPDATE tblUser.....WHERE UserID = '" &
CType(ea.Item.Cells(1).Controls(0), HyperLink).Text & "'"
Call FillDataGrid(ea.CommandName, strSQL)
dgUsers.EditItemIndex = -1
dgUsers.DataBind()
End Sub
--------------------------------------------------------------------------------
Note the 6 lines in the above code which are using CType. I am using
Controls(0) to retrieve the Text property of the TextBoxes. Those
lines retrieve the Text of the TextBoxes correctly but what I don't
understand is how does Controls(0) retrieve the correct Text of the
TextBoxes. If I use Controls(1) instead of Controls(0), then an error
gets generated. Why? Isn't Controls(0) the Label control & Controls(1)
the DataGrid in the Form?
When I add the following code (which loops through all the Controls in
the Form) in the Page_Load event function:
--------------------------------------------------------------------------------
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim ctrl As Control
Dim strSQL As String
strSQL = "SELECT * FROM tblUsers"
Call FillDataGrid(Nothing, strSQL)
For Each ctrl In Form.Controls
Response.Write(ctrl.GetType.ToString & " ---- " &
ctrl.ClientID & "<br>")
Next
End Sub
--------------------------------------------------------------------------------
The output of the above code is
--------------------------------------------------------------------------------
System.Web.UI.LiteralControl ---- ctl01
System.Web.UI.WebControls.Label ---- lblMessage
System.Web.UI.LiteralControl ---- ctl02
System.Web.UI.WebControls.DataGrid ---- dgUsers
System.Web.UI.LiteralControl ---- ctl03
--------------------------------------------------------------------------------
So why do I have to refer to the DataGrid as Controls(0) in the
OnUpdateCommand event handler of the DataGrid? Why not as Controls(1)?
Please note that I haven't shown the sub FillDataGrid for brevity.
Thanks,
Ron