我是 WebControl / CompositeControl 世界的新手,我正在玩一个小测试类。它只是一个 LinkButton ,点击后会更新。当我离开 UpdatePanel 时,事情很有效。但是当我尝试在里面运行它时,我仍然得到一个完整的POST响应。如何在 UpdatePanel 中使这个类工作?
这是班级:
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);
}
}
OnInit()
中的代码会使控件正确发布,但我没有获得 LinkButton 的更新文本。它仍在触发事件 - 当我调试时,我可以看到它被调用。设置此控件以在 UpdatePanel 中使用的正确方法是什么?
用法,以防万一:
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<cc:Test2 ID="jqTest02" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
答案 0 :(得分:1)
您必须为该按钮指定一个ID属性...这用于驱动UpdatePanel
的客户端javascript。更具体地说,它列在控件列表中,用于拦截和执行异步回发。
testLinkButton.ID = "btn";