|
-
January 10th, 2012, 08:22 AM
#1
CurrentRowIndex problem in Gridview
I have an ItemTemplate in my gridview. When I enter the gridview_RowCommand function, I'd like to get my CurrentRowIndex like I do for the normal DataBound columns.
Sadly, the COmmandArguemnt is empty. I can see there's a property for it, but i thought it would be there automatically like normal. Or should I fill it out, and if so, how?
-
January 10th, 2012, 11:08 AM
#2
Re: CurrentRowIndex problem in Gridview
The CommandArgument will be automatically set only for ButtonField column. Since you are using ItemTemplate, you need to specify the CommandArgument manually. You may set it in gridview's RowDataBound event:
linkbutton.CommandArgument = e.Row.RowIndex.ToString();
-
January 10th, 2012, 12:52 PM
#3
Re: CurrentRowIndex problem in Gridview
Ah...that seems to work. Now I only need to get my controls...
I tried
Code:
if (e.Row.RowIndex < 0) return;
LinkButton VaerdiEdit = (LinkButton)gwAnalyserAddEdit.Rows[e.Row.RowIndex].FindControl("LbVaerdi");
if (VaerdiEdit!=null)
VaerdiEdit.CommandArgument=e.Row.RowIndex.ToString();
but then it complains about "Index was out of range. Must be non-negative and less than the size of the collection."
-
January 10th, 2012, 02:09 PM
#4
Re: CurrentRowIndex problem in Gridview
It also seems as if this event fires before the row is inserted...
-
January 10th, 2012, 02:34 PM
#5
Re: CurrentRowIndex problem in Gridview
try this:
LinkButton VaerdiEdit = e.Row.Cells[0].Controls[0] as LinkButton;
another way is to specify the CommandArgument in aspx:
<asp:Button ... CommandArgument="<%# Container.DataItemIndex %>" />
-
January 10th, 2012, 03:11 PM
#6
Re: CurrentRowIndex problem in Gridview
I solved the problem. I moved the code to the RowCreated event, and I added a class variable int idx that I set in the RowDataBound, and then use in RowCreated. That works!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|