Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - gridview help

 
Thread Tools Search this Thread
Old 12-07-2006, 10:50 PM   #1
Default gridview help


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!




  Reply With Quote
Old 12-08-2006, 06:24 AM   #2
Steven Cheng[MSFT]
 
Posts: n/a
Default RE: gridview help
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.InvariantCultu re);
}
}
}
=====================


Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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



Steven Cheng[MSFT]
  Reply With Quote
Old 12-09-2006, 07:30 PM   #3
 
Posts: n/a
Default Re: gridview help
Thanks Suresh. that worked.

"Suresh" <> wrote in message
news:15FC4387-239A-4C38-B565-...
>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.
>
> "" wrote:
>
>> 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!
>>
>>
>>





  Reply With Quote
Old 12-11-2006, 02:14 AM   #4
Steven Cheng[MSFT]
 
Posts: n/a
Default Re: gridview help
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[MSFT]
  Reply With Quote
Old 01-13-2009, 12:43 PM   #5
bioscom
Junior Member
 
Join Date: Nov 2008
Location: Port Harcourt, Nigeria
Posts: 3
Send a message via Yahoo to bioscom
Default
Quote:
Originally Posted by 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.
Steven Cheng

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


bioscom
bioscom is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Gridview row information disappearing after selectedIndexChanged Ry99 General Help Related Topics 0 07-13-2009 03:35 PM
ASP GridView hyperlinks driving me insane! WillC999 General Help Related Topics 0 10-28-2008 11:02 AM
gridview inside usercontrol to XLS tejesvi Software 0 04-19-2008 08:52 AM
Checkbox values problem in gridview thanigaimani.thirumalai Software 0 11-09-2007 05:12 AM
Update and Delete From Gridview Usman Software 0 11-01-2006 10:05 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46