Get row number of Linkbutton

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
 
M

Mohamed Mosalem

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
 
T

tshad

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top