问题
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.
回答1:
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;
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/705615/asp-net-best-way-to-retain-label-text-value-between-postback-with-pagemethods