I've looked at pages and pages of documentation and forum threads, but I can't seem to get around this issue. Using the WebDataGrid (.NET 3.5) I have a grid with nine or so templated columns. The first column template is a LinkButton; I have that set up for an ItemCommand to process it (gets the detail data and displays it on another web page). I have my CommandName set up, and what I need to do is either pass the container for that row, or the row index, back to the server side ItemCommand method so I can grap some other fields from that row. Basically, in ItemCommand I need to know what row that LinkButton was clicked.
I've tried everything to get the CommandArgument to pass back the record number but nothing works. I tried the idea presented in another thread, using:
CommandArgument="<%# Container.Cell.Row.Index %>"
but that always gives back the error 'Cell' is not a member of 'System.Web.UI.Control'.
So my big question is: In ItemCommand (for a WebDataGrid) how can I either get the current row that the LinkButton was click on (in ItemCommand), or at least get passed back the row index?
I'm frustrated... hope someone can help. Thx.
/Tom
But when I used this
CommandArgument=<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).Item, "Row.Index") %>
I get an error
'TemplateContainer' is a type in 'UI' and cannot be used as an expression.
please suggest solution.
Maybe doing something like that will help?
CommandArgument='<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).DataItem, "CreatedDate") %>.<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).DataItem, "CreatedDate.Milliseconds") %>'
Yep. The current version won't let you reference Row.Index.
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Row'.
Unfortunately, I need then to keep a DateTime field as a command argument. It then loses the milliseconds. I can see them in the grid, but the command argument loses them. Any suggestions?
CommandArgument='<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).DataItem, "CreatedDate") %>' />
I wil try this. I tried the below and it worked on the first page of the grid. On later pages, the Command Argument gets cleared out somewhere, even though I see the correct value set in wdgNotes_InitializeRow for rows on the later pages, just like the first page. By the time it gets to the wdgNotes_ItemCommand on pages after the first page, the Command Argument is an empty string rather than the value it originally held.
protected void wdgNotes_InitializeRow(object sender, Infragistics.Web.UI.GridControls.RowEventArgs e) { TextBox tbNote = (TextBox)e.Row.Items[0].FindControl("tbNote"); tbNote.Text = tbNote.Text.Replace("~", "\r\n");
Button btnEditNote = (Button)e.Row.Items[6].FindControl("btnEditNote");
string sGroupID = (string)(((DataRowView)e.Row.DataItem)["GroupID"]); //CheckSecurity(); // Turn off Edit Note button if they do not have Read/Write or Read/Change access or // if they are in a different group than who created the note. if (((sAccess == "RW") || (sAccess == "RC")) && (sGroupID == sEmpGroup)) { btnEditNote.Enabled = true; btnEditNote.CommandArgument = e.Row.Index.ToString(); } else { btnEditNote.Enabled = false; } }
protected void wdgNotes_ItemCommand(object sender, Infragistics.Web.UI.GridControls.HandleCommandEventArgs e) { Button btnEditNote = sender as Button; if (btnEditNote != null) { //CheckSecurity(); // If they press the Edit note button, pull up the Edit Note popup window. // The security check is redundant, but an extra check does not hurt. if ((btnEditNote.CommandName == "Edit Note") && ((sAccess == "RW") || (sAccess == "RC"))) { int intRowIndex = Convert.ToInt16(btnEditNote.CommandArgument); DataRowView drvRow = (DataRowView)(wdgNotes.Rows[intRowIndex].DataItem); tbNoteToAddEdit.Text = ((string)(drvRow["Note"])).Replace("~", "\r\n"); dtbCreatedDate.Visible = true; lblCreatedDate.Visible = true; dtbCreatedDate.DateValue = (DateTime)(drvRow["CreatedDate"]); ViewState["NoteIdentifier"] = (string)(drvRow["Identifier"]); ViewState["Private"] = (string)(drvRow["Private"]); ViewState["GroupID"] = (string)(drvRow["GroupID"]); lblAddEditHeader.Text = sConstEditNote; btnAddEditNotePopup.Text = "Update"; AddEditNotePopupWindow.Show(); } } }
The Container needs to be cast to Infragistics.Web.UI.TemplateContainer. You can either store in the CommandArgument a unique identifier of the row, like a data key for example (here DataItem should be used)
CommandArgument=<%# DataBinder.Eval(((Infragistics.Web.UI.TemplateContainer)Container).DataItem, "PostId") %>
or reference the actual underlying object. In case of the ItemTemplate it is the cell. To get the row index, something like that can be done (here the Item property should be used):
This latter approach however has a problem, the Item property is currently referencing the same object as the DataItem. It's been fixed and the fix is coming up shortly.