Solved

basic, datalist retrieve datakey field

Posted on 2004-04-28
9
2,371 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
Technology Partners: 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 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

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!

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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