简体   繁体   English

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

[英]ASP.NET Best way to retain label.text value between postback with PageMethods

ASP.NET 2.0, PageMethods. 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.我正在使用 ASP.NET AJAX PageMethods 在我的页面上更改下拉列表时动态更改 Label 的文本。

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.我听说一种解决方案是将 label 内容保留在隐藏字段中,然后在 Page_Load 中的该字段中设置 Label 文本。

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.当下拉列表更改时,我想在 label 中放置那个人的电话。 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.但是,我认为进行完整的回发并不是最好的选择,所以我决定使用 PageMethod 获取电话,通过在下拉列表中选择的项目的 Id 来检索电话,并将其放在 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.我知道将它放在隐藏字段中,然后在有完整回发时将其设置回 Page_Load 中的 label 会起作用,但如果有其他解决方案,我会措辞。 Since WebMethods are marked as static, I cannot write Label.text = person.Telephone;由于 WebMethods 被标记为 static,我不能写 Label.text = person.Telephone; in them.在他们中。

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.由于您似乎有 ajax,您可以只进行部分回发,将数字写入 label 并写入视图状态,然后在 page_load 中将视图状态中的值写入 ZD304BA20E96D874341588EEABAC850。

In the DropDownList eventhandler:在 DropDownList 事件处理程序中:

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

And on PageLoad:在 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.如果您不想使用 Ajax,您可以在您的 aspx 文件中定义一个 HiddenInputField,使用 javascript 填充内容,然后在回发时使用该内容填充 label。 On aspx:在 aspx 上:

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

on PageLoad:在页面加载上:

myLabel.Text = myHiddenInput.Value;

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.唯一的重复就是调用您已有的相应代码。

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.我不确定您为什么认为这不是一个干净的解决方案,这是从客户端获取信息到服务器的好方法。

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?您是否考虑过将代码放入 Page_Load 以确保 label 反映了基于下拉列表值所需的值? 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.这样,在每次回发时,您至少可以确保获得正确的值,并且您不必担心将其存储在 2 个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM