使用Response.Write创建页面时为Page_Load编写代码 [英] Writing code for Page_Load when creating page with Response.Write

查看:82
本文介绍了使用Response.Write创建页面时为Page_Load编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有这个经典的ASP页面,从数据库中获取其样式/内容。我正在将它转换为ASP .NET C#。

所以,我在 Page_Load中使用了所有 Response.Write



如果有使用 Response.Write 创建的控件 - 它不能在代码隐藏文件中直接引用。所以 - 如果我需要在 Page_Load 中添加一些代码 - 我在 Page_Load sb 是一个 StringBuilder ):



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):

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.

推荐答案

首先,我看不到为什么要尝试在Page_Load事件处理程序中输出Page_Load事件处理程序。那样不行。您无法使用Response.Write输出将在服务器上运行的代码。只需将代码放在Page_Load处理程序中即可。



现在,假设您确实想要向页面输出一些HTML ...



使用< asp:Literal /> 控制并设置其文本属性,而不是使用Response.Write。它就像一个标签,除了它不对文本进行HTML编码(这样就可以输出HTML)。
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).


首先,不要点击回答发表评论。其次,正如其他人所说,在ASP.NET页面中使用Response.Write是INSANE。你没有转换到ASP.NET,否则,你在ASP.NET框架内做ASP并且什么都不做以利用它,你也可以不转换它。
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.


这篇关于使用Response.Write创建页面时为Page_Load编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆