Hello,
Does anyone know how to associate a LinkButton (webform control) to a webgrid row data?
I have an ultrawebgrid with three columns, column ID, column Status, and column Command (Add). The Add command is a link button (web form control). Everytime I click on the Add link button, I need to retrieve the Column ID and Column Status and and pass it to a modal dialog to change the current status and update it into the database.
Thanks in advance for your help.
Hs2
First, you can create a TemplatedColumn to place the LinkButton control inside of the column:
<igtbl:TemplatedColumn BaseColumnName="ProductName" IsBound="True" Key="ProductName"><Header Caption="ProductName"><RowLayoutColumnInfo OriginX="1" /></Header><CellTemplate><asp:LinkButton runat="server" ID="MyLinkButton" Text="Press Me" CommandName="Edit" OnClick="MyLinkButton_Click" CommandArgument="<%# Container.Cell.Row.Index %>" /></CellTemplate><Footer><RowLayoutColumnInfo OriginX="1" /></Footer></igtbl:TemplatedColumn>
In the LinkButton in the template there are a couple things to notice. First, the Click event is specified with a event handler MyLinkButton_Click method defined. Next, the CommandArgument property is specified. Its value is set to the Index of the Row that the Cell the LinkButton is in.
Then in your code behind, all you need to do is define the Click events handler then access the LinkButtons CommandArgument to figure out what row was clicked. Once you know the row, you can get the values from any cell in that row:
protected void MyLinkButton_Click(object sender, EventArgs e){ LinkButton button = (LinkButton)sender; System.Diagnostics.Debug.WriteLine(String.Format("Button Clicked in row {0}", button.CommandArgument.ToString()));
object id = this.UltraWebGrid1.Rows[int.Parse(button.CommandArgument)].Cells.FromKey("ColumnID").Value; object status = this.UltraWebGrid1.Rows[int.Parse(button.CommandArgument)].Cells.FromKey("ColumnStatus").Value;}
Hope that helps.
Devin
Help! I have added a link button as described above and it works.
However, when the grid is Sorted, the Container.Cell.Row.Index references the original row -- before the Sort. I have tried client sort and server sort.
How can I get the link button to reference the current row??!!
Thanks!!
Just in case anyone else stumbles upon this in search, I believe this forum thread gives the best resources and information (and additional forum posts with answers)
http://forums.infragistics.com/forums/p/20982/75631.aspx#75631
Hope this helps.
Is it possible to hadle the same Click event on the Client Side. What I want to achive is I have a hyperlink and on its Click event I want to run some Javascript code.How can i achive this,
Thank you in advance
Very nicely done. Here's the same code in vb.net assuming your "sender" is a LinkButton.
Dim i As Int32 = CType(CType(sender, LinkButton).NamingContainer, CellItem).Cell.Row.Index
in the link button click event:
int rowIndex = ((CellItem)((Control)sender).NamingContainer).Cell.Row.Index;
This appears to work in all cases...