创建实现IPostBackDataHandler自定义标签 [英] Create a custom label that implements IPostBackDataHandler

查看:153
本文介绍了创建实现IPostBackDataHandler自定义标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个实现IPostBackDataHandler,因为我想改变使用JavaScript的文本标签。如果我以后触发回传,比我的文字消失了。

I want to create a label that implements the IPostBackDataHandler, because I want to change the text with javascript. If I trigger a postback after that, than my text is gone.

在code,我已经是这样的:

The code that I already have is this:

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]总是空。

It is not working at all, I don't get how I should see that the text is changed and PostCollection[postDataKey] is always null.

推荐答案

IPostBackDataHandler 接口是用于输入。像跨度和div的元素没有得到存储在请求对象。我只想实施必要的ViewState的管理方法。下面是我开发了一个自定义的网格组件的例子:

The IPostBackDataHandler interface is intended for inputs. Elements like spans and divs don't get stored in the request object. I would just implement the necessary ViewState management methods. Here's an example from a custom grid component that I developed:

protected override void LoadViewState(object savedState)
{
    if (savedState != null)
    {
        object[] state = (object[])savedState;

        if (state[0] != null)
            base.LoadViewState(state[0]);
        if (state[1] != null)
            ((IStateManager)ItemStyle).LoadViewState(state[1]);
        if (state[2] != null)
            ((IStateManager)headerStyle).LoadViewState(state[2]);
        if (state[3] != null)
            ((IStateManager)AlternatingItemStyle).LoadViewState(state[3]);
    }
}

protected override object SaveViewState()
{
    object[] state = new object[4];

    state[0] = base.SaveViewState();
    state[1] = itemStyle != null ? ((IStateManager)itemStyle).SaveViewState() : null;
    state[2] = headerStyle != null ? ((IStateManager)headerStyle).SaveViewState() : null;
    state[3] = alternatingItemStyle != null ? ((IStateManager)alternatingItemStyle).SaveViewState() : null;

    return state;
}

protected override void TrackViewState()
{
    base.TrackViewState();

    if (itemStyle != null)
        ((IStateManager)itemStyle).TrackViewState();
    if (headerStyle != null)
        ((IStateManager)headerStyle).TrackViewState();
    if (alternatingItemStyle != null)
        ((IStateManager)alternatingItemStyle).TrackViewState();
}

这篇关于创建实现IPostBackDataHandler自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆