I added a linkbutton in a TemplatedField in a WebGrid.When I Click the LinkButton,How To Get the Row index in LinkButton Click event??
Alternatively -
templated column markup:
<igtbl:TemplatedColumn> <CellTemplate> <asp:LinkButton runat="server" ID="lblName" Text='<%# Eval("BoundColumnName") %>' CommandName="YourCommandName" CommandArgument="<%# Container.Cell.Row.Index %>" OnCommand="OnButtonCommand" /> </CellTemplate> </igtbl:TemplatedColumn>
Code behind:
protected void OnButtonCommand(object sender, CommandEventArgs e) { LinkButton btn = (LinkButton)sender; int rowIndex = int.Parse(btn.CommandArgument); }
Hello Guys,
@Emil - thanks for sharing your approach in forums, that sounds like a great idea. In any case, much better than the best I could come up with:
<igtbl:UltraWebGrid ID="UltraWebGrid1" runat="server" Height="200px" Width="325px" DataSourceID="AccessDataSource1" > <Bands> <igtbl:UltraGridBand> <Columns> <igtbl:TemplatedColumn> <CellTemplate> <asp:DropDownList runat="server" ID="DropDownList1" OnInit="DropDownList1_OnInit"> </asp:DropDownList> </CellTemplate> </igtbl:TemplatedColumn> </Columns> </igtbl:UltraGridBand> </Bands> ...</igtbl:UltraWebGrid> protected void DropDownList1_OnInit(object sender, EventArgs e) { DropDownList dropDownList = sender as DropDownList; CellItem parentCell = (CellItem) dropDownList.NamingContainer;
int rowIndex =parentCell.Cell.Row.BandIndex;
}
Hi Rumen,
I've just hit another issue with the RowIndex while sorting the original value of the RowIndex is maintained. Is this correct behaviour? Or may be I should ask what is the best practice for handling RowIndex and Sorting?
I would expect the RowIndex to change to the new order after the Sort. I'm using OnServer sorting...
Just a word of warning - relating to my previous post about the RowIndex and Sorting:
When you use the RowIndex decraratively in the markup: <%# Container.Cell.Row.Index %>
and sorting is used (OnServer in my case) the RowIndex is not repopulated after the sort!
So please be warned if you use the Alternative code sample above ...
I would guess that is probably related to ASP.NET page lifecycle. Everythin in the ASPX is processed before OnInit (at the very beginning), while sorting may take place later in the lifecycle, depending on your setup, but the templates had been already evaluated before that - hence the different indices.
Maybe you can try my approach with NamingContainer? Is it applicable in your case?