views:

3427

answers:

7

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!

A: 

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.

Daniel
A: 

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.

Kelsey
I'm not sure the poster wants the label to reflect the value of the drop down list.
Ash Machine
It sounds like he wants the text to reflect something based on what is selected by the dropdownlist. The PostBack could just do the same logic each time.
Kelsey
A: 

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.

eglasius
A: 

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.

Cedric Aube
+1  A: 

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.Text;

could be .Value instead of .Text i'm not sure at the moment.

gsnerf
A: 

gsnerf,

Thank you for your input. Actually, I was just wondering if there was another way than setting myLabel.Text = myHiddenInput.Text (or (string)ViewState["phone"]) in Page_Load

Cedric Aube
A: 

Freddy,

'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.'

Since WebMethods have to be static, how do I proceed to call, let's say, DropDownList1_SelectIndexChanged(), which is non static?

Cedric Aube