Get row number of Linkbutton

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a DataGrid:

<asp:datagrid id="articleList" runat="server"
OnDeleteCommand="articleList_DeleteFile"
OnItemDataBound="articleList_ItemDataBound"
DataKeyField="FullName" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name"></asp:HyperLinkColumn>
<asp:TemplateColumn HeaderText="DownLoad">
<ItemTemplate>
<asp:LinkButton id="DownLoad" Text="DownLoad"
OnClick="DownLoad_Click" Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>

I need to get the path and filename of the row selected which is in the
DataKeys fields.

In my Delete button, I find it like so:

public void articleList_DeleteFile(Object sender, DataGridCommandEventArgs
e)
{
//First, get the filename to delete
string fileName = (string)articleList.DataKeys[e.Item.ItemIndex];

But for the link button I can't use e.Item.ItemIndex to find the row number,
so the following doesn't work.

public void DownLoad_Click(Object sender, EventArgs e)
{
//First, get the filename to delete
string fileName = (string)articleList.DataKeys[e.Item.ItemIndex];

I get an error:

'System.EventArgs' does not contain a definition for 'Item'

How do I get the Row number so I can do something like:

string fileName = (string)articleList.DataKeys[ROWNUMBER];

Thanks,

Tom
 
Hi,
You can set the CommandName property of the link button to "Download",
remove the OnCilck handler from the LinkButton and use the DataGridCommand
event handler as follows

public void articleList_ItemCommand(Object sender, DataGridCommandEventArgs
e)
{
if (e.CommandName=="Delete")
{
//delete
}
if (e.CommandName=="Download")
{
//download
}
}
Regards,
Mohamed Mosalem
 
That works.

Thanks,

Tom
Mohamed Mosalem said:
Hi,
You can set the CommandName property of the link button to "Download",
remove the OnCilck handler from the LinkButton and use the DataGridCommand
event handler as follows

public void articleList_ItemCommand(Object sender,
DataGridCommandEventArgs e)
{
if (e.CommandName=="Delete")
{
//delete
}
if (e.CommandName=="Download")
{
//download
}
}
Regards,
Mohamed Mosalem

tshad said:
I have a DataGrid:

<asp:datagrid id="articleList" runat="server"
OnDeleteCommand="articleList_DeleteFile"
OnItemDataBound="articleList_ItemDataBound"
DataKeyField="FullName" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="Name" DataTextField="Name"
HeaderText="File Name"></asp:HyperLinkColumn>
<asp:TemplateColumn HeaderText="DownLoad">
<ItemTemplate>
<asp:LinkButton id="DownLoad" Text="DownLoad"
OnClick="DownLoad_Click" Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>

I need to get the path and filename of the row selected which is in the
DataKeys fields.

In my Delete button, I find it like so:

public void articleList_DeleteFile(Object sender,
DataGridCommandEventArgs e)
{
//First, get the filename to delete
string fileName = (string)articleList.DataKeys[e.Item.ItemIndex];

But for the link button I can't use e.Item.ItemIndex to find the row
number, so the following doesn't work.

public void DownLoad_Click(Object sender, EventArgs e)
{
//First, get the filename to delete
string fileName = (string)articleList.DataKeys[e.Item.ItemIndex];

I get an error:

'System.EventArgs' does not contain a definition for 'Item'

How do I get the Row number so I can do something like:

string fileName = (string)articleList.DataKeys[ROWNUMBER];

Thanks,

Tom
 
Back
Top