Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 18th, 2007, 03:14 PM
Authorized User
 
Join Date: Nov 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reference a textbox in a formview

Could someone tell me why I can not reference a textbox which is in a FormView, in a ContentPlaceHolder of the master page. I can access a textbox if it is not inside the FormView, but inside the FormView is returned a null reference. I have included the code I have be using to try this out and maybe you could spot what I am doing wrong.

Thanks

Andy M

' Sample code to get a reference to a textbox control inside a FormView inside a ContentPlaceHolder

Dim mpContentPlaceHolder As ContentPlaceHolder
Dim mpFormView As FormView
Dim mpTextBox As TextBox
mpContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
    mpFormView = CType(mpContentPlaceHolder.FindControl("FixtureFor mView"), FormView)
    If Not mpFormView Is Nothing Then
        mpTextBox = CType(mpFormView.FindControl("FixtureDateTextBox") , TextBox)
        If Not mpTextBox Is Nothing Then
            mpTextBox.Text = "TextBox Found!"
        End If
    End If
End If
 
Old September 18th, 2007, 03:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Pretty difficult to tell without knowing where it goes wrong. You have quite some defensive programming (e.g. checking if the object is Nothing or not) so I can't see where it goes wrong. Do you get a reference to the ContentPlaceHolder? Do you get a reference to the FormView?

If both questions can be answered with Yes, then the third is: is the FormView in a state where it has a TextBox? That is, when it's in ReadOnly mode, there are no text boxes.....

Hope this helps a little.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 18th, 2007, 03:57 PM
Authorized User
 
Join Date: Nov 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If it is any further help, I thought it might be because the textbox was in each of the Item/Edit/Insert Templates of the FormView, but I can access this textbox in its FormView when it is not inside a master page ContentPlaceHolder.

I have read that the master page and content page are merge when they are rendered, does this cause the controls in the content page to be hidden or protected in some way?

 
Old September 18th, 2007, 04:05 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yea, they are merged, but their controls are not hidden or protected. What happens is that the *client IDs* of the controls are changed, depending on their location in the hierarchy.

Maybe your code no longer works because it's being called at a wrong time / in a wrong location? From where are you calling this?

Is it possible to create a reproducible test page?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 18th, 2007, 04:14 PM
Authorized User
 
Join Date: Nov 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am doing this in the Page_Load event, this is because I have been experimenting.

The ContentPlaceHolder Client_ID is ct100_ContentPlaceHolder1.
The FormView is ct100_ContentPlaceHolder1_FixtureFormView
The Current mode of the FormView is ReadOnly.

This has come about because I was trying to get a Date Picker to work in a FormView for the insert and edit templates so that my users don't need to worry about date format. The problem is that all the sample date pickers I have found require to be implemented where master pages are not used. The javascript that is produced accesses the control and writes the date into it, but it doesn't work when the formView is in a master page.

Would it help if I attached the date picker code and you could see the original problem that started my investigation.

Well here it is anyway...

In the .aspx file is the FormView (I have provided relevant sample here:-

<InsertItemTemplate>
     <table>
        <tr>
           <td><strong>Date:</strong></td>
           <td>
               <asp:TextBox ID="FixtureDateTextBox" runat="server" Text='<%# Bind("FixtureDate") %>'>
               </asp:TextBox>
               <a href="javascript:calendar_window=window.open('cale ndar.aspx?formname=<%=Page.Master.FindControl("Con tentPlaceHolder1").FindControl("FixtureFormView"). FindControl("FixtureDateTextBox").ClientID%>',
                                    'calendar_window','width=180,height=200'); calendar_window.focus()">
               <img src="../Admin/Images/smallCalendar.gif" />
               </a>
            </td>
         </tr>


then in the calendar.aspx the javascript is created and stored in a liertal control :-

<script runat="server">
    Private Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim strjscript As String = "<script language=""javascript"">"
        strjscript &= "window.opener." & _
                   HttpContext.Current.Request.QueryString("formname" ) & ".value = '" & _
                   Calendar1.SelectedDate.ToString("dd-MMM-yyyy") & "';window.close();"
        strjscript = strjscript & "</scrip" & "t>" 'Don't Ask, Tool Bug
        Literal1.Text = strjscript
    End Sub

</script>
 
Old September 18th, 2007, 04:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:The Current mode of the FormView is ReadOnly.
I don't think there is a TextBox to access when the control is in ReadOnly mode during Page_Load.

Were you able to make it work with a ReadOnly FormView during Page_Load without a Master Page? Or where you calling the code during a different event?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 18th, 2007, 04:31 PM
Authorized User
 
Join Date: Nov 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I didn't try that. When I wrote to the Textbox in the FormView that was not in the master page I used the date picker code that I attached to my previous reply a moment ago so you may not have seen it. This was using the Calendar_SelectionChanged event generated when a date from the popup calendar was selected.

 
Old September 18th, 2007, 04:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And the Calendar is inside the Insert or Edit templates right?

If that's the case, it all makes sense. The FormView is in Edit or Insert mode, the calendar fires its event and inside the event handler you access the TextBox while *the FormView is still in Edit / Insert mode*.

However, during page load, there is no TextBox in the FormView.

Does this help?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old September 18th, 2007, 04:49 PM
Authorized User
 
Join Date: Nov 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for this ...

Yes the calendar is inside the insert template of the FormView.
The FormView is in Insert mode, I understand that.
The calendar fies its event, created the javascript, and that runs on the client and updates the textbox, but it doesn't.

Ignore the Page_Load event, that was me trying to see why the date was not being written to the txtbox and is not part of the application.

I still do not see why the date is not written into the textbox even when the FormView is in insert mode (although when I was debugging this the current mode of the FormView was still ReadOnly).

If it helps the artile where I got this date picker from was http://aspnet.4GuysFromRolla.com/articles/030202-1.aspx called 'Populating Form Inputs Using Calendar Control', at a glance you can see what I implemented.

 
Old September 18th, 2007, 04:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:(although when I was debugging this the current mode of the FormView was still ReadOnly).
I would say that's an interesting clue. Apparently, you think it's in Edit mode, but it isn't. In ReadOnly mode the edit controls won't be there.

So, I would look at a way to get the FormView to be in Insert / Edit mode. You can set the DefaultMode programmatically, or postpone calling your code until the FormView is in Edit / Insert mode.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004





Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlighting a textbox in a formview amatytfc ASP.NET 2.0 Basics 0 October 12th, 2007 03:13 PM
textbox initialization in Formview ekwong ASP.NET 2.0 Basics 2 November 17th, 2006 08:30 PM
Populate a Textbox in a Formview timeware ASP.NET 2.0 Basics 0 September 29th, 2006 10:08 AM
reference textbox values in FORMSVIEW templates alexdcosta ASP.NET 2.0 Basics 0 June 22nd, 2006 05:49 AM
Masked TextBox & formatting TextBox melvik C# 1 September 22nd, 2003 11:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.