managing controls

R

Ricardo Furtado

I'm using visual Basic .Net to make a web site and i'm having problems on
managing the controls contained in the web pages. For example, i have an
image who's ID="Img0", when i try to run a script that refers to Img0, like
the following:
Img0.src="BoxForn.bmp"
this doesnt work, and an error is shown stating that Img0 is not defined.
Now, i was able to overcome this problem by doing the following:
document.images.item("Teste1").src="BoxForn.bmp"
And that, indeed worked for the images i have in the page but for instance,
the textbox "txtUser", and other controls, besides the images, don't work,
and the error is allways the same "ControlXXX is not defined"
I programmed in html and javascript and i used to do the following when
refering to controls in the page
document.Form1.ControlName.value="Value"
But this doen't work either.
There is few documentation on this part of visual basic .net and i need to
know this in order to continue the web site, can you please help? following
is one of the pages i'm working on, it may help

my thanks in advanced

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
<!--
var Counter=0;
var Utilizador=0;
var Img0Val=0;

function Img0_onclick() {
if(Img0Val==0)
{
document.images.item("Teste1").src="BoxForn.bmp"
Img0Val=1;
}
else
{
document.images.item("Teste1").src="Bar3d.bmp"
Img0Val=0;
}
if(Utilizador==0)
{
Utilizador=1;
}
else
{
Utilizador=0;
}
Counter++;
}

function Img1_onclick() {
alert(document.images.item("Teste2").src="Bar3d.bmp")
if(document.images.item("Teste2").src=="Bar3d.bmp")
{
document.images.item("Teste2").src="BoxForn.bmp"
}
else
{
document.images.item("Teste2").src="Bar3d.bmp"
}
Counter++;
if(Utilizador==0)
{
Utilizador=1;
}
else
{
Utilizador=0;
}
Counter++;
---------> txtUser.value=Utilizador; <---------------------
}

function Img2_onclick() {
alert(document.images.item("Teste3").src="Bar3d.bmp")
if(document.images.item("Teste3").src="Bar3d.bmp")
{
document.images.item("Teste3").src="BoxForn.bmp"
}
else
{
document.images.item("Teste3").src="Bar3d.bmp"
}
Counter++;
txtUtilizador.value="Teste";
}

// -->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table style="width: 634px; height: 244px">
<tr>
<td>
<img ID="Img0" name="teste1" src="Bar3d.bmp"
language="javascript" onclick="return Img0_onclick()" /></td>
<td>
<img id="Img1" name="teste2" src="Bar3d.bmp"
language="javascript" onclick="return Img1_onclick()" /></td>
<td>
<img id="Img2" name="teste3" src="Bar3d.bmp"
language="javascript" onclick="return Img2_onclick()" /></td>
</tr>
<tr>
<td>
<img id="Img3" name="teste4" src="Bar3d.bmp" /></td>
<td>
<img id="Img4" name="teste5" src="Bar3d.bmp" /></td>
<td>
<img id="Img5" name="teste6" src="Bar3d.bmp" /></td>
</tr>
<tr>
<td>
<img id="Img6" name="teste7" src="Bar3d.bmp" /></td>
<td>
<img id="Img7" name="teste8" src="Bar3d.bmp" /></td>
<td>
<img id="Img8" name="teste9" src="Bar3d.bmp" /></td>
</tr>
</table>
<br />
<input id="txttxtUser" name="MyText" type="text" accesskey="User" />
</form>
</body>
</html>
 
P

Phillip Williams

Your question is really about Javascript, and not about VB.net, because all
of the objects that you listed in your code are running client-side.

To refer to an element that triggered an event (e.g. the onclick event) you
can use window.event.srcElement directly without having to write
document.images.item("Teste1"). For example, I would hook up all of the
images onclick events to one function named Image_Click as follows:

function Image_Click(){
var img = window.event.srcElement
var imgID = img.id;
switch (imgID){
case "Teste1":
img.src="BoxForn.bmp";
break;
case "Teste2":
// do something else
break;
}
}

Alternatively you can use document.getElementById(id) (for IE 5.5+) and
document.all[id] (for IE 5 and below)
 

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,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top