From: E L on
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[i].ItemArray[5].ToString();
intUserGroup = dsResult.Tables[0].Rows[i].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!

*** Sent via Developersdex http://www.developersdex.com ***
From: Simon Whale on
have a look at buttoncolumn

"E L" <smakawhat(a)yahoo.com> wrote in message
news:eOzAfRVuKHA.4220(a)TK2MSFTNGP05.phx.gbl...
>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[i].ItemArray[5].ToString();
> intUserGroup = dsResult.Tables[0].Rows[i].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!
>
> *** Sent via Developersdex http://www.developersdex.com ***


From: Norman Yuan on
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[i].ItemArray[5].ToString();
intUserGroup = dsResult.Tables[0].Rows[i].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" <smakawhat(a)yahoo.com> wrote in message
news:eOzAfRVuKHA.4220(a)TK2MSFTNGP05.phx.gbl...
>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[i].ItemArray[5].ToString();
> intUserGroup = dsResult.Tables[0].Rows[i].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!
>
> *** Sent via Developersdex http://www.developersdex.com ***