HELP! new to asp.net!

nima1985

Newcomer
Joined
Jun 29, 2005
Messages
19
Hi, I am making an ASP.NET web form for the first time, and I have a few questions:

1) How do you make global array variables?

I know that you can make global variables such as:

Application("end")=end

in your Global.asax's Application_Start (..) and access it in ur Page_Load:

Application("end")=false

but for arrays, how do you declare it and call it?

2) How do you allow for the user to move an object on the page? (ex: if the user is allowed to move an icon around the page to wherever they would like)? Also, how would you restrict the user to only move horizontally (or vertically)?


Thank you SO much in advance for any help. :)
 
1)

Application["myarray"] = new Int[2]{1, 2};

Int[] myarray = (Int[])Application["myarray"];
int data = myarray[0];

I think it works.

2)

you mean something like mymsn? I'm not sure its a .net issue. Its done using javascript if I'm not wrong, and has nothing to do with asp.net. Maybe ASP.net make it easier to create such script...
 
utilitaire said:
2)
you mean something like mymsn? I'm not sure its a .net issue. Its done using javascript if I'm not wrong, and has nothing to do with asp.net. Maybe ASP.net make it easier to create such script...
I think ASP.NET 2.0 will support something like this with the datasets (at least I've seen a video that mentioned it), but it hasn't been released yet.
 
hey joe and utilitaire
thanks so much for the replies :D

i ended up solving both issues actually (found a lotta good sites online that provide code)

1) use an arraylist, but must cast in the WebForm in order to use arraylist functions:

in Global.asax:

Dim list as New ArrayList

Application("list")=list


in WebForm1.aspx.vb:

CType(Application("list"), ArrayList).Insert(0, label1)



and for 2)

create a new object in asp.net, and in the HTML, after <HEAD> put:

<HEAD>
<style>.drag { CURSOR: hand; POSITION: relative }
</style>
<script language="JavaScript1.2">
<!--

/*
Drag and Drop Script-
© Dynamic Drive (www.dynamicdrive.com)
For full source code, installation instructions,
100's more DHTML scripts, and Terms Of
Use, visit dynamicdrive.com
*/

var dragapproved=false
var z,x,y
function move(){
if (event.button==1&&dragapproved){
z.style.pixelLeft=temp1+event.clientX-x
z.style.pixelTop=temp2+event.clientY-y
return false
}
}
function drags(){
if (!document.all)
return
if (event.srcElement.className=="drag"){
dragapproved=true
z=event.srcElement
temp1=z.style.pixelLeft
temp2=z.style.pixelTop
x=event.clientX
y=event.clientY
document.onmousemove=move
}
}
document.onmousedown=drags
document.onmouseup=new Function("dragapproved=false")
//-->
</script>



and right after the asp: declaration, put "class=drag":

<asp:image class="drag" id="Image1" ></asp:image>

So utilitair, ur right for 2), it needs javascript :)


One more question:
for 2), i actually wanted a slider bar thingy that the user can move horizontally. My image that i made using the above solution, the user can move it around anywhere on the page. adn I want to restrict that to only horizontally. Does anyone know any websites that show how to do that?

thanks again :)
 
Last edited:
You're doing some interesting stuff that I've never seen, but here's an idea:

Take the part of your code that says:
y=event.clientY
And delete it! That way, the "y" value can not be changed. (How did my font turn blue???)
 
hey joe

i deleted that line, but what happened was that, i want the bar to start at a certain position, and they can move it horizontally. when i deleted that line of code, as soon as i click on the image, it moves to coordinates (0,0) and i can then move it horizontally. but i want it to start at the coordinates i set in my WebForm1. as soon as i make the image, it shows up in the right coordinates, but when i click on it to move it, it goes up to (0,0)

here's my code:

in WebForm1:

Dim i As New System.Web.UI.WebControls.Image

top = 468
left = 50

i.ImageUrl() = "\slider2.JPG"
i.CssClass = "drag"
i.Style.Add("position", "absolute")
i.Style.Add("top", top.ToString & "px")
i.Style.Add("left", "80px")

CType(Application("Slider"), ArrayList).Insert(Application("RadioCount"), i)

PlaceHolder1.Controls.Add(CType(Application("Slider"), ArrayList).Item(i))


in my HTML:

same thing as before but without the line:

y=event.clientY


how can i fix this now ? :s
 
I GOT IT!! :D

instead of deleting the line:

y=event.clientY

you have to delete the line:

z.style.pixelTop=temp2+event.clientY-y

:D me happy

Oh but when i press another button, the image goes back to the original coordinates. Is there a way to keep it in the coordinates that the user moves it to?

one more question (..sorry..): my image, when i drag it, it has to go on a grid. But when the user drags it on the webform, it goes behind the grid. is there anyway of making the image always on top of everything on the webpage (i cant seem to find a "priority" function.. should i use layers? and if so, how )?

THANK YOU!!
 
When a button is clicked, your page calls the Page_Load() function again, which resets your image to your startup coordinates. You need to check the "IsPostback" value. If "IsPostback == true", the page is loading for the first time and you would want to set your coordinates for your image; if it is false, it is reloading after an event that was triggered, and you can either hope the coordinates are unchanged or reload them from variables you have set.

Now to the second question about the image going behind another element on your form: Set a z-index for the image and the other element. You will need to set the z-index order of the element to be lower than the z-index of your image, so that when the image nears the proximity of the other element, the browser sees that the element is "below" the image.

Hope all turns out well!
 
hey joe

the z-index thing worked :)

for the IsPostBack "function" that ur talking about, how would i be able to get the coordinates of the image after the user moves it? (because when teh user moves the image, and places it somewhere, the function that is called is in teh HTML :

if (event.button==1&&dragapproved){
z.style.pixelLeft=temp1+event.clientX-x
return false
}


so how would I be able to save these values in a variable type, even tho it is in javascript? ie, it has to be a global variable that can be accessed by the asp code and the javascript, but HOW?

:s

thanks again ;)
 
You might be able to place a couple of Labels in your form, set the .Visibility = false, then set Label1.Text = z.style.pixelLeft, and Label2.Text = z.style.pixelTop. That might work in JavaScript, but I don't know a lot about JS, so I could be lying.

If you can't get this technique to work, start a new thread and ask how to set a value in ASP that can be read by JS, and vice versa.

Not sure how all this will look in your code, either. Once you get it working, post a link so I can see what you made. It sounds interesting!
 
i thought about making images and hiding them until the user needs them, but i cant do that becuase the user can have a million images if they wanted. so it's better to create them as they're needed.

creating new threads also causes a lotta probs if the user creates, lets say, a million images, coz that means a million threads :s

if anyone knows how to access one variable from both asp/vb and javascript, it would be really great coz that's one possible solution :s until then, im sort of lost..

my application isnt that fancy :P but yea if i ever get it up online (get it approved by my supervisor, etc), ill definately put up a link.
warning: by looking at my page, u can easily tell that I just started (at the beginning of this week) with asp.net and webpages in general :s


thanks again to everyone (especially joe)
:)
 
nima1985 said:
if anyone knows how to access one variable from both asp/vb and javascript, it would be really great coz that's one possible solution :s until then, im sort of lost..

Maybe you can use a hidden field (with runat="server") who stores the value. This hidden field then can be accessed from server side and from client side, I suppose.
 
Thanks for the reply Hyuga. :)
What do you mean by "hidden field"? can you describe a lil bit more (maybe provide some example code)?
Thanks again :D
 
Hyuga said:
Maybe you can use a hidden field (with runat="server") who stores the value. This hidden field then can be accessed from server side and from client side, I suppose.
I don't think JavaScript can see ASP.NET [runat="server"] variables. That was why I suggested placing a Label component on the HTML form and using ASP.NET to write to it. JavaScript should be able to see the Label.
 
joe_pool_is said:
I don't think JavaScript can see ASP.NET [runat="server"] variables. That was why I suggested placing a Label component on the HTML form and using ASP.NET to write to it. JavaScript should be able to see the Label.


I changed my javascript code to:

<script language="JavaScript1.2">
<!--


var dragapproved=false
var z,x,y
function move(){
if (event.button==1&&dragapproved){
z.style.pixelLeft=temp1+event.clientX-x
//z.style.pixelTop=temp2+event.clientY-y
coordlabel.text=event.clientX <<<<<<
return false
}
}
function drags(){
if (!document.all)
return
if (event.srcElement.className=="drag"){
dragapproved=true
z=event.srcElement
temp1=z.style.pixelLeft
temp2=z.style.pixelTop
x=event.clientX
y=event.clientY
document.onmousemove=move
}
}
document.onmousedown=drags
document.onmouseup=new Function("dragapproved=false")
//-->
</script>




on spot <<<< i tried to save the label's text to the coordinates, and made the label visible so i can see that it works. when i load the page, and move the image, the label's text never changes to the x coordinate

:s
 
Okay, I'm giving advice on JavaScript, and I really don't know much about it...

I found something on a JavaScript site with a Google search. Would something like this work?
Code:
<html>
<head>
<script>
function BtnWrite() {
	self.document.forms[0].elements[0].value=event.clientX;
}
</script>
</head>
<body>
<form name="msgform" action="">
<input type="button" value="Click Here To See" onClick="BtnWrite()">
</form>
</body>
It uses a button to write text to, and that might be something you could modify to write to something else (I'm not sure what types are allowed for INPUT tags). Just keep in mind that whatever you write to in JavaScript has to be accessible in ASP.NET as well.
 
it doesnt seem to be working..

here's my javascript code (exactly what you posted up):

<script>
function BtnWrite() {
self.document.forms[0].elements[0].value=event.clientX;
}
</script>



and here's the info on the button:

<asp:Button id="testbutton" OnClick = "BtnWrite()" style="Z-INDEX: 135; LEFT: 992px; POSITION: absolute; TOP: 384px"
runat="server" Height="40px" Width="72px" Text="Button"></asp:Button>



the error that comes up is

'BtnWrite' is not a member of 'ASP.WebForm1_aspx'.


:s

i'm also confused about the javascript function: it saves the event.clientX btu only when the button is pressed. so how is the software suppose to know when the user drags the image (how are the drag class and the function above related?) because, it just gets the most recent event.clientX, and if the user drags the image, then clicks somewhere else before the program calls the BtnWrite function, the value will be incorrect..

Also, what is "self.document.forms[0].elements[0]" ?

i think maybe im totally misunderstanding :s

thanks again ;)
 
Last edited:
"self.document.forms[0].elements[0]":

Self is the current window, document is what "self" is displaying (an webpage), forms[0] is the first form you have listed on your page, and elements[0] is the first element.

You might want to check out this MSDN article:
http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/
This discusses methods both in ASP.NET 1.x and the "soon to be released" v2.0
 
hey
thanks for the link. :)


I had another question: i have an image, which is of a class "drag". so i declared it in my html as follows:

<asp:image id="Image1" style="Z-INDEX: 116; LEFT: 1216px; POSITION: absolute; TOP: 8px" runat="server"
Width="80px" Height="24px" ImageUrl="\slider2.JPG"
CssClass="drag"></asp:image>


in the class "drag" written in javascript, is it possible to save the id of the specific image that is being dragged? (ie, there can be 10 images all using teh class drag. but i wanna save the id of the specific instance of the image which is being used at teh moment)

is there anyway I can do that ?

i tried to do place the following in the function that gets called after the image gets dragged:

var n;
n=this.id


but that gave a value of "undefined" for n. i was able to save the location of the image however, so i know that the function is working properly. i just dont know how to save the instance's id.

thanks again ;)
 
Last edited:
nima1985 said:
it doesnt seem to be working..

here's my javascript code (exactly what you posted up):

<script>
function BtnWrite() {
self.document.forms[0].elements[0].value=event.clientX;
}
</script>



and here's the info on the button:

<asp:Button id="testbutton" OnClick = "BtnWrite()" style="Z-INDEX: 135; LEFT: 992px; POSITION: absolute; TOP: 384px"
runat="server" Height="40px" Width="72px" Text="Button"></asp:Button>



the error that comes up is

'BtnWrite' is not a member of 'ASP.WebForm1_aspx'.

It seems that in your ASP code you are looking for the button called BtnWrite, and you should look for the button testbutton. Your value, if the javascript works fine, should be placed in button1.value, and you have to access this control value from your ASP to recover it.
 
Back
Top