Solved

basic, datalist retrieve datakey field

Posted on 2004-04-28
9
2,364 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

16 Experts available now in Live!

Get 1:1 Help Now