我正在尝试编写用户控件。该控件将用于SharePoint WebPart。在大多数情况下,控件工作,问题是控件无法识别按钮被单击。按钮事件不会触发。代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CU_Controls
{
public class CheckBoxListDropDown : CheckBoxList
{
//First row
public string Title { get; set; }
private const string defaultCssResource = "CU_Controls.EmbeddedResources.DefaultStyles.css";
//Expand or hide on start
public bool OpenOnStart { get; set; }
protected Panel cbddPanel;
protected Panel headerPanel;
protected Literal headerText;
protected Image headerImage;
protected Panel listPanel;
protected Panel footerPanel;
protected Button okayButton;
protected Button cancelButton;
public event EventHandler ButtonClickDemo;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
this.CssClass = "cbddList";
cbddPanel = new Panel();
cbddPanel.CssClass = "cbddPanel";
cbddPanel.ID = this.ClientID + "_cbddPanel";
cbddPanel.EnableViewState = true;
headerPanel = new Panel();
headerPanel.ID = this.ClientID + "_headerPanel";
headerPanel.CssClass = "cbddHeaderPanel";
headerPanel.EnableViewState = true;
headerText = new Literal();
headerText.EnableViewState = true;
headerPanel.Controls.Add(headerText);
headerImage = new Image();
headerImage.ID = base.ClientID + "_arrowDown";
headerImage.CssClass = "cbddArrowDown";
headerImage.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "CU_Controls.EmbeddedResources.button.gif");
headerImage.EnableViewState = true;
headerPanel.Controls.Add(headerImage);
cbddPanel.Controls.Add(headerPanel);
listPanel = new Panel();
listPanel.ID = base.ClientID + "_listPanel";
listPanel.CssClass = "cbddListPanel";
listPanel.EnableViewState = true;
cbddPanel.Controls.Add(listPanel);
footerPanel = new Panel();
footerPanel.ID = base.ClientID + "_footerPanel";
footerPanel.CssClass = "cbddFooterPanel";
footerPanel.EnableViewState = true;
okayButton = new Button();
okayButton.ID = this.ClientID + "_cbddOkayButton";
okayButton.CssClass = "cbddOkayButton";
okayButton.Text = "Ok";
okayButton.Click += new EventHandler(okayButton_Click);
okayButton.EnableViewState = true;
footerPanel.Controls.Add(okayButton);
}
void okayButton_Click(object sender, EventArgs e)
{
ButtonClickDemo(sender, e);
}
protected override void Render(HtmlTextWriter writer)
{
cbddPanel.RenderBeginTag(writer);
headerText.Text = this.Title;
headerPanel.RenderControl(writer);
listPanel.RenderBeginTag(writer);
base.Render(writer);
footerPanel.RenderControl(writer);
listPanel.RenderEndTag(writer);
cbddPanel.RenderEndTag(writer);
string jqueryToggleFunction = @"
<script type=""text/javascript"">
$(document).ready(function () {{
$('.{0} td:even').css('background-color', '#efefef');
$('#{1}').live('click', function(){{ $('#{2}').toggle('fast'); }});
$('#{3}').live('click', function(){{ $('#{2}').toggle('fast'); __doPostBack('{3}',''); return false; }});
}});
</script>";
writer.Write(string.Format(jqueryToggleFunction, base.CssClass, headerImage.ClientID, listPanel.ClientID, okayButton.ClientID));
}
}
}
答案 0 :(得分:0)
您需要确保在触发回发时不重新创建所有控件。 只需添加:
protected override void CreateChildControls()
{
base.CreateChildControls();
if(this.IsPostBack == false)
{
//Your code which adds panels etc.
}
}