ASP.NET 2.0,PageMethods。
ASP.NET 2.0, PageMethods.
你好,
我使用ASP.NET AJAX PageMethods动态更改标签的文本时一个DropDownList是在我的页面更改。
I'm using ASP.NET AJAX PageMethods to dynamically change the text of a Label when a dropdownlist is changed on my page.
我知道,回发之间将不会保留标签的文本时,他们的客户端,这是我的情况正在发生变化。我听说一个解决办法是保持标签内容在一个隐藏字段,然后在Page_Load中设置该字段标签文本。
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?
感谢您!
只是为了澄清,我有名字的人一个DropDownList。当DropDownList中得到改变,我想,在一个标签,把那个人的电话。但是,我认为,做一个完整的回发是不是真的是最好的选择,所以我决定得到一个PageMethod的电话,通过在DropDownList检索电话所选项目的ID,而把它的标签。
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.
然而,由于其他控件导致完全回传,我失去了对每一个回发的电话。我知道,把它在一个隐藏字段,然后将其设置回在Page_Load中的标签时,有一个完整的回发会的工作,但我wordering如果有另一种解决方案。由于的WebMethods标记为静态的,我不能写Label.text = person.Telephone;在其中。
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.
至于你似乎有阿贾克斯,你可以只是做一个局部回传,写的号码标签,进入视图状态,并在Page_Load中写入从价值视图状态的标签。
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.
在DropDownList的事件处理程序:
In the DropDownList eventhandler:
string phone = <.. get phone number ...>;
myLabel.Text = phone;
ViewState["currentPhone"] = phone;
和上pageLoad的:
And on PageLoad:
myLabel.Text = (ViewState["currentPhone"] != null) ? (string)ViewState["currentPhone"] : string.Empty;
如果你不想使用Ajax你可以在你的aspx文件中定义一个HiddenInputField,JavaScript和上回发填充内容填充内容的标签。 在ASPX:
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" />
在pageLoad的:
on PageLoad:
myLabel.Text = myHiddenInput.Text;
可以.value的替代。文中我不知道的时刻。
could be .Value instead of .Text i'm not sure at the moment.