?
Solved

Retrieve a value from datalist

Posted on 2005-05-10
12
Medium Priority
?
676 Views
Last Modified: 2008-02-01
Hi all,

I have a datalist bind to a dataset on prerender. The list design is as follow:


<asp:datalist id="MyList" runat="server" RepeatColumns="2">
<ItemTemplate>
.......
                                                                           <%# DataBinder.Eval(Container.DataItem, "ProductID") %>....
     <%# DataBinder.Eval(Container.DataItem, "Description") %>....                                                  <asp:linkbutton id="AddItem" CommandName="Add"  Runat="server">Add Item</asp:linkbutton></b>
     ........                                                                      </ItemTemplate>
</asp:datalist>

I need whenever I click on the AddItem button to retrieve the ProductID value depending on the selected index. I tried to use the e.item.itemindex to retrieve the value but it is not working i.e dataset.Tables(0).Rows(e.item.itemindex).item("ProductID").Tostring() it tells me that there is no data at this position.
and the Dataset= mylist.datasource

Any help would be appreciated.
0
Comment
Question by:jschnei555
  • 8
  • 4
12 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 13968083
for you to access your original dataset, you must persist it so that it is available across postbacks:

Private ReadOnly Property PageDataSet() As DataSet
      Get
            If ViewState("PageDataSet") Is Nothing Then
                  ViewState("PageDataSet") = New DataSet
            End If
            
            Return DirectCast(ViewState("PageDataSet"),DataSet)
      End Get
End Property

Populate your data to PageDataSet and bind that to your DataList. And across a postback, you can retrieve values from the persisted dataset
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13968151
Further explanation,
==============
Persisting your DataSet in ViewState has a number of advantages over Session but if you want to use Session instead of ViewState (I would advise against it)
Private ReadOnly Property PageDataSet() As DataSet
     Get
          If Session("PageDataSet") Is Nothing Then
               Session("PageDataSet") = New DataSet
          End If
         
          Return DirectCast(Session("PageDataSet"),DataSet)
     End Get
End Property

The Session Object has an expiry. After it expires the data will no longer be there. Whereas storing the data in ViewState allows for the data to be there because it is stored on the client. Storing it in Session protects the data from prying eyes. Storing it in ViewState is not secure unless you set EnableViewStateMac to True in the page directives:

<%@ Page enableViewStateMac="True" %>
0
 

Author Comment

by:jschnei555
ID: 13968612
Sorry but I didn't get how can i populate the data to pagedataset? Can you give me more details?

Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:b1xml2
ID: 13968626
The PageDataSet is just a custom property that you implement on your page. The point is not so much the name of that property as much as the persistence of the data.
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13968733
for instance where you are loading the data and binding it...

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  If Not IsPostBack Then
      'code example ONLY
      Dim connection As New System.Data.OleDbConnection(..)
      Dim adapter As New System.Data.OleDbDataAdapter("select * from table1",connection)
      adapter.Fill(PageDataSet)
      MyList.DataSource = PageDataSet.Tables(0)
      MyList.DataBind()
  End If
End Sub
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13968742
alternatively, you can retrieve the ProductID found inside the DataList but for this, we need to see your ItemTemplate structure even more.
0
 

Author Comment

by:jschnei555
ID: 13968868
I'm binding the data on page prerender using a function that returns a dataset.
Hence my original code is: mylist.datasource=PopulateList() where populate list returns a dataset and it exists in a different class
How can I use this with the pagedataset since it is a read only property.
Thanks for all the help...

0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 2000 total points
ID: 13968895
Ah!!!
PageDataSet.Merge(PopulateList())
MyList.DataSource = PageDataSet.Tables(0)
MyList.DataBind()
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13968923
That merges the data to the DataSet (a copy)
0
 

Author Comment

by:jschnei555
ID: 13969015
Thanks a lot :):)
I used the merge and it worked perfectly and on ItemCommand I retrieved the value as following:
x= pagedataset.tables(0).rows(e.item.itemindex).item("ProductID").tostring() and it worked

but is there a better way to do it? The itemtemplate is like I have described earlier, is there something in specific that you need to know?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13969082
If you were using a DataGrid, you could specify the ProductID as the DataKeyField and would not need to persist the DataTable....

in which case OnItemCommand it would be like so
x = DataGrid1.DataKey(e.Item.ItemIndex).ToString

but you need the DataList to be used because of the ColumnDirection.

you could store the ProductId inside a Label and hide it:

<asp:ItemTemplate>
<asp:Label Id="lblProductID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ProductID") %>' /> ....
</asp:ItemTemplate>

and your code would be
x = DirectCast(e.Item.FindControl("lblProductID"),Label).Text

in which case you can avoid persisting the data across postbacks. However if you should need a lot more than this, you should keep the persistence.

0
 

Author Comment

by:jschnei555
ID: 13969118
b1xml2,

Thanks a lot for your help and for all the information :):):)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question