Client side jscript issue

J

john

Working in Visual Studio

Objective: I am trying to have my server side vb code construct a file name
for use with my client side JavaScript code to then save the file to the
web. My saving to the web method is working with a preset test filename.

The issue I have is that when I put this JavaScript in the source code of an
asp:content holder of a masterpage container, it is not able to see any
server side controls, one of which is returning my filename I want to use.

In a standard HTML page where I worked out the code method it works fine, so
I know the method works.

It is only in asp:content page, where there is no HTML that I cannot read
the server side control.

I can read a HTMLInputText control in the asp:content page so long as it is
not set to runat="server", but then my server side vb code cannot see it to
write my file name to it for use with the JavaScript code.

I either need a way to read the server side control from the asp:content
page with JavaScript, or write to the HTMLInputText control not set to
runat="server" when the server responds, or a different approach that allows
me to get my filename in a way that the JavaScript can read it.

Thank you for any help,
John

Here is a snippet of the code up to where the error on page occurs:

<%@ Page Language="VB" MasterPageFile="MasterPage.master"
AutoEventWireup="false" MaintainScrollPositionOnPostback ="true"
CodeFile="APcontent.aspx.vb" Inherits="APcontent" title="Accounts Payable"
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

<script language="javascript" type="text/javascript">

// <!CDATA[

function File1_onclick() {

var Var1 = document.getElementById("TextBox1").value; 'ERROR OCCURS HERE

document.write(Var1);

//File saving code is here.

}

// ]]>

</script>

<asp:TextBox ID="TextBox1" runat="server"
Width="472px"></asp:TextBox>

</asp:Content>
 
N

nahid

Working in Visual Studio

Objective: I am trying to have my server side vb code construct a file name
for use with my client side JavaScript code to then save the file to the
web. My saving to the web method is working with a preset test filename.

The issue I have is that when I put this JavaScript in the source code of an
asp:content holder of a masterpage container, it is not able to see any
server side controls, one of which is returning my filename I want to use.

In a standard HTML page where I worked out the code method it works fine, so
I know the method works.

It is only in asp:content page, where there is no HTML that I cannot read
the server side control.

I can read a HTMLInputText control in the asp:content page so long as it is
not set to runat="server", but then my server side vb code cannot see it to
write my file name to it for use with the JavaScript code.

I either need a way to read the server side control from the asp:content
page with JavaScript, or write to the HTMLInputText control not set to
runat="server" when the server responds, or a different approach that allows
me to get my filename in a way that the JavaScript can read it.

Thank you for any help,
John

Here is a snippet of the code up to where the error on page occurs:

<%@ Page Language="VB" MasterPageFile="MasterPage.master"
AutoEventWireup="false" MaintainScrollPositionOnPostback ="true"
CodeFile="APcontent.aspx.vb" Inherits="APcontent" title="Accounts Payable"
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

<script language="javascript" type="text/javascript">

// <!CDATA[

function File1_onclick() {

var Var1 = document.getElementById("TextBox1").value; 'ERROR OCCURS HERE

document.write(Var1);

//File saving code is here.

}

// ]]>

</script>

<asp:TextBox ID="TextBox1" runat="server"
Width="472px"></asp:TextBox>

</asp:Content>

Check the view source of rendered file your are using master page so
TextBox1 are not render in clict side with name TextBox1...you will
see some prefix add with it...using IE dev tool bar find out the
render id of TextBox1 and then modify

var Var1 = document.getElementById("....something changes in render
find out from view source...TextBox1").value;
 
N

nahid

Working in Visual Studio

Objective: I am trying to have my server side vb code construct a file name
for use with my client side JavaScript code to then save the file to the
web. My saving to the web method is working with a preset test filename.

The issue I have is that when I put this JavaScript in the source code of an
asp:content holder of a masterpage container, it is not able to see any
server side controls, one of which is returning my filename I want to use.

In a standard HTML page where I worked out the code method it works fine, so
I know the method works.

It is only in asp:content page, where there is no HTML that I cannot read
the server side control.

I can read a HTMLInputText control in the asp:content page so long as it is
not set to runat="server", but then my server side vb code cannot see it to
write my file name to it for use with the JavaScript code.

I either need a way to read the server side control from the asp:content
page with JavaScript, or write to the HTMLInputText control not set to
runat="server" when the server responds, or a different approach that allows
me to get my filename in a way that the JavaScript can read it.

Thank you for any help,
John

Here is a snippet of the code up to where the error on page occurs:

<%@ Page Language="VB" MasterPageFile="MasterPage.master"
AutoEventWireup="false" MaintainScrollPositionOnPostback ="true"
CodeFile="APcontent.aspx.vb" Inherits="APcontent" title="Accounts Payable"
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

<script language="javascript" type="text/javascript">

// <!CDATA[

function File1_onclick() {

var Var1 = document.getElementById("TextBox1").value; 'ERROR OCCURS HERE

document.write(Var1);

//File saving code is here.

}

// ]]>

</script>

<asp:TextBox ID="TextBox1" runat="server"
Width="472px"></asp:TextBox>

</asp:Content>

Check the view source of rendered file your are using master page so
TextBox1 are not render in client side with name TextBox1...you will
see some prefix add with it...using IE dev tool bar find out the
render id of TextBox1 and then modify

var Var1 = document.getElementById("....something changes in render
find out from view source...TextBox1").value;
nahid
KAZ Software
http://www.kaz.com.bd
blog: http://nahidulkibria.blogspot.com
 
J

john

Thanks, that was it, it added "ctl00_ContentPlaceHolder1_" to the front of
the ID.


nahid said:
Working in Visual Studio

Objective: I am trying to have my server side vb code construct a file
name
for use with my client side JavaScript code to then save the file to the
web. My saving to the web method is working with a preset test filename.

The issue I have is that when I put this JavaScript in the source code of
an
asp:content holder of a masterpage container, it is not able to see any
server side controls, one of which is returning my filename I want to
use.

In a standard HTML page where I worked out the code method it works fine,
so
I know the method works.

It is only in asp:content page, where there is no HTML that I cannot read
the server side control.

I can read a HTMLInputText control in the asp:content page so long as it
is
not set to runat="server", but then my server side vb code cannot see it
to
write my file name to it for use with the JavaScript code.

I either need a way to read the server side control from the asp:content
page with JavaScript, or write to the HTMLInputText control not set to
runat="server" when the server responds, or a different approach that
allows
me to get my filename in a way that the JavaScript can read it.

Thank you for any help,
John

Here is a snippet of the code up to where the error on page occurs:

<%@ Page Language="VB" MasterPageFile="MasterPage.master"
AutoEventWireup="false" MaintainScrollPositionOnPostback ="true"
CodeFile="APcontent.aspx.vb" Inherits="APcontent" title="Accounts
Payable"
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

<script language="javascript" type="text/javascript">

// <!CDATA[

function File1_onclick() {

var Var1 = document.getElementById("TextBox1").value; 'ERROR OCCURS
HERE

document.write(Var1);

//File saving code is here.

}

// ]]>

</script>

<asp:TextBox ID="TextBox1" runat="server"
Width="472px"></asp:TextBox>

</asp:Content>

Check the view source of rendered file your are using master page so
TextBox1 are not render in client side with name TextBox1...you will
see some prefix add with it...using IE dev tool bar find out the
render id of TextBox1 and then modify

var Var1 = document.getElementById("....something changes in render
find out from view source...TextBox1").value;
nahid
KAZ Software
http://www.kaz.com.bd
blog: http://nahidulkibria.blogspot.com
 
M

mark4asp

Working in Visual Studio

Objective: I am trying to have my server side vb code construct a file name
for use with my client side JavaScript code to then save the file to the
web. My saving to the web method is working with a preset test filename.

The issue I have is that when I put this JavaScript in the source code of an
asp:content holder of a masterpage container, it is not able to see any
server side controls, one of which is returning my filename I want to use.

In a standard HTML page where I worked out the code method it works fine, so
I know the method works.

It is only in asp:content page, where there is no HTML that I cannot read
the server side control.

I can read a HTMLInputText control in the asp:content page so long as it is
not set to runat="server", but then my server side vb code cannot see it to
write my file name to it for use with the JavaScript code.

I either need a way to read the server side control from the asp:content
page with JavaScript, or write to the HTMLInputText control not set to
runat="server" when the server responds, or a different approach that allows
me to get my filename in a way that the JavaScript can read it.

Thank you for any help,
John

Here is a snippet of the code up to where the error on page occurs:

<%@ Page Language="VB" MasterPageFile="MasterPage.master"
AutoEventWireup="false" MaintainScrollPositionOnPostback ="true"
CodeFile="APcontent.aspx.vb" Inherits="APcontent" title="Accounts Payable"
%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

<script language="javascript" type="text/javascript">

// <!CDATA[

function File1_onclick() {

var Var1 = document.getElementById("TextBox1").value; 'ERROR OCCURS HERE

document.write(Var1);

//File saving code is here.

}

// ]]>

</script>

<asp:TextBox ID="TextBox1" runat="server"
Width="472px"></asp:TextBox>

</asp:Content>

The client-side code (i.e. the rendered HTML) will not actually have a
control called TextBox1 because using Master pages will result in a
prefix being added to that control. You need to take that into account
by modifying your javascript:

var Var1 = document.getElementById("<%=TextBox1.ClientID%>").value;

If this newsgroup had a penny for every time that query has been
answered...
 
M

mark4asp

Thanks, that was it, it added "ctl00_ContentPlaceHolder1_" to the front of
the ID.

No. Don't do it like that. If you change you code later you will regret
hard-coding a constant in there - VERY VERY BAD.

var Var1 = document.getElementById("<%=TextBox1.ClientID%>").value;
 
M

Mark Rae [MVP]

Check the view source of rendered file your are using master page so
TextBox1 are not render in client side with name TextBox1...you will
see some prefix add with it...using IE dev tool bar find out the
render id of TextBox1 and then modify

var Var1 = document.getElementById("....something changes in render
find out from view source...TextBox1").value;

PLEASE IGNORE THIS!!!

It is an extremely bad thing to do as there is no guarantee that the
rendered ID will always remain the same - almost *any* change to the layout
and structure of the markup has the potential to change these client-side
IDs...

Instead, use the method suggested by mark4asp.
 
S

Steven Cheng[MSFT]

Thanks for mark4asp's input.

Hi john,

I also suggest you use Mark4asp's recommended approach. For ASP.NET server
control, when it is put inside another (one ore more level) container
control, its client-side html DOM id will be mangled to a long text(include
its upper level container's ID). For such scenario, if your client-side
script in page want to reference these control, you'd better use the
"Control.ClientID" to inject the actual client-side DOM id of your server
control. Here is also the MSDN reference mentioned this:

#Client Script in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/library/3hc29e2a.aspx

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top