ASP Button in a Datalist

  • Thread starter Thread starter Chris Fink
  • Start date Start date
C

Chris Fink

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
 
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
 
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");
}
 
Back
Top