How can I do this?

B

Bob Cummings

Greetings

I have figured out how to create my own WebCustomControl and use it on a
web form. It is a very simple control, a couple of labels and a button.
I have been able to create and raise a custom event handler for the
button. That is the web form as a private method in the "code behind"
page and I can invoke that method when clicking on the button in my
WebCustomControl.

I can read my data from the database and put the information in the
correct places. And all is good so far.

However the problem I am running across is when I create the custom
controls on the fly so to speak how can I capture the button click event
from one of them and how will I know which one it is?


Here is how I am adding them to the page, I needed to add them to a
panel for otherwise they went all over and panel contains them nicely.

private void buildPage()
{
try
{
OleDbDataReader dr ;
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText="SELECT Title, Author, Media, Year, Available FROM
resource";
if (myCon.State == ConnectionState.Closed) myCon.Open();
cmd.Connection = myCon;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while(dr.Read())
{
myDisplay = new CustomControl.Display();
myDisplay.Title = dr.GetString(0);
myDisplay.Author = dr.GetString(1);
myDisplay.Media = dr.GetString(2);
myDisplay.Year = dr.GetString(3);
myDisplay.Available = dr.GetBoolean(4).ToString();
this.myGrid1.Controls.Add(myDisplay);
}
}
catch (System.Exception e)
{
Response.Write (e.Message);
myCon.Close();
}
}
}

And this event handler worked fine when I only read in one column but
now there may be 100 of them and I am lost how to handle that situation.

public void myDisplay_Click (object sender, System.EventArgs e)
{
CustomControl.Display my = (CustomControl.Display)sender;
Response.Write(my.UniqueID.ToString());
}

thanks

Bob
 
N

Natty Gur

Hi,

I don't see where you bind your custom control event to event handler.
Any way, you can add unique ID to every instance of custom control and
register your event handler for all of created custom control. you can
determinate which control raise event by checking event sender argument
ID.
while(dr.Read())
{
myDisplay = new CustomControl.Display();
myDisplay.Title = dr.GetString(0);
myDisplay.Author = dr.GetString(1);
myDisplay.Media = dr.GetString(2);
myDisplay.Year = dr.GetString(3);
myDisplay.Available = dr.GetBoolean(4).ToString();
myDisplay.ID = dr.GetString(4); // 4 holds unique ID
myDisplay.UniqueID = dr.GetString(4);
myDisplay.Click += new EventHandler(myDisplay_Click );
this.myGrid1.Controls.Add(myDisplay);
}

public void myDisplay_Click (object sender, System.EventArgs e)
{
CustomControl.Display my = (CustomControl.Display)sender;
Response.Write(my.UniqueID.ToString());
}


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top