Passing data from server side to client side?

M

Mike Fellows

Ok, im not sure if this is at all possible and if it is how i go about it is
beyond me

i have a piece of client side code that requires a piece of data from the
server side (an ID number in this case)

how can i pass the data from one side to the other, or retrieve it from the
webform on the client side?

i hope ive explained this properly

Mike Fellows
 
E

Eliyahu Goldin

Mike,

This is one of the most common tasks in ASP.NET development. The solution is
to use hidden elements. If you need a single id value, you can use any html
element capable of holding values. If you need to pass the value back, you
need to use a <input type="hidden" ...>. If you need an id column in a grid,
you need to ad a column to your datagrid and make it invisible with a
stylesheet rule.

Note, if you use a server control and set property Visible to false, the
control won't be rendered to the client and you won't get anything.

Eliyahu
 
M

Mike Fellows

Eliyahu,

ok i sort of understand (though not fully)

if i have a label on my webform called MyLabel

and i have a clientside HTML button that runs my client side code

i want my HTML button to get the value of MyLabel so that i can use it
clientside
 
M

Mike Fellows

Eliyahu,

I hate to be a pain, but im probably more confused than ever

could you point me in the direction of an example at all?
(ive tried searching for them but i cant find any)

Thanks

Mike
 
E

Eliyahu Goldin

That's fine. Give you label an id and refer to it from javascript. If it is
an asp:Label, it will translate to <span> and you can get the label text via
innerText property.

Eliyahu
 
M

Mike Fellows

Eliyahu,

ok all worked fine on the server side, but i get an error on page on load

im including my code so you can see what im doing, i think the problem lies
within the javascript script

thanks

Mike

<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">

<SCRIPT language="javascript">
var id=hiddenId.innerText
</SCRIPT>

<SCRIPT language="VBScript">
Dim objWord
Dim exists
Sub OpenDoc()
Set objWord = CreateObject("Word.Application")
objWord.Documents.Open("\\Gringotts\common
documents\AutoPackProduction\Remortgage\Factfind.doc")
objWord.Visible = true
objWord.activedocument.saveas("c:\"& id &".doc")
set objWord=nothing
End Sub
</SCRIPT>

</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
</form>
<input style="Z-INDEX: 101; LEFT: 165px; WIDTH: 157px; POSITION: absolute;
TOP: 82px; HEIGHT: 48px"
onclick="opendoc()" type="button" value="opendoc">&nbsp;
<asp:label id="hiddenId" style="Z-INDEX: 102; LEFT: 50px; POSITION:
absolute; TOP: 28px" runat="server"
CssClass="Hidden"></asp:label>
</body>
</HTML>
 
E

Eliyahu Goldin

Mike,

Let's say you want to pass hidden id value from server to client.

1. Add a hidden label control to the aspx page:
<asp:Label id="hiddenId" runat="server" CssClass="Hidden"></asp:Label>
Add a stylesheet rule for Hidden to the page: .Hidden{display:none}

2. In the code-behind on server-side assign values to the id:
hiddenId.Text = "12345";

3. In javascripts on client-side get the value:
var id=hiddenId.innerText;

If it is not clear, please tell me what step causes a problem.

Eliyahu
 
M

Mike Fellows

you are a superstar

thanks Eliyahu

Mike


Eliyahu Goldin said:
Mike,

A few things:

You can't have webcontrols outside <form> tags.

The javascript should be a function and you should call it on client-side
load event:
<SCRIPT language="javascript">
var id;
function getId() {id=hiddenId.innerText;}
</SCRIPT>
...
<body MS_POSITIONING="GridLayout" onload="getId()">
After this variable id is available to other javascripts functions on the
page.

Also the stylesheet rule for Hidden is missing, unless you don't want to
hide the id.

Eliyahu

us i
 
E

Eliyahu Goldin

Mike,

A few things:

You can't have webcontrols outside <form> tags.

The javascript should be a function and you should call it on client-side
load event:
<SCRIPT language="javascript">
var id;
function getId() {id=hiddenId.innerText;}
</SCRIPT>
....
<body MS_POSITIONING="GridLayout" onload="getId()">
After this variable id is available to other javascripts functions on the
page.

Also the stylesheet rule for Hidden is missing, unless you don't want to
hide the id.

Eliyahu
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top