ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Android Programming
Design Pattern By PHP
Spring+Strut+Hibernate
C# Tips & Technique
C# Design Pattern
Linux Quick Tips
C# using Linq น่าใช้จริงๆ
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     Create Button at runtime   (2009-09-03)

Taking System.Web.UI.WebControls.Button as a testing Control

Creating Button control dynamically and adding on the Form following way and assign Click event to it,
Code in the Page_Load method will be called every time the Form Post
btn_Click method will be executed whenever the Dynamically created Button clicked.

protected void Page_Load(object sender, EventArgs e)
{
int x = 0;
int y =100;
for (int i = 1; i < 10; i++)
{

Button btn = new Button();
btn.Name = "btn" + i.ToString();
btn.Text = "Button " + i.ToString();
btn.Click += new EventHandler(btn_Click);
btn.Location = new Point(x, y);
y += 30;
this.Controls.Add(btn);
}
}

void btn_Click(object sender, EventArgs e)
{
int btnIndex = Convert.ToInt32(((Button)sender).ID.Substring(3, 1));
Response.Write(btnIndex.ToString());
}

 


Comment

Mr.ZengCode  (03 กันยายน 2552)   
IP : 210.4.138.41

หรือจะย่อให้เหลือ

  void btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show(((Button)sender).Name);
            ((Button)sender).Visible = false;
        }

ก็ได้นะครับ ไม่รู้จะเขียนทำไมให้ยืดยาวเนอะ เราก้ต้องรู้จักมา apply กันด้วยเนอะๆ


Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.056600 seconds to load.