Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have this classic ASP page that gets its style/content from the database. I am converting it to ASP .NET C#.
So, I am using all the Response.Write in the Page_Load.

If there is a control that is being created using Response.Write - it cannot be directly referenced in the code behind file. So - if I need to add some code in the Page_Load - I was trying like this in the Page_Load(sb is a StringBuilder):

C#
protected void Page_Load(object sender, EventArgs e)
{ 
 sb.Append(".....Page Content.....");
 sb.Append("<script runat="server" language="C#" type="text/C#">");
 sb.Append("protected void Page_Load(object sender, EventArgs e){");
 sb.Append("eventTypeBG_left = 'url(images/start/evtype_L.jpg)';");
 sb.Append("eventTypeBG_left_mid = url(images/start/evtype_LM.jpg)';");
 sb.Append("imgHead1.Attributes.Add('data-bg', eventTypeBG_left);");
 sb.Append("imgHead2.Attributes.Add('data-bg', eventTypeBG_left_mid);");
 sb.Append("} </script>");
 sb.Append(".....Page Content....");
 Response.Write(sb);
}


But it doesn't work. How can I achieve this?

Thanks.
Posted
Updated 27-Mar-16 20:55pm
v3
Comments
AspDotNetDev 25-Aug-10 14:03pm    
FYI, you have quotes inside your strings. You can do that, but not the way you're doing it. Your code should not even compile.
AspDotNetDev 25-Aug-10 17:05pm    
You are welcome. FYI, I deleted your "thank you" message because it was not an answer. You can post comments to answers by clicking "Add Comment" rather than "Add an Answer". Also, your other "answer" is not an answer either, but I already replied to it with something useful so I'm leaving it there.

First of all, I don't see why you are trying to output a Page_Load event handler inside of the Page_Load event handler. That will not work. You can't output code that will run on the server using Response.Write. Just put the code in your Page_Load handler.

Now, assuming you actually want to output some HTML to the page...

Use a <asp:Literal /> control and set the text property of that instead of using Response.Write. It's like a label, except it doesn't HTML encode the text (so that allows you to output HTML).
 
Share this answer
 
Comments
Ekjon 25-Aug-10 18:19pm    
Control myControl = FindControl("imgHead1"); can be used to find one created like that. But Control class doesn't have "Attributes". So I can't use the
statement:
imgHead1.Attributes.Add("data-bg", eventTypeBG_left);
(to dynamically change the background).
AspDotNetDev 25-Aug-10 18:31pm    
So cast it to the proper type. Or if you can't, replace that control with a new instance of the proper type based on the information in that control. Or, when the you originally dynamically create the control, add the properties then. Or clear out all the controls and recreate them during the page load.
First of all, don't hit 'answer' to post comments. Secondly, as someone else said, it's INSANE to use Response.Write in an ASP.NET page. You're not converting to ASP.NET otherwise, you're doing ASP inside the ASP.NET framework and doing nothing to take advantage of it, you may as well not convert it.
 
Share this answer
 
Comments
Ekjon 25-Aug-10 18:00pm    
Oh, yeah! Sorry. Like I used to do in message forums, and currently use some other sites that work the same way - as soon as I saw the big thing underneath, I just started typing there and hit the button.
No reading, or no connection between the text and the brain.
I spent lot of times wondering - why am I in the wrong job. But guess its too late, or may be I'll change some day. Till then - I'll be around bugging you guys. Thanks.

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