ASP的CompositeControl&安培;的ScriptManager [英] ASP CompositeControl & ScriptManager
问题描述
我真的很新来的的的WebControl / CompositeControl的的世界,我有一个小测试类我与播放。这只是一个的的LinkButton 的该按钮时更新。工作的事情伟大的,当我离开它的的的UpdatePanel 的。但是,当我尝试运行它里面我仍然得到一整页的POST响应。我怎样才能让这里面类工作的的UpdatePanel 的?
下面是类:
公共类的Test2:CompositeControl的
{
私人静态只读对象testButtonEvent =新的对象(); 公共事件的EventHandler OnTestClick
{
添加{Events.AddHandler(testButtonEvent,值); }
除去{Events.RemoveHandler(testButtonEvent,值); }
} 私人LinkButton的testLinkButton; 公共虚拟字符串testLinkButtonText
{
得到
{
对象o =的ViewState [testLinkButtonText];
返回(O == NULL)?的String.Empty:(字符串)O;
}
组
{
如果(价值== NULL)
ViewState.Remove(testLinkButtonText);
其他
的ViewState [testLinkButtonText] =值;
}
} 保护覆盖无效的OnInit(EventArgs的发送)
{
/ *这东西使得阿贾克斯友好的,但停止文本渲染
EnsureChildControls(); ScriptManager的SCMAN = ScriptManager.GetCurrent(页);
如果(SCMAN!= NULL)
{
ScMan.RegisterAsyncPostBackControl(testLinkButton);
} * / base.OnInit(E);
} 保护覆盖无效的CreateChildControls()
{
Controls.Clear(); testLinkButton =新的LinkButton();
testLinkButton.Command + =新CommandEventHandler(testClick);
testLinkButtonText =测试的ViewState文本; Controls.Add被(testLinkButton);
} 无效testClick(对象发件人,CommandEventArgs E)
{
testLinkButtonText =更新的案文+ DateTime.Now.ToLongTimeString();
} 保护覆盖无效渲染(HtmlTextWriter的作家)
{
RenderContents(作家);
} 保护覆盖无效RenderContents(HtmlTextWriter的作家)
{
EnsureChildControls(); testLinkButton.Text = testLinkButtonText;
testLinkButton.RenderControl(作家);
}}
在code在的OnInit()
使控制正确张贴,但我不明白的的的LinkButton 。它仍然发射了事件 - 当我调试我可以看到它被调用。什么是设置此控制了使用的正确方法的的UpdatePanel 的?
使用,以防万一:
< ASP:的UpdatePanel =服务器的UpdateMode =条件>
<&的ContentTemplate GT;
< CC:Test2的ID =jqTest02=服务器/>
< /&的ContentTemplate GT;
< / ASP:的UpdatePanel>
您必须给该按钮的ID属性...这是在客户端JavaScript,驱动的UpdatePanel 使用code>。更具体地说,它在控制列表的上市拦截并做异步回发。
testLinkButton.ID =BTN;
I'm really new to the WebControl / CompositeControl world, and I have a small test class I am playing with. It's just a LinkButton that updates when clicked. Things work great when I leave it out of UpdatePanel. But when I try to run it inside I still get a full page POST response. How can I make this class work inside a UpdatePanel?
Here's the class:
public class Test2 : CompositeControl
{
private static readonly object testButtonEvent = new object();
public event EventHandler OnTestClick
{
add { Events.AddHandler(testButtonEvent, value); }
remove { Events.RemoveHandler(testButtonEvent, value); }
}
private LinkButton testLinkButton;
public virtual string testLinkButtonText
{
get
{
object o = ViewState["testLinkButtonText"];
return (o == null) ? String.Empty : (string)o;
}
set
{
if (value == null)
ViewState.Remove("testLinkButtonText");
else
ViewState["testLinkButtonText"] = value;
}
}
protected override void OnInit(EventArgs e)
{
/* This stuff makes it ajax friendly but stops the text rendering
EnsureChildControls();
ScriptManager ScMan = ScriptManager.GetCurrent(Page);
if (ScMan != null)
{
ScMan.RegisterAsyncPostBackControl(testLinkButton);
} */
base.OnInit(e);
}
protected override void CreateChildControls()
{
Controls.Clear();
testLinkButton = new LinkButton();
testLinkButton.Command += new CommandEventHandler(testClick);
testLinkButtonText = "Test ViewState Text";
Controls.Add(testLinkButton);
}
void testClick(object sender, CommandEventArgs e)
{
testLinkButtonText = "Updated Text On " + DateTime.Now.ToLongTimeString();
}
protected override void Render(HtmlTextWriter writer)
{
RenderContents(writer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
EnsureChildControls();
testLinkButton.Text = testLinkButtonText;
testLinkButton.RenderControl(writer);
}
}
The code in OnInit()
causes the control to post correctly, but I don't get the updated text for the LinkButton. It is still firing off the event - when I debug I can see it being called. What's the proper way to set this control up for use in a UpdatePanel?
Usage, just in case:
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<cc:Test2 ID="jqTest02" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
You have to give the button an ID property...this is used in the client-side javascript that drives the UpdatePanel
. More specifically, it's listed in the list of controls to intercept and do async postbacks for.
testLinkButton.ID = "btn";
这篇关于ASP的CompositeControl&安培;的ScriptManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!