gridview help

G

Guest

Hi all,

I am trying to have a linkbutton control with the commandname property set.
However, when I invoke it, the CommandArgument property ends up coming to be
blank. However, if I click on the buttonfield control i get back the
rowindex in CommandArgument. Why is that? How do I get my custom linkbutton
to work?

public void GridView_Command(object sender, GridViewCommandEventArgs e)

{

switch (e.CommandName)

{

case "EditSelect":

Response.Write(e.CommandArgument);

Response.End();

GridView_Edit(Convert.ToInt32(e.CommandArgument));

break;

}

}

<asp:ButtonField ButtonType="Link" CommandName="EditSelect" HeaderText="ID"
Text="Edit" />

<asp:TemplateField HeaderText="ID" HeaderStyle-HorizontalAlign="left"
ItemStyle-HorizontalAlign="left">

<ItemTemplate>

<asp:LinkButton ID="btnedit" runat="server" Text='<%#Eval("feeid") %>'
CommandName="EditSelect" />

</ItemTemplate>

</asp:TemplateField>



TIA!
 
S

Steven Cheng[MSFT]

Thanks for Suresh's input.

Hi Param,

As for the ASP.NET GridView control, it will use the CommandArgument
property of Command button(Button or LinkButton) to hold the RowIndex
parameter. The built-in CommandField/ButtonField will use the following
code to assign the rowindex at Button's creation time:

===reflector diassembled code===
control1.Text = this.Text;
control1.CommandName = this.CommandName;
control1.CommandArgument =
rowIndex.ToString(CultureInfo.InvariantCulture);
====================

Therefore, if you have a custom template field that will add a button
control to trigger a GridView event, you can manually set the current
rowindex into the button's "CommandArgument" field. e.g.

======in aspx template==========
<asp:GridView ID="GridView1" .............. AllowPaging="True" PageSize="4">
<Columns>
..................

<asp:TemplateField>
<ItemTemplate>

<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Select" CommandArgument="<%# Container.DisplayIndex %>">My
Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
...........
=============================

or you can also use RowDataBound event to set the commandargument. e.g.

======in RowDataBound event==========
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btn = e.Row.FindControl("btn1") as LinkButton;

if (btn != null)
{

btn.CommandArgument =
e.Row.RowIndex.ToString(CultureInfo.InvariantCulture);
}
}
}
=====================


Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks Suresh. that worked.

Suresh said:
I believe using the CommandArgument to determine the row index only works
when you use the command field in a gridview.

Not sure if row index is what you are after in the CommandArgument??
If it is try the following:

(FYI you can bind any data item value to the commandargument property as
well)

asp:LinkButton ID="btnedit" runat="server" Text='<%#Eval("feeid") %>'
CommandName="EditSelect" CommandArgument="<%# Container.DataItemIndex %>"
/>

HTH,
Suresh.
 
S

Steven Cheng[MSFT]

Hi Param,

DataItemIndex refer to the index of current databound item in the
datasource. The value may differ from actual GridView item index when you
using paging. the DisplayIndex matchs the actual GridView itemindex no
matter you use paging or not.

<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Select" CommandArgument="<%# Container.DisplayIndex %>">My
Select</asp:LinkButton>

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Joined
Nov 28, 2008
Messages
3
Reaction score
0
Steven Cheng[MSFT] said:
Hi Param,

DataItemIndex refer to the index of current databound item in the
datasource. The value may differ from actual GridView item index when you
using paging. the DisplayIndex matchs the actual GridView itemindex no
matter you use paging or not.

<asp:LinkButton ID="LinkButton1" runat="server"
CommandName="Select" CommandArgument="<%# Container.DisplayIndex %>">My
Select</asp:LinkButton>

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
Steven Cheng

Thank you very much for this post, it just solved my problem.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top