Need to add Button to DataList

J

Jack

Hello,

I need to add extra buttons to a datalist, but I don't know to add them to
the control. I need to use the onCommand Functions, like OnUpdate, OnEdit,
OnUpdate, OnCanel, Etc. I would like to do this VB.Net is that is can be
done. Any help would be great, I have been trying to do this for a while
and need help.

Thanks in advance for any help,

Jack
 
K

Ken Cox - Microsoft MVP

Hi Jack,

I'm not really clear on what your problem is or what's not working for you.
Perhaps you could elaborate? It the meantime, the code below (ASP.NET 2.0)
inserts buttons into a datalist template.

Let us know?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 5
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function

Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
DataList1.DataSource = CreateDataSource()
DataList1.DataBind()
End If
End Sub

Sub btncmd(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Label1.Text = e.CommandName
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="DataList1" runat="server">
<itemtemplate>
<asp:button id="Button1" runat="server" commandname="Add"
oncommand="btncmd" text="Button" />
<asp:button id="Button2" runat="server" commandname="Edit"
oncommand="btncmd" text="Button" />
<asp:button id="Button3" runat="server" commandname="Delete"
oncommand="btncmd" text="Button" /><br />
<%#DataBinder.Eval(Container, "DataItem.StringValue")%>
</itemtemplate>
</asp:datalist>
<asp:label id="Label1" runat="server" text=""></asp:label></div>
</form>
</body>
</html>
 
N

Nathan Sokalski

Well, add a Button Control to the template just like you would any other
Control. Then set the CommandName property of the Button. Depending on what
CommandName you chose, one of the following events will be triggered when
the button is clicked, depending on the value of CommandName:

"edit" EditCommand
"update" UpdateCommand
"cancel" CancelCommand
"delete" DeleteCommand
"item" ItemCommand

You can then determine which Item's button was clicked by using properties
such as e.Item.ItemIndex. The Button also contains an optional
CommandArgument property which is passed as well (if you use this property
you will probably assign it a value in the ItemDataBound event). The e
parameter, which is passed to the eventhandler, also contains several other
properties. For more info, see the documentation or for a specific question
feel free to ask. (Don't worry, it took me a while to figure this out also)
Good Luck!
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top