DataList DataKey button_click assignment Problem (DataListCommandEventArgs)

J

Jim in Arizona

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 <ItemTemplate> of 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
 
J

Jim in Arizona

Jim said:
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 <ItemTemplate> of 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?
 

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

No members online now.

Forum statistics

Threads
473,708
Messages
2,569,347
Members
44,655
Latest member
VinayKumarNevatiia

Latest Threads

Top