Solved

basic, datalist retrieve datakey field

Posted on 2004-04-28
9
2,365 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grab and show facebook pages event calendars 6 65
Not showing page correctly 3 31
C#  Radio button search for Date not DateTime 4 35
Code works but it's slow 24 45
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

23 Experts available now in Live!

Get 1:1 Help Now