Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2006, 10:51 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to retrieve text from server-side?

Hello,

I define a table in aspx file and Javascript code.
Purpose: when the user clicks on any cell of the table, the text of it must
be rendered in an Alert.

 <asp:Table ID="table1" runat="server">
 </asp:Table>

<script language="javascript" type="text/javascript">
function tableclick(event)
{
strid=window.event.srcElement.id
alert(strid) // this works: i get e.g. 3:2
strid=window.event.srcElement.text}
alert(strid) // this doens't work ("undefined")
}

I create row and cells in the code-behind:
Dim r As TableRow
Dim c(50, 20) As TableCell
For i = 0 To 50
r = New TableRow()
For j = 0 To 20
c(i, j) = New TableCell()
c(i, j).ID = j & ":" & i.ToString
r.Cells.Add(c(i, j))
Next
Table1.Rows.Add(r)
Next

c(3, 2).Text = "this is the text i want in Javascript"
...

My problem is that the Alert gives "undefined".
I can get the 'ID' of each cell, why not the 'text'?
I know there is the property 'ClientID' but it's overruled by the ID i
defined in the code, so that's not the point (i think).

Thanks for help.


 
Old June 16th, 2006, 10:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi hertendreef,

JavaScript != ASP.NET

Most stuff you define in server side pages does not directly translate to client side properties. So, in JavaScript / HTML there are no properties like text and clientId. These are ASP.NET concepts.

Try innerHTML or innerText instead.


Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old June 17th, 2006, 03:38 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for your reply. It was innerText indeed.
If you don't mind, a last similar question, about backcolor:
I need to get the background color of the clicked cell, just like the text in previous problem. I can do it with the Forecolor, but not with Backcolor (gives empty Alert, no error):
Code-behind is now: (tried with two syntaxes)
...
c(3, 2).BackColor = System.Drawing.ColorTranslator.FromHtml("red")
c(3, 2).ForeColor = System.Drawing.ColorTranslator.FromHtml("yellow")
//c(3, 2).BackColor = Drawing.Color.Red
...

and the Javascript:
forecl=window.event.srcElement.style.color // this works
backcl=window.event.srcElement.style.background // this not
alert(forecl)
alert(backcl)

Thanks again



 
Old June 17th, 2006, 03:44 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

To make things a bit confusing, there are at least three different terms for the same thing.

In ASP.NET, things are called like this:

BackColor
ForeColor
etc

These names map to client side CSS properties, like back-color and color.

In JavaScript in turn, these items map to

backgroundColor
color

The rule of thumb with the conversion between CSS and JavaScript is:

start with a lower case, remove dashes and start each word after a dash with a capital letter.

So, background-color becomes backgroundColor, font-weight becomes fontWeight and so on.

To cut a long story short: you need backgroundColor.

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old June 17th, 2006, 06:04 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks, it works ...






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Converting Client-side to Server-side Code kwilliams ASP.NET 2.0 Professional 1 November 21st, 2007 05:25 PM
Firing server side events at client side codes mehdi62b ASP.NET 1.0 and 1.1 Basics 6 May 18th, 2005 09:11 AM
sharing a server-side variable with client-side pigtail Javascript How-To 6 November 4th, 2004 02:01 AM
Two Client Side vs Server Side issues Milo Classic ASP Professional 5 May 25th, 2004 02:47 PM
Accessing Server Side Data on Client Side steve456 Classic ASP Professional 3 October 15th, 2003 02:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.