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?
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!
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.
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 :-)
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();
-Tony
I got a one solution from developer support where he's using AfterRowUpdateHandler like so:
<ClientSideEvents AfterRowUpdateHandler="UltraWebGrid1_AfterRowUpdateHandler" />
<script type="text/javascript" id="igClientScript">function UltraWebGrid1_AfterRowUpdateHandler(gridName, rowId){ igtbl_doPostBack("UltraWebGrid1");}</script>