Wednesday, March 28, 2012

Where do I went wrong?

consider the following code

[u]Main page[/u]

<html>
<head>
<script language="Javascript">
function GetDate(CtrlName)
{
ChildWindow = window.open('Calendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName, "PopUpCalendar", "width=270,height=300,top=200,left=200,toolbars=no,scrollbars=no,status=yes,resizable=no");
}
</script>
</head>
<body>
<form runat="server">
<asp:TextBox id="txtStartDate" runat="server"></asp:TextBox>
Calendar
</form>
</body>
</html>

[u]Calendar.aspx[/u]

<%@dotnet.itags.org. Page Language="VB" Debug="true" %>
<script runat="server"
Public strFormName As String
Public strCtrlName As String
Public strSelectedDate As String

Sub Page_Load(Sender As Object, E As EventArgs)
If Not IsPostBack Then
myCalendar.SelectedDate = System.DateTime.Now()
ddlMonth.Items.FindByValue(DateTime.Now.Month).Selected = True
ddlYear.Items.FindByValue(DateTime.Now.Year).Selected = True
End If
strSelectedDate = myCalendar.SelectedDate.ToString("dd/MM/yyyy")
strFormName = Request.QueryString("FormName")
strCtrlName = Request.QueryString("CtrlName")
'response.Write(strFormName)
'response.Write(strCtrlName)
'Dim var = Request.Cookies("Uid").Value
'response.Write(var)
End Sub

Sub myCalendar_SelectionChanged(sender As Object, e As EventArgs)
strSelectedDate = myCalendar.SelectedDate.ToString("dd/MM/yyyy")
End Sub

Sub ddl_SelectedIndexChanged(Sender As Object, e As EventArgs)
myCalendar.VisibleDate = New DateTime(CInt(ddlYear.SelectedItem.Value), CInt(ddlMonth.SelectedItem.Value), 1)
End Sub

</script>
<html>
<head>
<script language="Javascript">
function ReturnDate()
{

window.opener.document.forms["<%= strFormName %>"].elements["<%= strCtrlName %>"].value = "<%= strSelectedDate %>";
window.close();

}

function Close()
{
window.close();
}
</script>
</head>
<body>
<form runat="server">
<table>
<tbody>
<tr>
<td align="middle">
Month:
<asp:DropDownList id="ddlMonth" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
Year:
<asp:DropDownList id="ddlYear" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="1970">1970</asp:ListItem>
<asp:ListItem Value="1971">1971</asp:ListItem>
<asp:ListItem Value="1972">1972</asp:ListItem>
<asp:ListItem Value="1973">1973</asp:ListItem>
<asp:ListItem Value="1974">1974</asp:ListItem>
<asp:ListItem Value="1975">1975</asp:ListItem>
<asp:ListItem Value="1976">1976</asp:ListItem>
<asp:ListItem Value="1977">1977</asp:ListItem>
<asp:ListItem Value="1978">1978</asp:ListItem>
<asp:ListItem Value="1979">1979</asp:ListItem>
<asp:ListItem Value="1980">1980</asp:ListItem>
<asp:ListItem Value="1981">1981</asp:ListItem>
<asp:ListItem Value="1982">1982</asp:ListItem>
<asp:ListItem Value="1983">1983</asp:ListItem>
<asp:ListItem Value="1984">1984</asp:ListItem>
<asp:ListItem Value="1985">1985</asp:ListItem>
<asp:ListItem Value="1986">1986</asp:ListItem>
<asp:ListItem Value="1987">1987</asp:ListItem>
<asp:ListItem Value="1988">1988</asp:ListItem>
<asp:ListItem Value="1989">1989</asp:ListItem>
<asp:ListItem Value="1990">1990</asp:ListItem>
<asp:ListItem Value="1991">1991</asp:ListItem>
<asp:ListItem Value="1992">1992</asp:ListItem>
<asp:ListItem Value="1993">1993</asp:ListItem>
<asp:ListItem Value="1994">1994</asp:ListItem>
<asp:ListItem Value="1995">1995</asp:ListItem>
<asp:ListItem Value="1996">1996</asp:ListItem>
<asp:ListItem Value="1997">1997</asp:ListItem>
<asp:ListItem Value="1998">1998</asp:ListItem>
<asp:ListItem Value="1999">1999</asp:ListItem>
<asp:ListItem Value="2000">2000</asp:ListItem>
<asp:ListItem Value="2001">2001</asp:ListItem>
<asp:ListItem Value="2002">2002</asp:ListItem>
<asp:ListItem Value="2003">2003</asp:ListItem>
<asp:ListItem Value="2003">2004</asp:ListItem>
<asp:ListItem Value="2004">2005</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="middle">
<asp:Calendar id="myCalendar" runat="server" BorderWidth="1px" BackColor="#FFFFCC" Width="220px" DayNameFormat="FirstLetter" ForeColor="#663399" Height="200px" Font-Size="8pt" Font-Names="Verdana" BorderColor="#FFCC66" ShowGridLines="True" OnSelectionChanged="myCalendar_SelectionChanged">
<SelectorStyle backcolor="#FFCC66"></SelectorStyle>
<NextPrevStyle font-size="9pt" forecolor="#FFFFCC"></NextPrevStyle>
<DayHeaderStyle height="1px" backcolor="#FFCC66"></DayHeaderStyle>
<SelectedDayStyle font-bold="True" backcolor="#CCCCFF"></SelectedDayStyle>
<TitleStyle font-size="9pt" font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></TitleStyle>
<OtherMonthDayStyle forecolor="#CC9966"></OtherMonthDayStyle>
</asp:Calendar>
</td>
</tr>
<tr>
<td align="middle">
<input id="btnReturnDate" onclick="Javascript:ReturnDate()" type="button" value="Select" runat="Server" />
<input id="btnCloseWindow" onclick="Javascript:Close()" type="button" value="Close" runat="Server" />
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>

I keep on getting error message saying that elements.txtStartDate is not an object, where do I went wrong?learningProcess, a big part of learning any programming or scripting environment is learning how to debug problems.

You will need to learn how to "plant" tests inside code. Such tests let you know whether your code is working the way you think it should.

For example, I can see that you have two problems with this line:

window.opener.document.forms["<%= strFormName %>"].elements["<%= strCtrlName %>"].value = "<%= strSelectedDate %>";
You should know this too, as this is where the error message was pointing. So, you need to break that code down into parts, and get each part working.

First, you can test whether your popup is correctly finding the field of the parent window. You should do this before you try passing values to it.
Try:

var parentDocument = window.opener.document;
if (parentDocument)
{
var parentItem = parentDocument.getElementById("<%= strCtrlName %>");
if (parentItem)
{
parentItem.value = "Success! This value comes from the popup";
}
else
{
alert( 'Could not find parentItem');
}
}
else
{
alert( 'Could not find parentDocument');
}

You can see that I've broken the Javascript down into parts. First, find the document. Then, find the document item. Then, pass a value to it. Each time, if my "find" fails, it will alert me. This will narrow down where in code the problem is occurring.

Replace your line of javascript with my javascript here, and let me know how you go.
I get the error message "Could not find parentItem". So this mean it couldn't find the txtStartDate? I have tried to change the code to

window.opener.document.forms["<%= strFormName %>"].txtStartDate.value = "<%= strSelectedDate %>";

and still I get error "txtStartDate is null or not any object
In your code, how are you assigning a value to strCtrlName?

Is it like this:
strCtrlName = txtStateDate.ID

If so, change it to
strCtrlName = txtStateDate.ClientID

Let me know how you go. If this works, I will explain the difference.
//Calling the Pop Up window
ChildWindow = window.open('Calendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName, "PopUpCalendar", "width=270,height=300,top=200,left=200,toolbars=no,scrollbars=no,status=yes,resizable=no");

I pass the value of the form name and

asp:TextBox id="txtStartDate"
using querystring method
'Calendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName

And under the Calendar.aspx / Child page
I uses Dim strCtrlName = Request.QueryString("CtrlName") to get the asp:TextBox Id.
I see.

But in the following line:

'Calendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName
how are you getting the value of CtrlName?

Here's why. Even though you have given the Textbox an ID of "txtStartDate", if you load your page and View > Source, you will see that the ID has probably changed. This is why the javascript in your popup cannot find the textbox you are specifying.

Hence, wherever it is that you're specifying CtrlName for the line shown above, you will need to change that line.

For example, you might have:

CtrlName = "txtStartDate"
This won't work because, as I mentioned, the ID will change by the time it gets to the browser.

You will need to change it to something like:

CtrlName = Page.FindControl( "txtStartDate" ).ClientID

Oops I left out the important.
I actually uses this code


<script>
function GetDate(CtrlName)
{
ChildWindow = window.open('Calendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName, "PopUpCalendar", "width=270,height=300,top=200,left=200,toolbars=no,scrollbars=no,status=yes,resizable=no");
}
<asp:TextBox id="txtStartDate" runat="server"></asp:TextBox>
Calendar

So you mean the ID of the asp:TextBox is dynamic and it changes from time to time?

Also let say I assign a id / name to <form>, it will however be represented as pagename_id or pagename:name. If I were to program it static should I write the pagename as well?
For example my page is testpage.aspx and the id for the form is formID. If I run the program and view source it stated id as id="testpage_fromID"
> So you mean the ID of the asp:TextBox is dynamic and it changes from time to time?

Yes, exactly right.

You give it an ID of txtStartDate. However, you might place it inside another control. In this case, by the time it is rendered as HTML, it might have an ID such as "__ctl0_txtStartDate". This latter ID is called the ClientID.

This is why the following line will not work:

Calendar

Instead, you will need to replace 'txtStartDate'. Here's how...

Change that line to read:

<a id="LinkToPopupCalendar" runat="server">Calendar</a>

This changes it into a control that we can work with in code. Now, in your Page_Load method, you dynamically assign this <a ...> tag its href value:
LinkToPopupCalendar.Attributes.Add( "href", "javascript:GetDate('" & txtStartDate.ClientID & "');" )

So, try that, and let me know how you go.
Nop, I still get back the same old error message

elements.txtStartDate is null or not an object

Do you want to have a copy of my source code?

Pls visit
http://www.vbcity.com/forums/topic.asp?tid=52436
and you can download the source code I attach with on the bottom of the page.

Thanks!
Nop it doesn't work either
I get back the same old error message

elements.txtStartDate is null or not an object

Do you want a copy of my source code so that you can understand my situation well as I may not explain properly due to my poor english?
Please visit
http://www.vbcity.com/forums/topic.asp?tid=52436
for the source code I attach with (on the bottom of the page)

Thanks!
I've uploaded a test page towww.edition3.com/developers/learningProcess.

Ithink that this is along the lines of what you want.

If so, the code for the two pages is available atwww.edition3.com/developers/learningProcess/calendar.zip.

If that's not what you want, please let me know now.
Sorry SomeNewKid2 as I couldn't reply to you that soon as I'm away from my PC for some moment.
However THANKS for helping me out. Yeah I got your code and it works perfectly so I will want to check that out. That is really kind of you for helping those who had problem as some people like me was new to this whole .net stuff.
*I don't know that we can write javascript in .net and Page.RegisterStartupScript( "ReturnDateScript", ReturnDateScript ) it until now.
Thanks for all your help and I really appriciate it. A thousand thanks from me.
Have a nice day and Happy New Year!!!!!!
When I try to run this and click on the "Calendar" link, I receive this error:

Microsoft JScript runtime error: Object expected

and the GetDate(); method is highlighted.

I'm using Visual Studio .NET 2002 and running this in debug mode.

Any information appreciated.
Well, I don't use VS.NET, so I can't say for sure.

However, the GetDate() function is generated and injected ... it does not actually "reside" on the page. Rather, the page must be rendered in order for the GetDate() function to be available.

I have no idea how the VS.NET debugger works, though I'd guess it steps through executing code. In which case, debugging would indeed break the page. The page must be rendered and flushed to the client in order for the GetDate() function to be available.

I suspect that the debugger is designed to debuglogic, not UI elements. With logic, youcan step though executing code, watching the calls and seeing the values of variables. But UI elements must be tested following a complete render of the page.

Hope this helps.

0 comments:

Post a Comment