Solved

basic, datalist retrieve datakey field

Posted on 2004-04-28
9
2,368 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 4

Expert Comment

by:BChan
ID: 10944248
0
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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 .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now