I have ajax enabled on my grid. I'd like to force a full page postback after a new row is added. Is there a way to do this?
There are a couple of ways I can think of off the top of my head. The first would be to use the BeforeXMLHTTPRequest client-side event that the WebGrid fires before any AJAX interaction - http://help.infragistics.com/NetAdvantage/NET/2008.3/CLR3.5
When you handle this event, you can cancel the request if the action was a new row added, and call __doPostBack manually instead.
The second option would be to handle this from the server-side. When you handle the RowAdded server-side event, emit a scriptblock that calls __doPostBack on the client-side. This would force the client to first complete the add asynchronously, and then perform a full postback.
Hope this helps,
-Tony
Tony Lombardo"]There are a couple of ways I can think of off the top of my head. The first would be to use the BeforeXMLHTTPRequest client-side event that the WebGrid fires before any AJAX interaction - http://help.infragistics.com/NetAdvantage/NET/2008.3/CLR3.5 When you handle this event, you can cancel the request if the action was a new row added, and call __doPostBack manually instead.
Ok, tried #1. Strange thing is that BeforeXMLHTTPRequest only seems to fire for Updates but not when a new row is inserted. The following function would only give me hi7 (update) and no hi5(add new row):
function BeforeXmlHttpRequest(gridName, type) { alert('hi' + type); }
Also tried dopostback with ClientSideEvents-AfterRowInsertHandler. It would indeed postback when I clicked on the Add Row button but won't give me a new row after the postback.
Tony Lombardo"] The second option would be to handle this from the server-side. When you handle the RowAdded server-side event, emit a scriptblock that calls __doPostBack on the client-side. This would force the client to first complete the add asynchronously, and then perform a full postback.
Sorry don't know where/how exactly to handle the RowAdded server-side event? Is there any sample code. Right now my aspx.vb page just has an empty sub:
Protected Sub UltraWebGrid1_AddRow(ByVal sender As Object, ByVal e As Infragistics.WebUI.UltraWebGrid.RowEventArgs) Handles UltraWebGrid1.AddRow End Sub
Any code I put in there doesn't do anything.
Thanks for trying to help out Tony!
Regards,Ali
P.S. Is there a way to initiate rowupdate from the ClientSideEvents-AfterRowInsertHandler? Then I could immediately do a postback after update.
Tony,
I did the following with AfterRowInsertHandler...
ClientSideEvents-AfterRowInsertHandler="AfterInsert"
function AfterInsert(gridName, rowId) { var row = igtbl_getRowById(rowId); row.processUpdateRow(); __doPostBack(null); }
This works well. I click on the Add Row button. The grid adds a new row, then updates (saves) the record, then does a postback. I see the new identity value in the identity column. Now the only remaining issue is that after the postback the last row that was just inserted is kinda hiding. You have to scroll down to see it. I wish there was a way to bring focus to the last row after any postback.
alirizvi said:I wish there was a way to bring focus to the last row after any postback.
You can use the "scrollToView" function available off of the row object
Use the InitializeLayout client-side event and inside of your function get a reference to the row in question. ie.
var row=igtbl_getRowById(rowID).scrollToView();
By the way, if the only reason you want to postback is to retrieve the identity value, you can accomplish that during the grid's async processing of the update. It sounds like you're probably doing something a little more complex, but I wanted to let you know just in case.
Tony Lombardo"]I wish there was a way to bring focus to the last row after any postback. By the way, if the only reason you want to postback is to retrieve the identity value, you can accomplish that during the grid's async processing of the update. It sounds like you're probably doing something a little more complex, but I wanted to let you know just in case.[/quote] Tony, yes that's what I'm trying to do (retrieve the identity value). Could you please elaborate on the async processing part. I don't know how to do that so I'm jumping through hoops :-)
I wish there was a way to bring focus to the last row after any postback.
[/quote]
Tony, yes that's what I'm trying to do (retrieve the identity value). Could you please elaborate on the async processing part. I don't know how to do that so I'm jumping through hoops :-)
Wow, heady stuff!! But it works.
Thanks much, Tony.
The trick is to get the Identity value from the database when you execute the instert. You can do this by adding a select statement to your insert command for your SQL DataSource For example, using the Northwind Orders table:
InsertCommand="INSERT INTO [Orders] ([CustomerID], [EmployeeID], [OrderDate], [RequiredDate], [ShippedDate], [ShipVia], [Freight], [ShipName], [ShipAddress], [ShipCity], [ShipRegion], [ShipPostalCode], [ShipCountry]) VALUES (@CustomerID, @EmployeeID, @OrderDate, @RequiredDate, @ShippedDate, @ShipVia, @Freight, @ShipName, @ShipAddress, @ShipCity, @ShipRegion, @ShipPostalCode, @ShipCountry) SELECT @NewCustID = SCOPE_IDENTITY()"
Notice the SCOPE_IDENTITY() function being used at the end. Now you add a parameter to match which will capture the value of SCOPE_IDENTITY in @NewCustID. Once you have that value, you can assign it into the grid cell during the add new process. To do that you add an eventhandler to the SQLDataSource's Inserted event, and one to the WebGrid's AddRow event.
I've attached a project that demonstrates this - the grid is bound to a SQLDataSource which is bound to the Northwind database, the Orders table. You will need to update the ConnectionString to run the sample, and possibly run the Project Upgrade Utility unless you happen to be on 8.3.20083.1009.
Hope this helps!