开发者

Eval ViewState value on a server control tag?

开发者 https://www.devze.com 2023-04-12 12:46 出处:网络
Is there a way to Eval the ViewState value on a server contro开发者_开发技巧l tag? <asp:TextBoxText=\'<%#Eval(Viewstate[\"key\"])%>\' runat=\'server\' />

Is there a way to Eval the ViewState value on a server contro开发者_开发技巧l tag?

<asp:TextBox    Text='<%#Eval(Viewstate["key"])%>' runat='server' />

Thanks.


Just add a watch for that ViewState value, or set a breakpoint and use the Immediate Window.


edit Changed answer to better suit your requirement to set visibility on a control.

Sticking a databind() in the code behind after setting the Viewstate value will work. e.g.

aspx.cs : -

protected void Page_Load(object sender, EventArgs e)
{

    ViewState["Test"] = "hello";
    DataBind();
}

aspx : -

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtHello" runat="server" visible='<%#  ViewState["Test"].ToString()    == "hello"  %>' Text="Show Hello"></asp:TextBox>
</div>
</form>
</body>

will "show hello" but if you change the viewstate value to something else it won't.


You can do it like this:

<asp:Label runat="server" Text='<%# ViewState["key"] %>'/>

The <%# %> annotation means whatever is within provides access to codebehind. In this case, it will access ViewState and retrieve the item saved under "key". As a result, the @Text of the label will be whatever the value of ViewState["key"] is.

0

精彩评论

暂无评论...
验证码 换一张
取 消