[ASP.NET][C#] récupérer un événement click d'un custom web control

Résolu
outcast_fr Messages postés 11 Date d'inscription jeudi 10 novembre 2005 Statut Membre Dernière intervention 11 avril 2006 - 7 mars 2006 à 09:16
outcast_fr Messages postés 11 Date d'inscription jeudi 10 novembre 2005 Statut Membre Dernière intervention 11 avril 2006 - 7 mars 2006 à 12:30
Bonjour,
j'ai créé un petit custom web control (pas de user control) composé d'un label et d'un bouton.
Mon but c'est de récupérer l'événement associé au bouton.
J'ai donc suivi un tutoriel, mais sans réel succès.

Voici le code :


Code:,
----

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

namespace EAC_WebControls
{
/// <summary>
/// Summary description for EAC_WebControls.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:HeadWebPage runat=server></{0}:HeadWebPage>")]
public class HeadWebPage : System.Web.UI.WebControls.WebControl
{
private string text;
private Button myButton = new Button();

[Bindable(true),
Category("Attributs WebControl"),
Description("Texte de définition."),
DefaultValue("")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

//-----------------------------------------------------------------------
//Déclaration des délégués
public delegate void ValidButtonClickHandler(object sender, EventArgs e);

//Déclaration des événements qui utilisent ces délégués
[Category("Action"),
Browsable(true),
Description("Evénement associé au bouton de validation.")]
public event ValidButtonClickHandler BoutonValidClick;

public virtual void OnValidButtonClick(EventArgs e)
{
if (BoutonValidClick != null)
{
BoutonValidClick(this, e);
}
}

public void ButtonValid_Click(object sender, EventArgs e)
{
OnValidButtonClick(e);
}
//-----------------------------------------------------------------------

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// The HTML writer to write out to

protected override void Render(HtmlTextWriter output)
{
Text += "Champ Texte : ";
output.Write(Text);

myButton.Text = "WebControl";
myButton.RenderControl(output);
}
}
}

Après dans une page ASP.NET sur l'événement click du bouton je mets ceci :

Code:,
----

Response.Write("<script>alert("Bouton cliqué !");</script>");

Mais ça ne marche pas et je ne vois vraiment pas pourquoi.
Donc si vous pouvez m'aider ... ce serait cool
Merci.

1 réponse

outcast_fr Messages postés 11 Date d'inscription jeudi 10 novembre 2005 Statut Membre Dernière intervention 11 avril 2006
7 mars 2006 à 12:30
Bonjour,
Bon finalement j'ai trouvé la solution à mon problème grâce à ce topic (voir l'avant dernière réponse) :
http://forums.asp.net/608217/ShowPost.aspx

Bon je n'ai pas compris pourquoi ça marchait, mais l'essentiel, c'est que ça fonctionne bien.

Il faut donc implémenter l'interace System.Web.UI.INamingContainer .
Le code fonctionnel est donc celui-ci :


Code:,
----

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

namespace EAC_WebControls
{
/// <summary>
/// Summary description for EAC_WebControls.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:TestBouton runat=server></{0}:TestBouton>")]
public class TestBouton : System.Web.UI.WebControls.WebControl, System.Web.UI.INamingContainer
{
private string _Text = "Champ Texte : ";
private Button myButton = new Button();

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

protected override void CreateChildControls()
{
Controls.Clear();
//myButton = new Button();
//myButton.ID = "blah";
//myButton.Text = TexteBouton;
myButton.Click += new EventHandler(ButtonValid_Click);
this.Controls.Add(myButton);
}

[Bindable(true),
Category("Attributs WebControl"),
Description("Texte du Label."),
DefaultValue("")]
public string TexteLabel
{
get { return _Text; }
set { _Text = value; }
}

//-----------------------------------------------------------------------
//Déclaration des délégués
public delegate void ValidButtonClickHandler(object sender, EventArgs e);

//Déclaration des événements qui utilisent ces délégués
[Category("Action"),
Browsable(true),
Description("Evénement associé au bouton de validation.")]
public event EventHandler Click;

public void OnClick(EventArgs e)
{
EventHandler click = Click;
if (click != null)
{
click(this, e);
}
}

public void ButtonValid_Click(object sender, EventArgs e)
{
this.OnClick(e);
}
//-----------------------------------------------------------------------

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// The HTML writer to write out to

protected override void Render(HtmlTextWriter output)
{
output.Write(TexteLabel);

myButton.Text = "Mon bouton";
myButton.RenderControl(output);
}
}
}

En espérant que cela pourra aider quelqu'un un jour ...
3
Rejoignez-nous