更新面板和标签

发布于 2024-07-26 22:22:58 字数 343 浏览 4 评论 0原文

我有一个更新面板,其中有一些控件。 例如,我有一个标签、一个文本框和一个回发按钮。

我的标签文本是从资源文件中检索的,因此,在页面加载期间我执行以下

Page_Load()
{
    If(!isPostBack)
    {
        Label.Text = //Resource value;
    }    
}

操作 问题是,通过按钮单击触发器发回后,标签的文本消失了,因为我认为必须再次设置资源?

关于摆脱这种多余的标签回发有什么建议吗? 因为我有多个控件,所以很难将所有文本框包装在一个更新面板等中......

干杯

I have an update panel with some controls in it. for example, I have a label, a textbox and a button to postback.

My label text is retrieved from the resource file, therefore, during page load I do the following

Page_Load()
{
    If(!isPostBack)
    {
        Label.Text = //Resource value;
    }    
}

Problem is, after posting back via the button click trigger, the label's text disappeared, as I assume the resource must be set again?

Any advice on getting rid of this redundant label postbacks? Because I have multiple controls, its hard to wrap all textboxes in one update panel etc...

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的夜不怕黑 2024-08-02 22:22:58

除非您手动关闭标签或父控件的视图状态,否则标签文本值将保留在视图状态中。 如果启用了视图状态,则不必重新分配标签的文本。

如果您不希望使用视图状态,但要从资源动态分配值,则每次都需要手动设置标签的文本属性。 被回发的页面不知道运行时更改的标签值,除非它保持在视图状态中。

为控件生成的 ViewState 很重,因为它还存储标签控件的其他属性。 如果除了值之外没有任何变化,您可能需要考虑仅将值存储在 ViewState 对象中,并关闭标签控件的视图状态,并在每次回发时手动进行连接。

// Store it
ViewState["YourLabel"] = "Text you want to store in the label.";

// On postback make sure you are assigning it
YourLabel.Text = Convert.ToString(ViewState["YourLabel"]);

我认为您不会找到自动执行此操作的方法。 您可以编写一个辅助函数,以便在每次回发时自动执行此操作,方法是迭代 ViewState 中的项目,并假设您已将该键命名为与控件相同的名称,然后搜索具有该键名称的任何标签控件,然后根据为该键存储的值自动分配文本。 我从来没有真正尝试过这个,但它可能会以牺牲一点速度为代价。

Label text values are maintained in the viewstate unless you have manually turned of viewstate for the label or a parent control. You shouldn't have to re-assign the label's text if the viewstate is enabled.

If you don't want the viewstate to be used but you are assigning the value dynamically from a resource then you will need to set the label's text property manually each time. The page being posted back has no knowledge of label values that are changed at runtime unless it is maintain in the viewstate.

ViewState produced for a control is heavy as it also stores other attributes of the label control. If nothing other than the value is changing, you might want to consider just storing the value in the ViewState object and turning off viewstate for the label controls and doing the wiring up manually on each postback.

// Store it
ViewState["YourLabel"] = "Text you want to store in the label.";

// On postback make sure you are assigning it
YourLabel.Text = Convert.ToString(ViewState["YourLabel"]);

I don't think you will find an automatic way of doing this. You could maybe write a helper function to do it automatically each time there is a postback by iterating through the items in the ViewState and assuming you have named the key the same as your controls, do a search for any label controls with the key's name and assign the text automatically based on the value stored for that key. I have never actually tried this but it probably would work at the expense of a little speed.

会发光的星星闪亮亮i 2024-08-02 22:22:58

您可以使用以下任一方法:

  • 关闭该控件的 ViewState。 EnableViewState="false" 并在每次页面加载时在代码中设置它。 如果这只是像你说的那样从资源文件加载的字符串,那么不会有任何性能问题,所以不用担心。
  • 以声明方式设置控件的值(在 .aspx 文件中)。 您可以使用 <%$ ... %> 绑定语法。
  • Pre_init 阶段(在 ViewState 加载/开始跟踪之前)设置代码中的值。

如果您在 Web 服务器级别使用页面压缩,那么向每个响应添加几个字节的文本并不是什么大问题。 如果此标签在中继器或类似设备中使用 1000 次,它只会变得很糟糕。

You may use any of the following methods:

  • Turn off ViewState for that control. EnableViewState="false" and set it in code on every page load. If this is just a string loaded from a resource file like you say, there won't be any performance problem, so no worries there.
  • Set the value of the control declaratively (in the .aspx file). You can use the <%$ ... %> binding syntax.
  • Set the value in code in the Pre_init phase (which is before ViewState is loaded/started tracking).

And if you're using page compression at the web server level as you should, then adding a few bytes of text to every response isn't a big deal. It would only get bad if this label is used 1000 times in a repeater or something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文