如何显示在asp.net中变量的值是在Page_Load中设置功能 [英] how to display variable value in asp.net which is set in Page_Load function

查看:395
本文介绍了如何显示在asp.net中变量的值是在Page_Load中设置功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量这是我从数据库中获取我想要输出aspx页面这个变量在html之间。
我试图把它公开,但它显示了一些错误,我怎么可以在页面加载功能设置的变量,在它的aspx页面?

I have a variable which I get from database I want to output this variable in the aspx page in between the html. I tried to make it public but it shows some error, how can I use a variable set in page load function in its aspx page?

推荐答案

您放置在点文字控制你赢来显示变量,然后你pageLoad的设置这个值文本控件

First way

You place a Literal control in the point you won to display the variable and then you set on PageLoad this value to the Literal Control

<asp:Literal runat="server" id="txtValueA" EnableViewState="false" />

和code的后面。

and on code behind.

txtValueA.Text = "one of the basic";

这种方式你在你尝试呈现什么也更多的控制。

This way you have also more control over what you try to render out.

另一种方式是把它公开,你说的和打印时,页面呈现。这是一种不同的方式,但不是一个更好的。

The other way is to make it public as you say and print it when the page render. This is a different way, but not a better one.

public string cMyValue = "some string here";

和aspx页面上

<%=cMyValue%>

这样你得到的参数时,页面呈现在运行时和发送这个在运行时间上的客户端。
如果您尝试使用UpdatePanel内这种方式,那么更新失败由于更新面板无法读取并再次渲染整个页面,但只有code的后面。

This way you get the parameter when the page renders at run time and send this in run time on the Client. If you try this way inside an updatepanel, then the update is fail because the update panel can not read and render again the full page but only the code behind.

尽量避免这种方式,并使用它,只有当你真正需要它,因为这样做的$ C $一个呼叫c的那一刻它尝试呈现页面,更改页面...

Try to avoid this way, and use it only when you really needed it because this make a call to the code the moment its try to render the page and change the page...

用这种方式的一个想法是,当你有你喜欢做一些额外的计算,你刷新的内容,然后调用额外的时间成本的函数。例如。

One idea to use this way is when you have some extra calculation that you like to make, you flush the content and then you call a function with extra time cost. For example.

<%
  Response.Flush();
  Response.Write(CallATimeConsumeFunctionThatReturnString());
%>

这篇关于如何显示在asp.net中变量的值是在Page_Load中设置功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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