<script> and button
<script> and button
<script> and button
<script> and button
<script> and button
<script> and button <script> and button <script> and button <script> and button <script> and button <script> and button <script> and button <script> and button
<script> and button <script> and button
<script> and button
Go Back  Xtreme Visual Basic Talk > > > <script> and button


Reply
 
Thread Tools Display Modes
  #1  
Old 08-02-2010, 07:14 AM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default <script> and button


I have always used code-behind pages for coding. The page is generated by egttign a series of files and outputting the HTML.
I have put a button in one of the pages and want to perform an action when its clicked - there is a text box with the button.

I cant write it in code behind page as it won't recognise the text box as its input from one of the HTML file s I laod at page_load.

So I added <script> tags in my .aspx page thinking (hoping) it woudl recognise it but when I press the button I jsut get An error has occured in the script on the page" "Error: 'DoTask2' is undefined.

My .aspx HTML is below. Can anybody please point me in the right direction?

Thanks

Quote:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="canvas._default"%>
<script language="VB" runat="server">
public function DoUpload2
bodycoding.innertext="Button pressed!"
end function
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Page Title</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<div id="bodycoding" runat="server"></div>
</form>
</body>
</HTML>

Last edited by tonymaclennan; 08-02-2010 at 07:17 AM. Reason: Adding to
Reply With Quote
  #2  
Old 08-02-2010, 09:18 AM
Qua's Avatar
Qua&lt;script&gt; and button Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

I can't really decipher your intention from the above explanation. When your ASP button is clicked your will be notified server-side via the Click event. In this click event you have access to any of the input given on the page before the click.

There are a few ways to access the input data. If the data has been entered into an ASP.NET control, then you can simply access it through the controls properties. If the input is entered into a normal html control, then you will have to access the data through the query string. You can access the query string data via the Request.QueryString collection.

If you're not interested in handling the click event server side, but instead client side (as your approach above kinda indicates due to the user of javascript) then you're half way on the right track. You will have to hook up a javascript function with the html click event of the button. You can do this with the following syntax:
Code:
<input type="button" onclick="DoUpload2();" Value="Click Me" />
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #3  
Old 08-02-2010, 10:55 AM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

Hi,

Let me clarify.

I have 2 HTML files: pagehead.htm, pagebody.htm
The page default loads and uses the code behind page default.aspx.vb to load the two html file and insert the HTML code generated into the bodycoding <div> in the default.aspx HTML section.

Now, lets say pagebody.htm has a text box (textbox1) and a button (button1) which I want to run code from at runtime. If I refer to textbox1 in the default.aspx.vb page it won't work as the text box isnt in the form yet (as its added from the html files at runtime).

In the .htm file where the button is I placed "onclick="DoUpload2();"" in the html code and placed some code in the default.aspx file - like shown in the original comment - hoping when I clicked the button1 this code would run....


... BUT the error occurs & nothing happens...

Hope that makes sense?..
Reply With Quote
  #4  
Old 08-02-2010, 11:52 AM
Qua's Avatar
Qua&lt;script&gt; and button Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Quote:
Originally Posted by tonymaclennan View Post
Hi,

Let me clarify.

I have 2 HTML files: pagehead.htm, pagebody.htm
The page default loads and uses the code behind page default.aspx.vb to load the two html file and insert the HTML code generated into the bodycoding <div> in the default.aspx HTML section.

Now, lets say pagebody.htm has a text box (textbox1) and a button (button1) which I want to run code from at runtime. If I refer to textbox1 in the default.aspx.vb page it won't work as the text box isnt in the form yet (as its added from the html files at runtime).

In the .htm file where the button is I placed "onclick="DoUpload2();"" in the html code and placed some code in the default.aspx file - like shown in the original comment - hoping when I clicked the button1 this code would run....


... BUT the error occurs & nothing happens...

Hope that makes sense?..
Seeing how both of your files is plain html files how do you intend to make use of ASP.NET? In the code you have posted so far you haven't shown or used any ASP.NET features so far and generally your approach appear messy and/or unstructured.

It sounds like masterpages would be a fit solution for your problem. They allow you to define the common outline for your pages. This is usually items such as: Menu, page header, page footer and any other relatively static content. They then allow specific areas such as the page body to be defined inside each specific .aspx page.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #5  
Old 08-02-2010, 12:48 PM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

Quote:
Originally Posted by Qua View Post
Seeing how both of your files is plain html files how do you intend to make use of ASP.NET? In the code you have posted so far you haven't shown or used any ASP.NET features so far and generally your approach appear messy and/or unstructured.
Many things are being done, I just didn't see the need to list what takes place i nthe code behind page. But what template file is loaded depends on requested page, different querystrings - accesses the database performing different querys depending on the requested URL.

The issue is with trying to recognise form elements such as buttons and text boxes which are present in the html template page from the actual aspx page. As it cant be done using the code-behind page with it not being able to find the form element yet.
Reply With Quote
  #6  
Old 08-02-2010, 02:30 PM
PlausiblyDamp's Avatar
PlausiblyDamp&lt;script&gt; and button PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Do you have an aspx page in this project? If so what is the relationship between this aspx page and these two html files?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 08-02-2010, 04:31 PM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

Quote:
Originally Posted by PlausiblyDamp View Post
Do you have an aspx page in this project? If so what is the relationship between this aspx page and these two html files?
I have many aspx pages & many html files.
When the user loads the page it will load relevant html files depending on what page they are requesting in the url query. But for alot of pages it will replace certain text strings with html from another html file depending on what page the user requests. It also replaces items in the html code depending on cookies placed by my script & content from my DB.


But I want to include a form in one of my html templates - it will be loaded in to the page at runtime depending on what the user has requested. But I am unsure how to get the form to execute vb code as obviously I cannot do it from the code behind page as the form isn't yet in the aspx page - as the html containing the form is added at runtime...
Reply With Quote
  #8  
Old 08-03-2010, 02:21 AM
Qua's Avatar
Qua&lt;script&gt; and button Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Quote:
Originally Posted by tonymaclennan View Post
I have many aspx pages & many html files.
When the user loads the page it will load relevant html files depending on what page they are requesting in the url query. But for alot of pages it will replace certain text strings with html from another html file depending on what page the user requests. It also replaces items in the html code depending on cookies placed by my script & content from my DB.


But I want to include a form in one of my html templates - it will be loaded in to the page at runtime depending on what the user has requested. But I am unsure how to get the form to execute vb code as obviously I cannot do it from the code behind page as the form isn't yet in the aspx page - as the html containing the form is added at runtime...
I think I finally understand your intention. First of all, it is possible to handle this situation with a codebehind file. You should create your form loaded dynamically from html in such way that when it is submitted it's action is a specific .aspx page of your choice. This is simple:
Code:
<form id="form1" runat="server" action="WebForm1.aspx" method="get"></form>
Moreover I have specified the method to be a "get" request since this will add any form element values to the querystring. The next step is to add actual input elements to the form:
Code:
    <form id="form1" runat="server" action="WebForm1.aspx" method="get">
        <input type="text" name="randomText" value="Testing" />
        <input type="submit" name="name" value="Click to submit" />
    </form>
Now you have pure html that will post the content of a textbox to an aspx page. To access the entered input use the Request.QueryString collection:
Code:
       If (Request.QueryString("randomText") IsNot Nothing) Then
            Dim randomText As String = Request.QueryString("randomText")
        End If
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #9  
Old 08-04-2010, 07:05 AM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

That works an absolute treat - thanks sooo much!!!

Another question though.

If i was adding items to a list box I would use:

dim listitem
call getmylistarray(ausersid)
counter=0
do until mylistarray(counter)="" or counter >15
listitem=new listitem
listitem.value=ANIDHER
listitem.text=SOMETEXT
lstitems.items.add(listitem)
counter+=1
loop

But if the listbox was in the HTML page being loaded at runtime, how would I populate that list box with content loaded at runtime? Or would I have to find another method?

Many thanks
Reply With Quote
  #10  
Old 08-04-2010, 07:51 AM
tonymaclennan tonymaclennan is offline
Contributor
 
Join Date: Sep 2004
Location: Lincolnshire
Posts: 402
Default

That aside actually. I am uing this type of thing for many things - one of them is uploading files.

Previously, I used the following code. This was a form that I placed in the actual ASPX page and did not insert from html tempalte file.

Quote:
Sub DoUpload(ByVal Sender As Object, ByVal e As System.EventArgs)
Dim GuestIP As String
GuestIP = Request.ServerVariables("REMOTE_ADDR")
GuestIP = GuestIP.Replace(".", "")
Dim FileName As String
Dim sPath As String
Dim sFile As String
Dim sFullPath As String
Dim sSplit() As String
Dim sPathFriendly As String

'Upload to same path as script
'Internet Anonymous User must have write permissions

sPath = Server.MapPath(".") 'LocPicturesDir
If Right(sPath, 1) <> "\" Then
sPathFriendly = sPath 'Friendly path name for display
sPath = sPath & "\"
Else
sPathFriendly = Left(sPath, Len(sPath) - 1)
End If


'Save as same file name being posted
'The code below resolves the file name
'(removes path info)
sFile = Sender.PostedFile.FileName
sSplit = Split(sFile, "\")
sFile = sSplit(UBound(sSplit))

FileName = GuestIP & Mid(sFile, InStrRev(sFile, "."))
sFullPath = LocUploadsDir & FileName
Try
Sender.PostedFile.SaveAs(sFullPath)
bodycoding.InnerText = "Upload of File " & sFile & " to " & sPathFriendly & " succeeded." + vbNewLine + "Original loc: " + sFile
Catch Ex As Exception
bodycoding.InnerText = "Upload of File " & sFile & " to " & sPathFriendly & " failed for the following reason: " & Ex.Message
End Try


End Sub

I used the following form code to do this:

Quote:

<form id="frmupload" runat="server" action="default.aspx" method="get">
<input type="file" name="randomText" value="Testing" />
<input type="submit" name="name" value="Click to submit" onclick="DoUpload" />
</form>
Now I tried adding " onclick="DoUpload" " to the form field i nthe html file but tha doesn't work.

How would I acheive this?

I also tried acheiving it by getting the file location from "Request.QueryString("randomText")" - although that gets the file location perfectly , when the .saveas method is used it comes up with an error as the request.querystrng is in string format..
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
&lt;script&gt; and button
&lt;script&gt; and button
&lt;script&gt; and button &lt;script&gt; and button
&lt;script&gt; and button
&lt;script&gt; and button
&lt;script&gt; and button &lt;script&gt; and button &lt;script&gt; and button &lt;script&gt; and button &lt;script&gt; and button &lt;script&gt; and button &lt;script&gt; and button
&lt;script&gt; and button
&lt;script&gt; and button
 
&lt;script&gt; and button
&lt;script&gt; and button
 
-->