Solved

basic, datalist retrieve datakey field

Posted on 2004-04-28
9
2,374 Views
Last Modified: 2012-08-13
hi,
I have a datalist, i am displaying some product names, i want to retrieve the product ids. i know this is sillly but ive been struggling with this for the past 2 hours, can any1 help me?

thanks
heart
0
Comment
Question by:heartguy123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
9 Comments
 
LVL 4

Expert Comment

by:BChan
ID: 10943878
Please post your dataacess code and the HTML for the datalist.
0
 

Author Comment

by:heartguy123
ID: 10943969
<asp:DataList ID="dlstProduct" DataKeyField="ProductID" OnSelectedIndexChanged="showItem" Runat="server">
                                                            <ItemTemplate>
                                                                  <asp:LinkButton  Text='<%# DataBinder.Eval(Container.DataItem,"ProductName")%>' Runat=server>
                                                                  </asp:LinkButton>
                                                            </ItemTemplate>
                                                      </asp:DataList>

--------------
public void showItem(Object src, System.EventArgs e)
            {
                  dlstProduct.DataBind();
                  lblTest.Text="hello";
                  string str1 = dlstProduct.DataKeys[dlstProduct.SelectedIndex].ToString();
                  lblTest.Text = str1;
                  lblTest.Text +="hello";

            }
0
 
LVL 4

Accepted Solution

by:
BChan earned 50 total points
ID: 10944130
heartguy123,
dlstProduct.DataKeys will return the primary key of the datasource.

After you have crewated the dataxtable, add this code before returining the result of the sql query:
          DataColumn[] keys = new DataColumn[1];
         keys[0] = dt.Columns["ProductID"]; //Change this to the appropiate column name
         // Then assign the array to the PrimaryKey property of the DataTable.
         dt.PrimaryKey = keys;

chage this and the rest of the code should work as is,
0
Industry Leaders: 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!

 

Author Comment

by:heartguy123
ID: 10944458
hi
I didn't create a datatable, im just confused and tired.
can you please explain this more?
thanks

0
 
LVL 4

Expert Comment

by:BChan
ID: 10945288
What is the datasource of the DataList  and how is it created?
 Does it come from a database? if it is from a database, what does teh SQL look like? You mak bbe able to simply put Productid as the first field in your select satement.



0
 
LVL 21

Assisted Solution

by:tovvenki
tovvenki earned 50 total points
ID: 10947649
Hi,
Just a have look at this url it should help you in solving your problem
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconDataListWebControl.asp

Regards,
Venki
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 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