Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have selected check box items car, bike, lorry

by using foreach am taking data's from database for selected item using
C#
string data1 = serializer.Serialize(rows);


problem is i need to (pass )display a data one by one for selected items
ScriptManager.RegisterClientScriptBlock
i used this

am getting out put only for lorry , the data is passing to script only when page load completes fully . what i need is i need to break and have to display in script immediately

What I have tried:

if (data1 != "")
{
HiddenField1.Value = data1;
//string strinfo = "data('" + data1 + "');";
ScriptManager.RegisterClientScriptBlock(this.Page, Page.GetType(), "Text", "data()", true);

}
Posted
Updated 28-Dec-16 8:26am
v2
Comments
F-ES Sitecore 22-Dec-16 4:45am    
If that code runs in a loop for each item then this line

HiddenField1.Value = data1;

overrides the previous data. So in the first loop it is set to "car", in the second to "bike" and in the third to "lorry", with each one overwriting the last which is why once your loop is done it only holds data for lorry.

Your second issue is when you use RegisterClientScriptBlock the "key" param ("Text" in your example) is used to ensure a script is added only once. In the first loop you add a call to "data()" with the key Text, in the second loop you try to add something with that key again so .net simply ignores that add and so on. Depending on what "data" does that might not be a problem, but it's something you need to keep in mind for the future.
Member 12119075 22-Dec-16 4:56am    
what will be a solution for done this
F-ES Sitecore 22-Dec-16 5:08am    
It depends what you want to do on the client. Personally I'd simply build up a List<string> of your three items and then parse that to JSON and write something to the page like

var data = <%=JsonGoesHere%>;

that would result in a json object in "data" on the client that you could then use in js to populate your fields or do whatever with. It's better to only use your .net code to generate the data to write to the page and have the js on the page use the data rather than try and generate a lot of dynamic js in your server code.

1 solution

Use the document.ready event in JavaScript. $( document ).ready() | jQuery Learning Center[^]. This will fire in JavaScript when the page is done loading. The code you have is running on the server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900