2

I want to build custom control consist of html code which shows table, and in the footer of this table I need to add three buttons. What I did is inherit from WebControl class:

public class MyCustomControl : WebControl {  
    protected override void RenderContents(HtmlTextWriter output)
    {  
    // using output.Write I write the table in html code  
    // and I write the three buttons using <input> tag in html not <asp:button> tag
    }  
}

What I want here is to add three events one to each button I wrote, and those events would be used in the user interface and fired when the proper button clicked:

<asp:MyCustomControl runat="server" id="myCtrl" onButton1Click="Button1_Click" onButton2Click="Button2_Click" />  

How can I do this ?

Thanx

**UPDATE1:
The render code in my custom control would be like this:

protected override void RenderContents(HtmlTextWriter output)
    {
    output.Write("<table> ......");
    output.Write("<input id='button1' type='button'>");
    output.Write("<input id='button2' type='button'>");
    output.Write("<input id='button3' type='button'>");
    output.Write(".........</table>");
    }

So how would I make 'button1' fire the event on server side?

**UPDATE2:
This is what the code looks like:

public class MyCustomControl : WebControl
{

 public Button Button1 = new Button {Text = "Button1"};
 public Button Button2 = new Button {Text = "Button2"};

 public event EventHandler Button1_Click;
 public event EventHandler Button2_Click;

 protected override void OnPreRender(EventArgs e)
 {
   Button1.Click += Button1Click;
   Button2.Click += Button2Click;
   base.OnPreRender(e);
 }

 protected override void RenderContents(HtmlTextWriter output)
 {
  using (var plh = new PlaceHolder())
   {
   var htmlCode = new StringBuilder();
   htmlCode.Append("....html code for table...");

   var container = new HtmlGenericControl { InnerHtml = htmlCode.ToString() };

   plh.Controls.Add(container);
   plh.Controls.Add(Button1);
   plh.Controls.Add(Button2);
   plh.RenderControl(output);

   htmlCode.Append("..../html code for table...");
   }
 }

 private void Button1Click(object sender, EventArgs e)
 {
  if (Button1_Click != null)
      Button1_Click(this, e);
 }

 private void Button2Click(object sender, EventArgs e)
 {
  if (Button2_Click != null)
      Button2_Click(this, e);
 }

And in the page.aspx:

<cc1:MyCustomControl ID="myCtrl" runat="server" onbutton1_click="MyCustomControl_Button1_Click" />

But even with this the button1's click method 'MyCustomControl_Button1_Click' not called.

Dabbas
  • 3,112
  • 7
  • 42
  • 75

3 Answers3

0

declare your new events like this:

public event EventHandler OnButton1Click;
public event EventHandler OnButton2Click;
public event EventHandler OnButton3Click;

this in the click method on each button do something like this:

public void Button1_Click(object sender, EventArgs e)
{
    if (OnButton1Click != null)
        OnButton1Click(this, null);
}
Z .
  • 12,657
  • 1
  • 31
  • 56
  • But how would the html input button fire the server side click method like any regular asp.net button does? like I said I'm writing the button html code manually, I didn't use ASP.NET button inside my custom control (because I don't know how to do it with three buttons and html table with it). – Dabbas Oct 11 '12 at 14:19
  • use asp:Buttons instead of "regular" buttons – Z . Oct 11 '12 at 14:28
  • How?..I'm writing the output result of the control using 'HtmlTextWriter' variable passed as parameter of the 'RenderContents' override method..so how can I add three asp.net buttons? – Dabbas Oct 11 '12 at 14:39
  • @Dabbas, see this question: http://stackoverflow.com/questions/188007/creating-an-aspbutton-programatically – Yehuda Shapira Oct 11 '12 at 14:47
  • @Dabbas Why not use asp:Button? It will render as regular HTML on the client's machine, and you can assign to it server-side methods! – Yehuda Shapira Oct 11 '12 at 19:13
0

Within MyCustomControl class, define events that take (object sender, EventArgs e) and return void.

Then add (still within MyCustomControl) the event handlers for the click events of the internal buttons.
Inside these methods, do CustomClickEvent.Invoke().

Then, in the page containing your custom control, do myControl.CustomClickEvent += new CustomClickEvent(name_of_method_within_page);

Yehuda Shapira
  • 8,460
  • 5
  • 44
  • 66
0

To render an asp control from your Custom Control do that from Render base method:

protected override void Render(HtmlTextWriter writer)

Prepare before your controls with this method

protected override void CreateChildControls()
ArtCava
  • 21
  • 7