ASP.NET 在使用 PageMethods 回发之间保留 label.text 值的最佳方法

发布于 2024-07-15 17:12:26 字数 599 浏览 11 评论 0原文

ASP.NET 2.0,页面方法。

美好的一天,

我正在使用 ASP.NET AJAX PageMethods 在页面上的下拉列表更改时动态更改标签的文本。

我知道当在客户端更改标签的文本时,标签的文本不会在回发之间保留,这就是我的情况。 我听说解决方案是将标签内容保留在隐藏字段中,然后在 Page_Load 中设置该字段中的标签文本。

然而,这个解决方案对我来说似乎并不干净。 还有其他替代方案或最佳实践吗?

谢谢你!


只是为了澄清一下,我有一个包含人员姓名的下拉列表。 当下拉列表更改时,我想在标签中放置该人的电话。 但是,我认为进行完整的回发并不是最好的选择,因此我决定使用 PageMethod 获取电话,传递下拉列表中所选项目的 Id 来检索电话,并将其放入标签中。

但是,由于其他控件会导致完整回发,因此我在每次回发时都会丢失电话。 我知道将其放在隐藏字段中,然后在有完整回发时将其设置回 Page_Load 中的标签是可行的,但如果有其他解决方案,我正在措辞。 由于 WebMethods 被标记为静态,因此我无法编写 Label.text = person.Telephone; 在他们中。

ASP.NET 2.0, PageMethods.

Good day,

I'm using ASP.NET AJAX PageMethods to dynamically change the text of a Label when a dropdownlist is changed on my page.

I know that the text of Labels are not retained between postbacks when they are changed on client-side, which is my case. I've heard that a solution is to keep the label content in a hidden field, then to set Label text from that field in Page_Load.

However, this solution does not seem really clean to me. Are there any other alternatives or best practices?

Thank you!


Just to clarify, I have a dropdownlist with people names. When the dropdownlist get changed, I want, in a label, to put the telephone of that person. However, I thought that doing a full postback was not really the best alternative, so I decided to get the telephone with a PageMethod, passing the Id of the item selected in the dropdownlist to retrieve the telephone, and the put it in the label.

However, since other controls cause a full postback, I lose the telephone on every postback. I know that putting it in a hidden field, then setting it back to the label in Page_Load when there is a full postback would work, but I was wordering if there was another solution. Since WebMethods are marked as static, I cannot write Label.text = person.Telephone; in them.

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

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

发布评论

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

评论(4

胡渣熟男 2024-07-22 17:12:26

由于您似乎有ajax,因此您可以只进行部分回发,将数字写入标签并写入视图状态,然后在 page_load 中将视图状态中的值写入标签。

在 DropDownList 事件处理程序中:

string phone = <.. get phone number ...>;
myLabel.Text = phone;
ViewState["currentPhone"] = phone;

在 PageLoad 上:

myLabel.Text = (ViewState["currentPhone"] != null) ? (string)ViewState["currentPhone"] : string.Empty;

如果您不想使用 Ajax,您可以在 aspx 文件中定义一个 HiddenInputField,用 javascript 填充内容,并在回发时用内容填充标签。
在 aspx 上:

<asp:HiddenInputField runat="server" ID="myHiddenInput" />

在 PageLoad 上:

myLabel.Text = myHiddenInput.Value;

As you seem to have ajax you could just do a partial postback, write the number to the label and into the viewstate, and in page_load write the value from the viewstate to the label.

In the DropDownList eventhandler:

string phone = <.. get phone number ...>;
myLabel.Text = phone;
ViewState["currentPhone"] = phone;

And on PageLoad:

myLabel.Text = (ViewState["currentPhone"] != null) ? (string)ViewState["currentPhone"] : string.Empty;

If you don't want to use Ajax you can define a HiddenInputField in your aspx file, fill the content with javascript and on Postback fill the label with the content.
On aspx:

<asp:HiddenInputField runat="server" ID="myHiddenInput" />

on PageLoad:

myLabel.Text = myHiddenInput.Value;
耳根太软 2024-07-22 17:12:26

我建议坚持使用隐藏字段解决方案,这样您就可以将逻辑保留在一处。

另一种方法是使用更新面板而不是页面方法,但我不会这样做,因为这对性能不太好。

正如其他人所说,第三种替代方法是每当下拉列表的选定值发生更改时在服务器端运行相同的逻辑。 唯一的重复就是调用您已有的适当代码。

I suggest to stick with the hidden field solution, so you can keep your logic in one place.

An alternative is to use an update panel instead of page methods, but I wouldn't do that because that isn't as good for performance.

As others have said, a third alternative is to run the same logic on the server side, whenever the selected value for the drop down has changed. The only duplication is just to make a call to the appropriate code you already have.

寒尘 2024-07-22 17:12:26

如果标签值由下拉列表的值确定,您始终可以在服务器上复制该逻辑。

如果该逻辑太复杂或者您不想在多个位置维护它,那么下一个最佳选择就是您建议的。 我不确定为什么您认为这不是一个干净的解决方案,这是从客户端向服务器获取信息的好方法。

If the labels value is determined by the value of the drop down you could always duplicate that logic on the server.

If that logic is too complex or you don't want to maintain it it multiple places then the next best option is what you suggested. I'm not sure why you think this is not a clean solution, this is a great way to get information from the client to the server.

下壹個目標 2024-07-22 17:12:26

您是否考虑过将代码放入 Page_Load 中以确保标签反映基于下拉列表值所需的值? 这样,在每次回发时,您至少可以确保获得正确的值,并且不必担心将其存储在两个位置。

Have you considered just putting the code in the Page_Load to make sure that the label is reflecting the value needed based on the dropdownlist value? This way on each postback you will at least be sure to get the proper value and you don't have to worry about storing it in 2 places.

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