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);
}
}
}
---
On Jan 1, 12:54 pm, "m...@ni" <[email protected]> wrote:
> 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.