Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

how to display variable value in asp.net which is set in Page_Load function

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First way

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

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

and on code behind.

txtValueA.Text = "one of the basic";

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

Second way

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";

and on aspx page

<%=cMyValue%>

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.

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());
%>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...