Button in WebCustomControl don't fire the onclick event, why?

Discussion in 'ASP .Net Building Controls' started by Stefan, Jul 8, 2003.

  1. Stefan

    Stefan Guest

    Maybe someone sees my mistake? The Button-OnClick-Event never fires...


    --------------------------------------------------------
    The code from the WebCostumControl:
    --------------------------------------------------------
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using artiso_lib;
    namespace artiso_lib
    {
    [DefaultProperty("IndexserverCatalog"), ToolboxData("<{0}:IndexserverSearch
    runat=server></{0}:IndexserverSearch>")]
    public class IndexserverSearch : System.Web.UI.WebControls.WebControl
    {
    [Bindable(true), Category("Appearance"), DefaultValue(""),
    EditorBrowsable(EditorBrowsableState.Always), Description("Name des
    Indexserver-Katalogs.")]
    public string IndexserverCatalog
    {
    get {return ViewState["IndexserverCatalog"].ToString();}
    set {ViewState["IndexserverCatalog"] = value;}
    }
    [Bindable(true), Category("Appearance"), DefaultValue(""),
    Description("Maximale Anzahl der Suchergebnisse. Keine Angabe für alle
    Suchergebnisse.")]
    public int MaxSearchResults
    {
    get {return (int)ViewState["MaxSearchResults"];}
    set {ViewState["MaxSearchResults"] = value;}
    }
    [Bindable(true), Category("Appearance"), DefaultValue("Styles.css"),
    Description("StyleSheet Datei für die Formatierung des DataGrids.")]
    public string StyleSheetFile
    {
    get {return ViewState["StyleSheetFile"].ToString();}
    set {ViewState["StyleSheetFile"] = value;}
    }
    [Bindable(true), Category("Appearance"), DefaultValue("fdgsd"),
    Description("Spalten die vom Indexserver gelesen werden sollen.")]
    public string Columns
    {
    get {return ViewState["GetColumns"].ToString();}
    set {ViewState["GetColumns"] = value;}
    }
    [Bindable(true), Category("Appearance"), DefaultValue("Suche"),
    Description("Beschriftung des \"Suche\" Buttons.")]
    public string SearchButtonText
    {
    get {return ViewState["SearchButtonText"].ToString();}
    set {ViewState["SearchButtonText"] = value;}
    }
    public DataTable Search(string SearchString)
    {
    Cisso.CissoQueryClass cqc = new Cisso.CissoQueryClass();
    cqc.Catalog = IndexserverCatalog;
    cqc.MaxRecords = MaxSearchResults;
    cqc.CiFlags = "SHALLOW";
    cqc.Query = SearchString;
    cqc.Columns = Columns;
    cqc.SortBy = "Rank[d]";
    ADODB.Recordset rsIX =
    (ADODB.Recordset)cqc.CreateRecordset("nonsequential");
    OleDbDataAdapter daConvertToDataset = new OleDbDataAdapter();
    DataSet ds = new DataSet();
    daConvertToDataset.Fill(ds, rsIX, "indexServerDirectories");
    if (ds.Tables["indexServerDirectories"].Rows.Count == 0)
    {
    Page.RegisterClientScriptBlock("NoSearchResult", "<script
    language='javascript'>alert('Die Suche konnte keine entsprechenden Dokumente
    finden.');</script>");
    }
    return ds.Tables["indexServerDirectories"];
    }
    private void btnSearch_Click(object sender, System.EventArgs e)
    {
    DataTable dt = this.Search(((TextBox)this.FindControl(this.ID +
    "SearchString")).Text);
    Page.RegisterClientScriptBlock("x", "<script
    language='javascript'>alert("+dt.Rows.Count+");</script>");
    }
    protected System.Web.UI.WebControls.Button btn = new Button();
    protected System.Web.UI.WebControls.TextBox tb = new TextBox();
    protected override void OnInit(System.EventArgs e)
    {
    btn.Click += new System.EventHandler(this.btnSearch_Click);
    }
    override protected void CreateChildControls()
    {
    if (IndexserverCatalog.Trim() == "")
    {
    Page.RegisterClientScriptBlock("CatalogError", "<script
    language='javascript'>alert('Sie haben keinen Indexserver-Katalog angegeben,
    bitte ändern Sie Ihre Seite entsprechend.');</script>");
    return;
    }
    if (Columns.Trim() == "")
    {
    Page.RegisterClientScriptBlock("ColumnError", "<script
    language='javascript'>alert('Sie haben keine zu suchenden Spalten angegeben,
    bitte ändern Sie Ihre Seite entsprechend.');</script>");
    return;
    }
    tb.ID = this.ID + "SearchString";
    tb.Attributes.Add("runat", "server");
    this.Controls.Add(tb);
    btn.ID = this.ID + "btnSearch";
    btn.Attributes.Add("runat", "server");
    btn.Text = SearchButtonText;
    this.Controls.Add(btn);
    }
    }
    }
    ------------------------------------------------------------
    End of code from the WebCostumControl:
    --------------------------------------------------------------

    ------------------------------------------------------------
    Code from CodeBehind of Webpage:
    --------------------------------------------------------------
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    namespace Indexserver
    {
    public class WebForm1 : System.Web.UI.Page
    {
    protected artiso_lib.IndexserverSearch IndexserverSearch1;
    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    {
    IndexserverSearch1.Columns = "Directory";
    IndexserverSearch1.IndexserverCatalog = "Test";
    IndexserverSearch1.MaxSearchResults = 200;
    IndexserverSearch1.SearchButtonText = "Suchen...";
    }
    }
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    }
    }

    ------------------------------------------------------------
    End of Code from CodeBehind of WebPage:
    --------------------------------------------------------------
     
    Stefan, Jul 8, 2003
    #1
    1. Advertising

  2. Stefan

    Stefan Guest

    Thanks Teemu Keiski!

    It was really the INamingContainer interface..

    For those of you, don't understand what was wrong:

    old: public class IndexserverSearch : System.Web.UI.WebControls.WebControl
    new: public class IndexserverSearch : System.Web.UI.WebControls.WebControl,
    INamingContainer

    Regards, Stefan



    "Teemu Keiski" <> schrieb im Newsbeitrag
    news:...
    > As a first look it is composite control. You need to implement
    > INamingContainer interface as well.
    >
    > And when you create controls on server-side you don't have to add
    > runat="server" for them. runat="server" on aspx declarative syntax

    specific
    > and you create control basically dynamically in this case. There's never
    > need for aspx syntax in such case.
    >
    > --
    > Teemu Keiski
    > MCP, Designer/Developer
    > Mansoft tietotekniikka Oy
    > http://www.mansoft.fi
    >
    > ASP.NET Forums Moderator, www.asp.net
    > AspAlliance Columnist, www.aspalliance.com
    >
    > Email:
    >
    >
    > "Stefan" <> wrote in message
    > news:...
    > > Maybe someone sees my mistake? The Button-OnClick-Event never fires...
    > >
    > >
    > > --------------------------------------------------------
    > > The code from the WebCostumControl:
    > > --------------------------------------------------------
    > > using System;
    > > using System.Data;
    > > using System.Data.OleDb;
    > > using System.Web.UI;
    > > using System.Web.UI.WebControls;
    > > using System.ComponentModel;
    > > using artiso_lib;
    > > namespace artiso_lib
    > > {
    > > [DefaultProperty("IndexserverCatalog"),

    > ToolboxData("<{0}:IndexserverSearch
    > > runat=server></{0}:IndexserverSearch>")]
    > > public class IndexserverSearch : System.Web.UI.WebControls.WebControl
    > > {
    > > [Bindable(true), Category("Appearance"), DefaultValue(""),
    > > EditorBrowsable(EditorBrowsableState.Always), Description("Name des
    > > Indexserver-Katalogs.")]
    > > public string IndexserverCatalog
    > > {
    > > get {return ViewState["IndexserverCatalog"].ToString();}
    > > set {ViewState["IndexserverCatalog"] = value;}
    > > }
    > > [Bindable(true), Category("Appearance"), DefaultValue(""),
    > > Description("Maximale Anzahl der Suchergebnisse. Keine Angabe für alle
    > > Suchergebnisse.")]
    > > public int MaxSearchResults
    > > {
    > > get {return (int)ViewState["MaxSearchResults"];}
    > > set {ViewState["MaxSearchResults"] = value;}
    > > }
    > > [Bindable(true), Category("Appearance"), DefaultValue("Styles.css"),
    > > Description("StyleSheet Datei für die Formatierung des DataGrids.")]
    > > public string StyleSheetFile
    > > {
    > > get {return ViewState["StyleSheetFile"].ToString();}
    > > set {ViewState["StyleSheetFile"] = value;}
    > > }
    > > [Bindable(true), Category("Appearance"), DefaultValue("fdgsd"),
    > > Description("Spalten die vom Indexserver gelesen werden sollen.")]
    > > public string Columns
    > > {
    > > get {return ViewState["GetColumns"].ToString();}
    > > set {ViewState["GetColumns"] = value;}
    > > }
    > > [Bindable(true), Category("Appearance"), DefaultValue("Suche"),
    > > Description("Beschriftung des \"Suche\" Buttons.")]
    > > public string SearchButtonText
    > > {
    > > get {return ViewState["SearchButtonText"].ToString();}
    > > set {ViewState["SearchButtonText"] = value;}
    > > }
    > > public DataTable Search(string SearchString)
    > > {
    > > Cisso.CissoQueryClass cqc = new Cisso.CissoQueryClass();
    > > cqc.Catalog = IndexserverCatalog;
    > > cqc.MaxRecords = MaxSearchResults;
    > > cqc.CiFlags = "SHALLOW";
    > > cqc.Query = SearchString;
    > > cqc.Columns = Columns;
    > > cqc.SortBy = "Rank[d]";
    > > ADODB.Recordset rsIX =
    > > (ADODB.Recordset)cqc.CreateRecordset("nonsequential");
    > > OleDbDataAdapter daConvertToDataset = new OleDbDataAdapter();
    > > DataSet ds = new DataSet();
    > > daConvertToDataset.Fill(ds, rsIX, "indexServerDirectories");
    > > if (ds.Tables["indexServerDirectories"].Rows.Count == 0)
    > > {
    > > Page.RegisterClientScriptBlock("NoSearchResult", "<script
    > > language='javascript'>alert('Die Suche konnte keine entsprechenden

    > Dokumente
    > > finden.');</script>");
    > > }
    > > return ds.Tables["indexServerDirectories"];
    > > }
    > > private void btnSearch_Click(object sender, System.EventArgs e)
    > > {
    > > DataTable dt = this.Search(((TextBox)this.FindControl(this.ID +
    > > "SearchString")).Text);
    > > Page.RegisterClientScriptBlock("x", "<script
    > > language='javascript'>alert("+dt.Rows.Count+");</script>");
    > > }
    > > protected System.Web.UI.WebControls.Button btn = new Button();
    > > protected System.Web.UI.WebControls.TextBox tb = new TextBox();
    > > protected override void OnInit(System.EventArgs e)
    > > {
    > > btn.Click += new System.EventHandler(this.btnSearch_Click);
    > > }
    > > override protected void CreateChildControls()
    > > {
    > > if (IndexserverCatalog.Trim() == "")
    > > {
    > > Page.RegisterClientScriptBlock("CatalogError", "<script
    > > language='javascript'>alert('Sie haben keinen Indexserver-Katalog

    > angegeben,
    > > bitte ändern Sie Ihre Seite entsprechend.');</script>");
    > > return;
    > > }
    > > if (Columns.Trim() == "")
    > > {
    > > Page.RegisterClientScriptBlock("ColumnError", "<script
    > > language='javascript'>alert('Sie haben keine zu suchenden Spalten

    > angegeben,
    > > bitte ändern Sie Ihre Seite entsprechend.');</script>");
    > > return;
    > > }
    > > tb.ID = this.ID + "SearchString";
    > > tb.Attributes.Add("runat", "server");
    > > this.Controls.Add(tb);
    > > btn.ID = this.ID + "btnSearch";
    > > btn.Attributes.Add("runat", "server");
    > > btn.Text = SearchButtonText;
    > > this.Controls.Add(btn);
    > > }
    > > }
    > > }
    > > ------------------------------------------------------------
    > > End of code from the WebCostumControl:
    > > --------------------------------------------------------------
    > >
    > > ------------------------------------------------------------
    > > Code from CodeBehind of Webpage:
    > > --------------------------------------------------------------
    > > using System;
    > > using System.Collections;
    > > using System.ComponentModel;
    > > using System.Data;
    > > using System.Drawing;
    > > using System.Web;
    > > using System.Web.SessionState;
    > > using System.Web.UI;
    > > using System.Web.UI.WebControls;
    > > using System.Web.UI.HtmlControls;
    > > namespace Indexserver
    > > {
    > > public class WebForm1 : System.Web.UI.Page
    > > {
    > > protected artiso_lib.IndexserverSearch IndexserverSearch1;
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > if (!IsPostBack)
    > > {
    > > IndexserverSearch1.Columns = "Directory";
    > > IndexserverSearch1.IndexserverCatalog = "Test";
    > > IndexserverSearch1.MaxSearchResults = 200;
    > > IndexserverSearch1.SearchButtonText = "Suchen...";
    > > }
    > > }
    > > override protected void OnInit(EventArgs e)
    > > {
    > > InitializeComponent();
    > > base.OnInit(e);
    > > }
    > > private void InitializeComponent()
    > > {
    > > this.Load += new System.EventHandler(this.Page_Load);
    > > }
    > > }
    > > }
    > >
    > > ------------------------------------------------------------
    > > End of Code from CodeBehind of WebPage:
    > > --------------------------------------------------------------
    > >
    > >

    >
    >
     
    Stefan, Jul 9, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad
    Replies:
    4
    Views:
    8,376
  2. murl
    Replies:
    6
    Views:
    4,503
  3. Ryan
    Replies:
    4
    Views:
    9,037
    Chris Botha
    Nov 8, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  5. mbs57

    ASP.NET button control OnClick event doesn't fire

    mbs57, Jun 18, 2009, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    1,474
Loading...

Share This Page