EventHandler in Asp.net with C#

April 15, 2009

Textbox Event:

protected override void OnInit(EventArgs e) {
    TextBox txtName = new TextBox();
    txtName.ID = "txtName";
    txtName.Text = "Enter your name";
    this.form1.Controls.Add(txtName);
 
    base.OnInit(e);
}

Dropdown List Event

DropDownList DropDown1 = new DropDownList();

this.form1.Controls.Add(DropDown1);

Example:

DropDownList ddlStatus = new DropDownList();

ddlStatus.Items.Insert(0, (new ListItem(“Pending”, “1″)));

ddlStatus.Items.Insert(1, (new ListItem(“Aproved”, “2″)));

ddlStatus.Items.Insert(2, (new ListItem(“Deleted”, “3″)));

ddlStatus.ID = “ddlStatus”;

Page.Form.Controls.Add(ddlStatus); //Remember

ddlStatus.SelectedIndexChanged += this.ddlStatus_SelectedIndexChanged1;

ddlStatus.AutoPostBack = true;

#region “ddlStatus_SelectedIndexChanged1″

protected void ddlStatus_SelectedIndexChanged1(object sender, System.EventArgs e)

{

Response.Redirect(“http://wwww.yahoo.com”);

}

#endregion

CheckBox Event :

#region “Page_Load”

protected void Page_Load(object sender, EventArgs e)

{

CheckBox CheckBox1 = new CheckBox();

this.form1.Controls.Add(CheckBox1);

((CheckBox)CheckBox1).CheckedChanged += new System.EventHandler(this.ClickMe);

CheckBox1.AutoPostBack = true; //Remember

}

#endregion

public void ClickMe(object sender, EventArgs e)

{

Response.Write(“Checkbox is checked”);

}

Button Event:

Example: protected void Page_Load(object sender, EventArgs e)

{

Button Button1 = new Button();

Button1.Text = “Submit”;

Panel3.Controls.Add(Button1);

Button1.Click += new System.EventHandler(this.JustAnotherEventHandler);

}

void JustAnotherEventHandler(object sender, System.EventArgs e)

{

Label1.Text = “Clicked”;

//ListBox1.Items.Add(“Event 1 says hello”);

}

Example:

System.Web.UI.HtmlControls.HtmlGenericControl div1= new
HtmlGenericControl( “div” );
div1.ID = “divNewTextBox”;
TextBox txt1 = new TextBox();
div1.Controls.Add( txt1 );

Linkbutton Events:

Example: You add an event delegate to a linkbutton and use the called method to do the work.

In C# (you can do the translation yourself) :

LinkButton lb = new LinkButton();
lb.text = “foo”;
lb.CommandArgument = “foo”;
lb.Click += new EventHandler(LbClick);
c.Controls.Add(lb);

then :

public void LbClick(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
string bar = lb.CommandArgment; //should == foo

}

Example2:

private LinkButton lb = new LinkButton();

protected void Page_Load(object sender, EventArgs e)

{

LinkButton lbTemp = CreateLinkButton(“lnk1″, “my 1st dynam control”);

lbTemp.Click += linkbutton_click;

//pnlHoldsStuff.Controls.Add(lbTemp);

this.Form1.Controls.Add(lb);

}

[System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] // ERROR: Optional parameters aren’t supported in C#

//string vsCssClsass

private LinkButton CreateLinkButton(string vsID, string vsText)

{

lb.ID = vsID;

lb.Text = vsText;

//lb.CssClass = vsCssClsass;

return lb;

}

private void linkbutton_click(object sender, System.EventArgs e)

{

Response.Write(“I got clicked”);

LinkButton lb = CreateLinkButton(“lnk2″, “my 2nd dynam control”);

lb.Click += linkbutton2_click;

//pnlHoldsStuff.Controls.Add(lb);

this.Form1.Controls.Add(lb);

}

private void linkbutton2_click(object sender, System.EventArgs e)

{

Response.Write(“Second Link button clicked”);

}


Hello world!

April 15, 2009

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!