【问题标题】:ASP.NET WebCustomControl event OnClick()ASP.NET WebCustomControl 事件 OnClick()
【发布时间】:2018-12-13 16:23:55
【问题描述】:

我想在 WebCustomControl 中将 OnClick 操作添加到我的按钮 (buttonSearch)。请帮助我,因为事件没有运行。 我尝试在谷歌搜索,但我没有找到任何东西:(

PS。对不起我的英语:)

非常感谢:)

namespace TestApp1.ctrls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl
    {

        private string searchText
        {
            get
            {
                if (ViewState["searchText"] == null)
                {
                    return "";
                }
                else
                {
                    return (String)ViewState["searchText"];
                }
            }
            set
            {
                ViewState["searchText"] = value;
            }
        }

        Panel searchPanel = new Panel() { Visible = true, Height = 62, Width = 321 };
        Label LabelSearch = new Label() { Text = "Wyszukaj: " };
        TextBox textBoxSearch = new TextBox() { Height = 16, Width = 184 };
        Button buttonSearch = new Button() { Text = "Wyszukaj", Width = 259 };
        //END PANEL SEARCH
        protected override void RenderContents(HtmlTextWriter output)
        {
            //Button search click event
            buttonSearch.Click += ButtonSearch_Click;
            searchPanel.Controls.Add(LabelSearch);
            searchPanel.Controls.Add(textBoxSearch);
            searchPanel.Controls.Add(new LiteralControl("<br />"));
            searchPanel.Controls.Add(buttonSearch);
            searchPanel.RenderControl(output);
        }

        private void ButtonSearch_Click(object sender, EventArgs e)
        {
            searchPanel.Visible = false;
        }
    }
}

【问题讨论】:

  • 如何在标记上实现自定义控件?页面加载后,您是否在浏览器中看到任何错误?
  • 我没有任何错误。只有在点击按钮后我没有任何动作:(

标签: asp.net asp.net-customcontrol


【解决方案1】:

我有一个解决方案:) INamingContainer :)

public class WebCustomControl1 : WebControl, INamingContainer

【讨论】:

    【解决方案2】:

    您似乎没有添加 EventHandler。类似的东西:

    buttonSearch.Click += new EventHandler(this.ButtonSearch_Click);
    

    【讨论】:

    • 问题仍然是没有事件发生?
    猜你喜欢
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多