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
Hi Devin,
1. Thanks so much for answering my question so fast. I really appreciate it. It works and I could pass the value to my modal. The only problem that I have right now is that, when the parent page is loaded I click on the linkbutton for the first time, it doesn't load the modal until I click on it the second time. Any idea why this is happening?
2. Can I use similar ideas like the asp LinkButton for an asp Dropdownlist in an ultrawebgrid.
I have an ultrawebgrid that I have embedded an asp:Dropdownlist, from this drop down list, I want to be able to select the item from it and it either redirect me to a new .aspx page or it does something in the code behind.
Here is my situation:
Ultrawebgrid with 5 columns and this webgrid contains many row;
Col1: ID
Col2: ProductName
col3:ProductType
col4: Status
col5: dropdownlist (Add, Remove, Edit, and Start)
When I select the Add item from the dropdownlist, I want to grab the data from the row that the user selects the Add item that belongs to the dropdownlist.
From that Add item selection, I would like to determine if it redirects to another .aspx page or allow the user to add a new row onto the webgrid. Similarly, these actions apply to the Remove, Edit, and Start.
Remove: If Remove is selected, then it removes the row.
Edit: If Edit is selected, it allows the user to edit certain cell on the row
Start: If Start is selected, it would start a service
Do you think it is wise to use an asp:Dropdownlist or should I use the dropdownlist from the ultrawebgrid. Which ever method you recommend, I would gladly take and please show me how to actually know which item from the dropdownlist I have selected and once I selected the item, how to do my conditions. I have tried many ways and it doesn't really work. From you reply above, it seems like it works with the asp linkbutton so there must be a way to do it with either the asp:Dropdownlist or the dropdownlist from the ultrawebgrid.
I am spent so much time on these two issues and if you could help me out that would be greatly appreciate it.
Thanks again!
I have figure out the solutions to my question above in regards to my modal not register/initialize/load on the first click of the LinkButton.
All I have to do was to call the OnLoad method and call the same script code that I called on the OnClick method. The OnLoad registers/initilialize the modal to the LinkButton for the first time.
Hope this will help to all who experiences this same problem.
Happy Coding!
HS2