data List selectedValue

J

James Page

Hi all

Have a bit of a problem..

I have a data list and in the item template I have an image button.
The Data list is data bound to sql data:
id
picId
etc...

The data key name for the data list = id

The imageUrl for the image button is bound to picId.

When the image button is clicked I need to get the selectedValue of the of
the item

here's what I've got:

Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
DataList1.ItemCommand
If e.CommandName = "test" Then
Dim var1 As String
Dim dl As New DataList
dl = Me.FindControl("DataList1")
var1 = dl.SelectedValue.ToString


End If

End Sub

All i'm getting is a null reference error on line - var1 =
dl.SelectedValue.ToString

Can anyone point me in the right direction.

Many thanks
 
G

Gregory A. Beamer

Hi all

Have a bit of a problem..

I have a data list and in the item template I have an image button.
The Data list is data bound to sql data:
id
picId
etc...

The data key name for the data list = id

The imageUrl for the image button is bound to picId.

When the image button is clicked I need to get the selectedValue of
the of the item

here's what I've got:

Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
DataList1.ItemCommand
If e.CommandName = "test" Then
Dim var1 As String
Dim dl As New DataList
dl = Me.FindControl("DataList1")
var1 = dl.SelectedValue.ToString


End If

End Sub

All i'm getting is a null reference error on line - var1 =
dl.SelectedValue.ToString

Can anyone point me in the right direction.

Wrong direction. Let's get back on course.

You don't have to search for a control on its own events, so the entire
idea of setting up dl is incorrect.

The event args (e) will have the information you need on finding the
proper row. If you have properly set up the id as a row id, it will be
there on e.Item. I don't have code in front of me.

You can then get information from the DataList by simply using the
DataList. Here is an example I found on the web, although I have not
tested it, so it is merely an example of how to do it, not necessarily a
100% correct implementation. This one grabs the key, which may or may
not be your desire, but use it as a guideline to get you to the answer:



Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
DataList1.ItemCommand
If e.CommandName = "test" Then

Dim key As String = _
DataList1.DataKeys.Item(e.Item.ItemIndex)

'You should then be able to pull the value by key name

End If

End Sub


Like I said, this is a rough cut, but it should have enough info to help
you navigate the waters.
 
J

James Page

Gregory to the rescue again!!

Here's what I've done works perfectly:

Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
DataList1.ItemCommand
If e.CommandName = "test" Then
Dim rowindex As String = e.CommandArgument.ToString()

End If
End Sub

The command arguement evaluates "id". Works like a charm.

Thanks again
 
G

Gregory A. Beamer

Gregory to the rescue again!!

Here's what I've done works perfectly:

Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
DataList1.ItemCommand
If e.CommandName = "test" Then
Dim rowindex As String = e.CommandArgument.ToString()

End If
End Sub

The command arguement evaluates "id". Works like a charm.

Good shot. Thanks for posting back.
 

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,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top