By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

ASP Button in a Datalist

P: n/a
Hello,

I have a datalist that contains an asp button. I have the need to pass a
value that is bound to my datalist along the button when the on_click event
is fired. My datalist creates a button for each row in my datasource, so
each button needs to pass a unique value. My questions are:

1. How do I pass the value with the button without making it visible on the
button or form.
2. Is there a way to grab this value in the button's event parameters, the
sender as system.object or the e as system.eventArgs

This button was previosly an anchor tag that passed the value through the
HREF querystring, but I did not like how it lost all the viewstate when I
redirected back.

Any help is appreciated,
Thanks
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Chris,

The only controls that will have an event are PAGE level controls.
Once you place a button inside a Datalist, it is now a CHILD of the datalist
and will need to use the Datalist's events instead.

For the button, you will need to assign, through databinding, these
attributes

CommandArguement
CommandName

BUTTON EXAMPLE
<asp:button id="Button1" runat="server" text="Push me" commandarguement='<%#
Container.DataItem("primaryKey") %>' commandname="Action" />
This sets the CommandArguement equal to the primary key of the record.

Now, for the event that will occur when button is pushed - This is the
Datalist's ItemCommand Event

Private Sub DataList1_ItemCommand(source as object, e as datalisteventargs)
Handles Datalist1.ItemCommand
'Assign a variable to hold CommandArgument

Dim primaryKey as Integer
primaryKey = CInt(e.CommandArguement)

'use this primaryKey variable in logic below
'Also, if there are more than one button (edit, delete, copy, etc)
'You put that "Action" in the CommandName attribute and use like so...

If e.CommandName = "Delete" Then
OleDbCommand1.CommandText = "DELETE DISTINCT FROM table WHERE
[primaryKey]="& primaryKey &";"
End If

End sub

Hope this helps,
Severin

"Chris Fink" <ch***@chrisfink.com> wrote in message
news:e%***************@TK2MSFTNGP11.phx.gbl...
Hello,

I have a datalist that contains an asp button. I have the need to pass a
value that is bound to my datalist along the button when the on_click event is fired. My datalist creates a button for each row in my datasource, so
each button needs to pass a unique value. My questions are:

1. How do I pass the value with the button without making it visible on the button or form.
2. Is there a way to grab this value in the button's event parameters, the
sender as system.object or the e as system.eventArgs

This button was previosly an anchor tag that passed the value through the
HREF querystring, but I did not like how it lost all the viewstate when I
redirected back.

Any help is appreciated,
Thanks

Nov 17 '05 #2

P: n/a
Hi.

There is a way:

First, change the buttons in your list to be control
buttons so they are using the OnCommand event instead of
OnClick. That makes you able to add a command parameter to
the buttons.

This code is from an application I'm making now. The
list's DataSource is a dataset, and when the button is
pushed another page opens. I use the session object for
storing different kinds of data.

Hope this helps you!

Ingeborg
private void dataList_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
DataRowView drv = (DataRowView) e.Item.DataItem;

Button anyButton = (Button)e.Item.FindControl
("anyButton");
anyButton.Command += new
System.Web.UI.WebControls.CommandEventHandler
(anyButton_Command);
anyButton.CommandArgument = drv.Row["id"].ToString
();
}

protected void anyButton_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
Session["Id"] = int.Parse(e.CommandArgument.ToString
());
Response.Redirect("newpage.aspx");
}

-----Original Message-----
Hello,

I have a datalist that contains an asp button. I have the need to pass avalue that is bound to my datalist along the button when the on_click eventis fired. My datalist creates a button for each row in my datasource, soeach button needs to pass a unique value. My questions are:
1. How do I pass the value with the button without making it visible on thebutton or form.
2. Is there a way to grab this value in the button's event parameters, thesender as system.object or the e as system.eventArgs

This button was previosly an anchor tag that passed the value through theHREF querystring, but I did not like how it lost all the viewstate when Iredirected back.

Any help is appreciated,
Thanks
.

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.