Getting DataKey Of DataList

892 views
Skip to first unread message

M@Ni

unread,
Dec 31, 2008, 1:25:29 PM12/31/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Suppose that there is a Datalist control, and in that control there
are further three controls, first is
label(to show title of Book), second is image(to show the picture of
that book) while the third control is a linkbutton(when this button is
clicked, it basically redirects the current page to BookDetail.aspx).
this third control basically get the DataKey of that record and then
redirect itself to BookDetail.aspx along with the BookId as a
QueryString, now the things is that, how come, i get the DataKey of
that record, from where the button is clicked, as the datalist
contains 10 records, which indirectly means that there are 10 DataKeys
present (1 for each record).

Protected Void LnkBtn_Click(----,----)
{
//here I just want to get the DataKey of that record, for which
the user wants to see more
//detail by clicking 1 book out 10 books (that are displayed in
datalist)

Int Bid=int.parse(DataList1.DataKeys[0].ToString()); // I new it
is wrong to get current DataKey
Response.Redirect(~/BookDetail.aspx?BId=”+BId);
}

Brandon

unread,
Dec 31, 2008, 3:22:47 PM12/31/08
to DotNetDe...@googlegroups.com
What is you datasource? You can define the datakeyname in the
properties pane of the control. Say your using sql and the field you
want to get is called "book_id", the that would be your datakeyname.
You don't even have to parse it as int, you can just add it right into
the query string , or better yet, you can set the datanavigate binding
to book_id, then set the navigate field to blah.aspx?bid={0}

Need more help, let me know brother

M@Ni

unread,
Jan 1, 2009, 2:54:08 AM1/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is not what i said, my problem is that how come i get the
datakeyname of that particular book, which is selected amongst the 10
books(that are displayed in the datalist control), as i already told u
tht every record that is gona dispaly in the datalist contains three
controls, 1)Label 2) Image 3)LinkButton.
now when i click on linkbutton,the code that i have ritten in code
behind against that linkbtn wil get executed and i want to take the
datakeyname of that book, from where linkbutton is clicked,as these
three controls are gona displayed 10 times, one for each record,so on
datalist there are actually 10 linkbutons that are displayed in
datalist. so out of that 10 records, only linkbuton of 1 record is
clicked at one time,so how come i get the datakeyname of that book in
code behind.

Brandon

unread,
Jan 1, 2009, 6:14:44 AM1/1/09
to DotNetDe...@googlegroups.com
Either you lost me or I lost you.

caloggins

unread,
Jan 1, 2009, 6:52:01 AM1/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
First, it's probably better to write the line:

Int Bid=int.parse(DataList1.DataKeys[0].ToString());

as this:

int bid = Convert.ToInt32(DataList1.DataKeys[0]);

(The second contains only one conversion, not two.)


If the link button is simply redirecting the to the new page, then
there is no reason you even need the click event. If the data key
column of the information is named 'Id', bind that to the PostBackUrl
of the LinkButton. If you actually need to do some processing of data
before the redirect, then you can pass the data key through the
CommandArgument field. It would probably be a good idea to read about
the DataList a bit, specifically the 'ItemCommand' event.

Cerebrus

unread,
Jan 1, 2009, 6:54:12 AM1/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Brandon is right in his assertion that there are easier ways to do
this... For instance, if you were using a DataGrid you would have
access to the HyperLinkColumn field which would allow you to assign a
DataNavigateUrlField and a DataNavigateUrlFormatString to a "select"
hyperlink declaratively. The GridView control offers a HyperLinkField
for the same functionality.

However, since you are using a DataList (a lighter, but less powerful
control), you have other options. What you are presently doing is
handling the Click event of the LinkButton. This same eventhandler
will be executed for each "Select" button in every row. There will be
no way of distinguishing (atleast directly) which row's Select button
was clicked. For this reason, the Button and LinkButton controls have
CommandName and CommandArgument properties which can be used when
handling the ItemCommand event of the DataList. The ItemCommand event
has a parameter called DataListCommandEventArgs which can tell you the
index of the LinkButton that was clicked in the DataList.

Something like the code below :
---

ASPX code:
-------------------
//In the DataList ItemTemplate:
..
..
<asp:LinkButton ID="lnkSel" runat="server" Text="Select"
CommandName="Select" />


Code-behind:
-------------------

protected void DataList1_ItemCommand(object source,
DataListCommandEventArgs e)
{
if(e.CommandName == "Select")
{
string bID;

// Get the index of the row from which this event was raised.
int idx = e.Item.ItemIndex;

// Get the DataKey with the same index.
object thisKey = DataList1.DataKeys(idx);
if(thisKey != null)
{
bID = thisKey.ToString();
Response.Redirect("~/BookDetail.aspx?BId=" + bID);
}
}
}
---

Cerebrus

unread,
Jan 1, 2009, 7:02:55 AM1/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Excellent point, Caloggins ! I forgot all about the PostBackUrl
property !

M@Ni

unread,
Jan 1, 2009, 12:44:33 PM1/1/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,i actualy want to do the same thing as (caloggins) said in his last
message that is:-

"If you actually need to do some processing of data
before the redirect, then you can pass the data key through the
CommandArgument field."

i actualy want that datakey, in the click event, of that linkbuton
which was clicked.
Protected Void LnkBtn_Click(----,----)
{
//here i want that key,and then i redirect to this url, from
(lnkbtn) click event, not from
//(DataList1_ItemCommand) event.
Response.Redirect(~/BookDetail.aspx?BId=”+BId);
}

Cerebrus i have an idea, that is, y not to store that datakey in
viewstate, as u already got the datakey(of that row, from where the
lnkbtn was clicked) in "DataList1_ItemCommand event" and then use the
viewstate under the click event of lnkbtn.

Also i new about postbackurl,but i want this thing in code behind,the
purpose behind it is that,i actually want to pass two parameters as a
querystring to BookDetail.aspx,m gona take the first parametr from the
datakey of datalist and the second parameter from another logic.
Otherwise the PostBackUrl is a better approach to take that
datakey,from where lnkbtn was clicked,bcoz its code is simple i.e

<asp:LinkButton ID="lnkbtn" runat="server" Text="ViewDetail"
PostBackUrl='<%# DataBinder.Eval
(Container.DataItem,"BId","ViewBookDetail.aspx?BId={0}") %>' />

its a much easier thing to do,but i want the datakey in the click
event of the linkbtn.
Cerebrus,Chuck,Caloggins,Brandon u people can realy help me in this
regard by giving me a sample code as cerebrus gave that,because its
easier to make sense from code rather just by telling the
solution.Thanks to u ppl, u ppl are realy showing concerns over my
problem,Thanks alot.


M@Ni

unread,
Jan 3, 2009, 7:56:07 AM1/3/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Cerebrus and Chuck, your help is needed in this regard,plz read my
immediate last post,Thanks alot, i realy want to solve this issue.

Cerebrus

unread,
Jan 3, 2009, 11:53:16 AM1/3/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Why ?
Reply all
Reply to author
Forward
0 new messages