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 / July 2007

Tip: Looking for answers? Try searching our database.

Client side jscript issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
john - 07 Jul 2007 02:50 GMT
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>
nahid - 07 Jul 2007 07:51 GMT
> Working in Visual Studio
>
[quoted text clipped - 56 lines]
>
> </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;
nahid - 07 Jul 2007 07:55 GMT
> Working in Visual Studio
>
[quoted text clipped - 56 lines]
>
> </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
john - 07 Jul 2007 09:05 GMT
Thanks, that was it, it added "ctl00_ContentPlaceHolder1_" to the front of
the ID.

>> Working in Visual Studio
>>
[quoted text clipped - 77 lines]
> http://www.kaz.com.bd
> blog: http://nahidulkibria.blogspot.com
mark4asp - 07 Jul 2007 09:30 GMT
>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;
Steven Cheng[MSFT] - 09 Jul 2007 08:03 GMT
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.
Mark Rae [MVP] - 07 Jul 2007 09:44 GMT
> 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
[quoted text clipped - 3 lines]
> 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.

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

mark4asp - 07 Jul 2007 09:28 GMT
>Working in Visual Studio
>
[quoted text clipped - 56 lines]
>
></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...

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.