当前位置: 代码迷 >> ASP.NET >> IPostBackDataHandler?解决思路
  详细解决方案

IPostBackDataHandler?解决思路

热度:3848   发布时间:2013-02-26 00:00:00.0
IPostBackDataHandler?
我想要实现一个自定义控件,不继承自某一个带有回送的webcontrol,同时有自己的属性:SelectID。
页面调用
<MyNameSpace:MyControl   runat= "server "   ID= "MyControl1 "     SkinName= "Skin1.ascx "   SelectID= '12 '> </MyNameSpace:MyControl>
正常
如果将控件放在repeater内
<asp:Repeater   ID= "DefaultList "   runat= "server "   >
                        <ItemTemplate>
                                                              <MyNameSpace:MyControl   runat= "server "   ID= "MyControl1 "     SkinName= "Skin1.ascx "   SelectID= ' <%#   DataBinder.Eval(Container.DataItem,   "SelectID ")   %> '> </MyNameSpace:MyControl>                         </ItemTemplate>        
                         
                </asp:Repeater>

就不正常,SelectID得不到值,看有大大说和IPostBackDataHandler有关,是否确实?如果确实,该如何实现这个回送接口。

------解决方案--------------------------------------------------------
[Bindable(true)]public string SelectedID{ get {}set{}}
------解决方案--------------------------------------------------------
楼上的Bindable特性是把SelectID设成可绑定的,这样才能在ItemTemplate里用Eval绑定,

------解决方案--------------------------------------------------------
微软的例子
---------------------------------------------
using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;


namespace CustomWebFormsControls {

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name= "FullTrust ")]
public class MyTextBox: Control, IPostBackDataHandler {


public String Text {
get {
return (String) ViewState[ "Text "];
}

set {
ViewState[ "Text "] = value;
}
}


public event EventHandler TextChanged;


public virtual bool LoadPostData(string postDataKey,
NameValueCollection postCollection) {

String presentValue = Text;
String postedValue = postCollection[postDataKey];

if (presentValue == null || !presentValue.Equals(postedValue)) {
Text = postedValue;
return true;
}

return false;
}


public virtual void RaisePostDataChangedEvent() {
OnTextChanged(EventArgs.Empty);
}


protected virtual void OnTextChanged(EventArgs e) {
if (TextChanged != null)
TextChanged(this,e);
}


protected override void Render(HtmlTextWriter output) {
output.Write( " <INPUT type= text name = "+this.UniqueID
+ " value = " + this.Text + " > ");
}
}
}
------解决方案--------------------------------------------------------