473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DataList DataKey button_click assignment Problem (DataListCommandEventArgs)

I'm don't know how I would get around this.

When working with a datalist control, there a specific built in commands
(OnEditCommand, OnCancelCommand, etc). These commands, when the datalist
control is made, refer to a subprocedure (eg:
OnUpdateCommand="MyUpdateProcedure").

When referring to a specific record that's written to the page by the
datalist object, it refers to the datakey (eg: <asp:datalist
datakeyfield="ID" ...>). Then whatever button I place into the
ItemTemplate, must refer to one of the OnWhateverCommand (edit, cancel,
update or delete), like so:
<asp:button ID="btnOne" CommandName="Update" runat="server" text="Update" />

Then, on the codepage, I would have:

Sub DL1_Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

Dim KeyField as String
KeyField = DL1.DataKeys(e.Item.ItemIndex)

'more code here to do the update
End Sub

Because of the DataListCOmmandEventArgs, I can create a string variable
and apply the keyfield of the record being displayed by the datalist
control. I would then used this in a sql statement, like so:

SQL = "UPDATE mytable SET name = 'John Smith' WHERE ID = " & KeyField

So, this all works fine, but I've run into a problem.

I wanted to put a button within the <ItemTemplateof the datalist that
updates a record but, I don't know how. If I double click the button, I
get the click procedure of the button but I have no way to add a
reference to the DataKeyField of the datalist control.

I can't just do this:

Sub btnOne_Click(ByVal sender As Object, ByVal e As System.EventArgs,
ByVal r as DataListCommandEventArgs)

Dim KeyField as String
KeyField = DL1.DataKeys(r.Item.ItemIndex)

End Sub

I get a signature error when I do that.

So, how do I get around this?? Is there a way??

Thanks,
Jim
Sep 18 '06 #1
1 3891
Jim in Arizona wrote:
I'm don't know how I would get around this.

When working with a datalist control, there a specific built in commands
(OnEditCommand, OnCancelCommand, etc). These commands, when the datalist
control is made, refer to a subprocedure (eg:
OnUpdateCommand="MyUpdateProcedure").

When referring to a specific record that's written to the page by the
datalist object, it refers to the datakey (eg: <asp:datalist
datakeyfield="ID" ...>). Then whatever button I place into the
ItemTemplate, must refer to one of the OnWhateverCommand (edit, cancel,
update or delete), like so:
<asp:button ID="btnOne" CommandName="Update" runat="server"
text="Update" />

Then, on the codepage, I would have:

Sub DL1_Update(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

Dim KeyField as String
KeyField = DL1.DataKeys(e.Item.ItemIndex)

'more code here to do the update
End Sub

Because of the DataListCOmmandEventArgs, I can create a string variable
and apply the keyfield of the record being displayed by the datalist
control. I would then used this in a sql statement, like so:

SQL = "UPDATE mytable SET name = 'John Smith' WHERE ID = " & KeyField

So, this all works fine, but I've run into a problem.

I wanted to put a button within the <ItemTemplateof the datalist that
updates a record but, I don't know how. If I double click the button, I
get the click procedure of the button but I have no way to add a
reference to the DataKeyField of the datalist control.

I can't just do this:

Sub btnOne_Click(ByVal sender As Object, ByVal e As System.EventArgs,
ByVal r as DataListCommandEventArgs)

Dim KeyField as String
KeyField = DL1.DataKeys(r.Item.ItemIndex)

End Sub

I get a signature error when I do that.

So, how do I get around this?? Is there a way??

Thanks,
Jim
I see that I can use the OnItemCommand="myprocedure" to run more more
code, but if I do that, that only allows for one button to use it.

<asp:DataList OnItemCommand="TestCommand" ... />
<asp:Button CommandName="Item" ... />

Sub TestCommand(ByVal sender As Object, ByVal e As DataListCommandEventArgs)

Dim tkey As String
tkey = dlShowRequests.DataKeys(e.Item.ItemIndex)
Response.Write(tkey)

End Sub

I'm assuming I could use the OnEditCommand, OnCancelCommand,
OnUpdateCommand, OnDeleteCommand and now OnItemCommand to code up to
five button to run any code I want and refer back to the datakey of the
record displayed by the datalist control, but what if I want more and am
I really limited to just these 'command's?

Sep 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Neil | last post by:
I can't get the event handling to work on Button or ImageButton inside the header of a DataList. If I change Button to LinkButton it works fine. Has anyone had this problem? I'm using c# code...
2
by: Eduardo Rosa | last post by:
Hello people, I'm using DataList to show some info from db (sqlserver), my question is about how make a button that call a function to do something using info from a line of the DataList. I...
1
by: Neil Jarman | last post by:
Hi, I'm new to this today, and I've got some test code (see below.) The data loads fine. I can't understand why any of the events fire. Once the page loads, clicking on thew button from the...
3
by: asadikhan | last post by:
Hi, I am trying to use a datalist to edit data in a MySql database as follows. For now I just want to be able to get the changes to be made on the dataset. Here is my code: using System;...
1
by: Craig | last post by:
This is killing me, I've been trying to figure this out for 2 days. When I click on the linkbutton, it executes the GetData(int) method to set the DataSource. The FAILURE is when it tries to...
0
by: yousuf | last post by:
I have a dataList that pulls from a database table - Im trying to do the normal Edit, Update, Delete, Cancel - on it. when I put the following inside a user control - .ascx file it dosn't work,...
3
by: Mirek Endys | last post by:
I have DataList as part of DataList item. DataList in DataList. The parent DataList working well including Edit command, that shows Edit template and correctly bind the data into edit template...
0
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I have am dynamically loading a web user control based on the click of a tab strip I load the default control for the first tab in the page load event after checking page is not postback. After...
3
by: Crazy Cat | last post by:
Hi all, I am developing an asp.net 2.0 application in Visual Studio 2005. On my page I have a simple datalist that is bound programmatically to a collection of simple objects. On this page I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.