Welcome to Dream.In.Code
Become an Expert!

Join 150,068 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,782 people online right now. Registration is fast and FREE... Join Now!




How to write into a DIV from ASP.NET?

 
Reply to this topicStart new topic

How to write into a DIV from ASP.NET?

netnewb
25 Feb, 2008 - 02:07 PM
Post #1

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 13

Hello,
I want to pass an array from ASP.NET to HTML or JavaScript. I'm aware that you can do it by creating an ASP literal, a hidden control, or directly passing a variable, but this seems ugly to me. I'd like the data to sit in a plain old hidden div, so human can read it in the source file. The way I'd LIKE to do it is like this:

CODE
<div id="passedindata">
data1,data2,data3,data4,data5,
</div>


And in asp.net, I reference the data sequence with
CODE
passedindata.InnerHtml


I've written javascript that properly parses the passedindata, but ASP.NET cannot see the div. If I add 'runat="server"' to the div tag, then ASP.NET can use it fine, but asp.net changes the id name of the div, so javascript can no longer see it!

1. How can I make this work?
2. Is this a really wrong way to pass data? If so why?

Thank you.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Write Into A DIV From ASP.NET?
25 Feb, 2008 - 02:16 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
My first question would be why do you want to do it this way? You say you want the data hidden, but want the user to be able to view source and see it, if thats the case why hide it at all? Normally when someone wants to hide data is it so the user cannot access or read it, so why not just do it the easy way and store it in the View State or in a persistent object?
User is offlineProfile CardPM
+Quote Post

netnewb
RE: How To Write Into A DIV From ASP.NET?
25 Feb, 2008 - 02:34 PM
Post #3

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 13

QUOTE(PsychoCoder @ 25 Feb, 2008 - 03:16 PM) *

My first question would be why do you want to do it this way? You say you want the data hidden, but want the user to be able to view source and see it, if thats the case why hide it at all? Normally when someone wants to hide data is it so the user cannot access or read it, so why not just do it the easy way and store it in the View State or in a persistent object?


The main reason I want to do it that way is that I'd like to reduce the amount of magic data mysteriously flying around--for debugging purposes. If the data lands in a hidden div, I can see it in the html. Why hide it? Because the data is used by javascript.

Secondary reason is that it seems only natural that one should be able to insert their own dynamic html code from asp.net, to be put in a div--without being forced into using/making a control.

I do not understand your references to "the easy way". Easy to me is as I outlined above. I'm not insisting to do it my way, but I see no reason why some automagical way (especially when I don't know what you're talking about) is superior to simply modifying the tag's content, just as I would modify its style if/when I want.

This post has been edited by netnewb: 25 Feb, 2008 - 02:36 PM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Write Into A DIV From ASP.NET?
25 Feb, 2008 - 02:53 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well then, you could just do something like this:


You would have your <DIV> setup like so:


vb

<div id = "MyDiv" style="display:none;"><%# Data1 %></div>



Then to write data to it create yourself a property, like so:


vb

'Property variable
Private _data1 As String
'Now for the property
Public Property Data1 As String
Get
Return _data1
End Get
Set
_data1 = Value
End Set
End Property



Then, whenever you set that property it will be bound to the div we created. And as long as style="display:none;" is present the div isn't visible to the user, but you should be able to see it when you view source
User is offlineProfile CardPM
+Quote Post

netnewb
RE: How To Write Into A DIV From ASP.NET?
25 Feb, 2008 - 04:34 PM
Post #5

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 13

QUOTE(PsychoCoder @ 25 Feb, 2008 - 03:53 PM) *

Well then, you could just do something like this:


No go. It compiles and runs, but the passed-in data is not visible. (Hence, cannot parse it with javascript as intended)

In content page, I have:
CODE
<div id="passedindata" style="display:none;">
  <%# DotColorings %>
</div>


The source html code just shows a blank line between the div tags.

In the code behind for the page, I have:

CODE
    private string _dotcolors;
    public string DotColorings
    {
        get
        {
            return _dotcolors;
        }
        set
        {
            _dotcolors=value;
        }
    }


And finally, in the Page_Load (asp.net), I have:
CODE
DotColorings ="zero,grn,grn,grn,grn,grn,grn,grn,grn,grn,";


-----
Also, my methodology is to build things piecemeal. So I first build the javascript parser, working off some static test data in the div, and after that's working, create the asp.net portion to overwrite the div's contents with the real data. If I could just overwrite the div's .InnerHtml, then this would work perfectly. I could just leave my default/junk/static data in the div, and it would be overwritten by asp.net. But by your method (if it were working), I have to leave the div blank.

If what I'm trying to do is really the wrong way, please tell me why, and what is the best way. Again, please keep in mind that I want to be able to build progressively, piecemeal, preferably without magic data passing. If this methodology is antiquated, please say why.

Also, please tell me how I can do rudimentary debugging on asp.net side. For example, I'd like to call MessageBox (or write to a debug window or whatever) to display the value of DotColorings right after I set it, just to make sure it went ok.

Thank you!
User is offlineProfile CardPM
+Quote Post

netnewb
RE: How To Write Into A DIV From ASP.NET?
25 Feb, 2008 - 05:29 PM
Post #6

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 13

I found the problem in the above. Instead of <%# DotColorings %>, I needed to use <%=DotColorings%>
This works to bring in the data. It's acceptable. But my other problem/questions still remain:

QUOTE
Also, my methodology is to build things piecemeal. So I first build the javascript parser, working off some static test data in the div, and after that's working, create the asp.net portion to overwrite the div's contents with the real data. If I could just overwrite the div's .InnerHtml, then this would work perfectly. I could just leave my default/junk/static data in the div, and it would be overwritten by asp.net. But by your method (if it were working), I have to leave the div blank.

If what I'm trying to do is really the wrong way, please tell me why, and what is the best way. Again, please keep in mind that I want to be able to build progressively, piecemeal, preferably without magic data passing. If this methodology is antiquated, please say why.

Also, please tell me how I can do rudimentary debugging on asp.net side. For example, I'd like to call MessageBox (or write to a debug window or whatever) to display the value of DotColorings right after I set it, just to make sure it went ok.


Can anyone help?
Thank you!

User is offlineProfile CardPM
+Quote Post

netnewb
RE: How To Write Into A DIV From ASP.NET?
26 Feb, 2008 - 04:34 PM
Post #7

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 13

BTW, I didn't mean to be rude. Thank you for your help PC.
Is what I'm trying to do really out of the ordinary?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month