Controls


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
 
Ad

Advertisements

B

bloodsugarsuckerfish

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

Hey Ron,

The reason that controls(0) works is because you are getting the first
control of the Cell within the datagrid row, not the first control of
the page.

So, CType(ea.Item.Cells(3).Controls(0), TextBox)

Gets the 1st control in the 4th cell of the current row of the
datagrid.

Hope this helps

Rich
 
R

RN1

Hey Ron,

The reason that controls(0) works is because you are getting the first
control of the Cell within the datagrid row, not the first control of
the page.

So, CType(ea.Item.Cells(3).Controls(0), TextBox)

Gets the 1st control in the 4th cell of the current row of the
datagrid.

Hope this helps

Rich- Hide quoted text -

- Show quoted text -

Yes...yes....you are correct. I had got it wrong. Controls(0) refers
to the 1st control in the CELL of the current row & not the 1st
control on the page. Thanks for pointing it out.

Now since Controls(0) refers to the 1st control in the cell, then that
means when a BoundColumn is used & the DataGrid is in the editable
mode, then to refer to the TextBox control in that cell, Controls(0)
HAS to be used ALWAYS since, unlike a TemplateColumn which can house
more than one control in its ItemTemplate (& thus can have multiple
controls in one cell), a BoundColumn cannot be coupled with any other
control. Am I correct?

Please correct me if I am wrong.

Regards,

Ron
 
Ad

Advertisements

M

Mufaka

RN1 said:
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
Controls(0) is referring to the first control *within* a cell of the
selected item from the DataGrid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top