0

ユーザー コントロールを作成しようとしています。コントロールは 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));

        }

    }
}
4

1 に答える 1

0

ポストバックが発生したときにすべてのコントロールを再作成しないようにする必要があります。追加するだけです:

protected override void CreateChildControls()
{                  
    base.CreateChildControls();
    if(this.IsPostBack == false)
    {
        //Your code which adds panels etc.
    }
}
于 2012-06-14T12:27:42.773 に答える