Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RN1 - 07 Mar 2008 12:49 GMT
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
bloodsugarsuckerfish - 07 Mar 2008 14:03 GMT
> 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
[quoted text clipped - 83 lines]
>
> 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
RN1 - 07 Mar 2008 14:40 GMT
On Mar 7, 7:03 pm, bloodsugarsuckerfish
<bloodsugarsuckerf...@googlemail.com> wrote:

> > 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
[quoted text clipped - 100 lines]
>
> - 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
Mufaka - 15 Mar 2008 16:00 GMT
> 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
[quoted text clipped - 83 lines]
>
> Ron
Controls(0) is referring to the first control *within* a cell of the
selected item from the DataGrid.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.