1
我想創建一個實現IPostBackDataHandler的標籤,因爲我想用javascript更改文本。如果我在此之後觸發回發,那麼比我的文本消失。創建一個實現IPostBackDataHandler的自定義標籤
,我已經擁有的代碼是這樣的:
public class CustomLabel : Label, IPostBackDataHandler
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (Page != null)
Page.RegisterRequiresPostBack(this);
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
this.Text = postCollection[postDataKey];
return true;
}
public void RaisePostDataChangedEvent()
{
//throw new NotImplementedException();
}
}
它不工作,我不明白我怎麼應該看到文本更改和PostCollection [postDataKey]總是空。
謝謝,我以爲我能夠實現IPostBackDataHandler,但我錯誤地得到這個工作。我現在要使用一些隱藏的域。 – Marlou
隱藏字段不會解決您的問題。您需要在視圖狀態下跟蹤控件。儘管你已經從Label控件繼承,所以它應該非常簡單。 –