Event handling with dynamic buttons

E

E L

I am not sure how to do this. I have part of it working and I think I
understand why it isn't working.

I am dynamically placing some data into and ASP:Table from a DataSource.

I am looping through the datasource by going through the rows no problem
to display the data in cells added by rows to the asp:table. What I am
also doing though is adding a button for each row that is to run an
Access insert query that adds a record in a log table.

I have used the following code to get this to work which it does:

... this is in a loop of the DataSet

for (int i = 0; i < dsResult.Tables[0].Rows.Count - 1; i++)
{
Button btn = new Button();
btn.Text = "whatever";
intTID = dsResult.Tables[0].Rows.ItemArray[5].ToString();
intUserGroup = dsResult.Tables[0].Rows.ItemArray[6].ToString();
btn.Click += new EventHandler(this.b_Click);
tCell1.Controls.Add(btn);
etc.. etc..
}


... [assume connection open and all that stuff]
private void b_Click(object sender, EventArgs e)
{
cmd.Parameters.AddWithValue("pmSignOff", intSignOffID);
cmd.Parameters.AddWithValue("pmUserID", intUserID);
cmd.Parameters.AddWithValue("pmTableNameID", intTID);
cmd.Parameters.AddWithValue("pmGroupID", intUserGroup );

cmd.ExecuteNonQuery();
}

Here is the problem I am having. The button does display for each row,
and the query does execute and run when I press the button. The values
being passed in intTID and intUserGroup (which are local to the class
and sort of global), are suppose to be different for each row though,
and are to insert a unique table ID, and a different group depending on
what is in the dataset. Problem is it is only assigning the last
tableID from the result set into the button and will always pass that
same ID no matter what button is pressed. This is the same with the
intUserGroup.

What do I have to do differently to make sure that when the button is
created dynamically it will pass the unique values for each row?

Thanks!
 
N

Norman Yuan

In the "for..." loop, after getting the values of intTID and intUserGroup,
you need to somehow link the values to the button before the loop goes to
next row.

The simplest way is the use Button's CommandArgument property to store the 2
values:

for (int i=0.............)
{
Button btn = new Button();
btn.Text = "whatever";
intTID = dsResult.Tables[0].Rows.ItemArray[5].ToString();
intUserGroup = dsResult.Tables[0].Rows.ItemArray[6].ToString();

btn.CommandArgument=intTID +"," + intUserGroup

btn.Click += new EventHandler(this.b_Click);
tCell1.Controls.Add(btn);
//etc.. etc..
}

Then, in the Click event handler, you retrieve the values from the button's
CommandArgument:

private void b_Click(object sender, EventArgs e)
{
Button btn=sender as Button;
string[] args=btn.CommandArgument.Split(new char[]{','});
int tid=Int32.Parse(args[0];
int group=Int32.Parse(args[1];

cmd.Parameters.AddWithValue("pmSignOff", intSignOffID);
cmd.Parameters.AddWithValue("pmUserID", intUserID);
cmd.Parameters.AddWithValue("pmTableNameID", tid);
cmd.Parameters.AddWithValue("pmGroupID", group );

cmd.ExecuteNonQuery();
}


E L said:
I am not sure how to do this. I have part of it working and I think I
understand why it isn't working.

I am dynamically placing some data into and ASP:Table from a DataSource.

I am looping through the datasource by going through the rows no problem
to display the data in cells added by rows to the asp:table. What I am
also doing though is adding a button for each row that is to run an
Access insert query that adds a record in a log table.

I have used the following code to get this to work which it does:

.. this is in a loop of the DataSet

for (int i = 0; i < dsResult.Tables[0].Rows.Count - 1; i++)
{
Button btn = new Button();
btn.Text = "whatever";
intTID = dsResult.Tables[0].Rows.ItemArray[5].ToString();
intUserGroup = dsResult.Tables[0].Rows.ItemArray[6].ToString();
btn.Click += new EventHandler(this.b_Click);
tCell1.Controls.Add(btn);
etc.. etc..
}


.. [assume connection open and all that stuff]
private void b_Click(object sender, EventArgs e)
{
cmd.Parameters.AddWithValue("pmSignOff", intSignOffID);
cmd.Parameters.AddWithValue("pmUserID", intUserID);
cmd.Parameters.AddWithValue("pmTableNameID", intTID);
cmd.Parameters.AddWithValue("pmGroupID", intUserGroup );

cmd.ExecuteNonQuery();
}

Here is the problem I am having. The button does display for each row,
and the query does execute and run when I press the button. The values
being passed in intTID and intUserGroup (which are local to the class
and sort of global), are suppose to be different for each row though,
and are to insert a unique table ID, and a different group depending on
what is in the dataset. Problem is it is only assigning the last
tableID from the result set into the button and will always pass that
same ID no matter what button is pressed. This is the same with the
intUserGroup.

What do I have to do differently to make sure that when the button is
created dynamically it will pass the unique values for each row?

Thanks!
 

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