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!
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:
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?
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?
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
<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
----- 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.
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.