Click to See Complete Forum and Search --> : Adding JS to an ASP.NET Web User Control


kyuss
03-06-2003, 04:54 AM
Hello all, :)

I'm going nuts with this: I've made a web page with ASP.NET (loader.aspx) which has only a placeholder control.

Then I've made some web user controls, which are my different views (ascx files).
I then load dinamically the ascx I need into the loader page.

But the problem comes when trying to access a certain textbox or control in the view with javascript... I don't have no <body> or <form> tag, because they are in the loader page.
So, for example, I want to set the focus on a specific textbox of View1.ascx just when loading. If it was an aspx page that would be easy, just using the onload event of the form or body tag and calling the textbox.focus(). But since I don't have those tags.... How can I do that?

The html of the files explained:

There is the loader file which I use to load all my views - ascx web user control files. The loader has only a placeholder:
<body>
<form id="Form1" method="post" runat="server">
<asp:PlaceHolder id="phdr" runat="server">
</asp:PlaceHolder>
</form>
</body>

the ascx control (my view), the file that i load into the placeholder:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="view1.ascx.cs" ... %>
<asp: panel id="pnl1" ... runat="server">
<DIV ... ms_positioning="GridLayout">
<asp:TextBox id="txtName" ... runat="server" ></asp:TextBox>
... some other controls here
</DIV>
</asp: panel>
...some other panels which I turn visible or invisible depending on the user input progress.

So, how can i put JavaScript code in the ascx file to run when the panel or div is first loaded and set the focus on a textbox inside one of the divs or panels of the ascx file?


Thank you very much in advance ,' )
kyuss.

Ribeyed
03-06-2003, 08:17 AM
hi,
can't you or have you tried putting the javascript in the ascx files?

edited:
Sorry forget that, i will have a look into it. I am new to ASP.NET myself:(
edited again:
Do you have a URL to see the page?

Ribeyed
03-06-2003, 12:00 PM
hi,
i found this artical on microsft's Knowledge Base, it deals with what your need to do, apply client-side focus() to ASP webcontrols. I had a quick look but didn't have time to try it out, let me know how you get on with this. Anyway here is the url:

http://support.microsoft.com/default.aspx?scid=kb;en-us;316719#3

PeOfEo
03-06-2003, 12:28 PM
Is there anyway you can make an external script and use the url feature of java script?

kyuss
03-06-2003, 02:32 PM
First of all, thanks for replying=)

[SWR]Ribeyed:
The link you posted is for setting the focus in asp:textbox controls, which is what I want to do, but unfortunately not in a simple aspx web page which has the typical html structure. I need write JS code in the ascx control, which has not the <form> or <body> tags, so I don't know how to refferenciate to them.

PeOfEo:
I can load an external js file, that's no problem, but what for?

Thanks again to you both.
Cheers,
kyuss.

Ribeyed
03-06-2003, 03:10 PM
hi,
yes i understand what you are saying, your ascx file doesn't have and html tags but why not? My understanding of .NET which is not much but you can write HTML in an ascx file. I have experemented with code behind and created a login page which has HTML elements in it. I then have my main page and include the ascx file.
I would also maybe try adding the javascript call, to the web control in your ascx file and put the javascript sript in the loader.aspx, not sure if this would work but worth a try.
Can you give a URL fo rthe page as well it will help to see what you are trying to do, thanks

kyuss
03-06-2003, 06:44 PM
Hello [SWR]Ribeyed ,' )

I just came across the code I needed.
This is the link:
http://www.reflectionit.nl/InitialFocus.aspx

This class runs fine with my ascx web user controls now.

Regarding html in ascx files: My ascx file has the code behind file (C Sharp) and the html part, which of course has html code, but it has not <form> tag because there is only one form, the one in the aspx page which includes the control. Setting the focus state of the textbox in the onload event of the form should be easy, but since i have a form which has lots of possible different controls (ascx files) that i can load within,... i didn't know a way to do it.

Thanks for your help, mate.

kyuss.
:)

Ribeyed
03-06-2003, 06:49 PM
hi,
yes i was going to ask that last post did you need it in the codebehind file for the ascx file. Well i have bookmarked that page, thank you:D Very simple solution to the problem i might add.