I have designed a rad grid and enable in-place editing mode using this example but when I want to fire server side update command to delete in DB no action fires, and below my code:
protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
//Get the GridEditableItem of the RadGrid
GridEditableItem editedItem = e.Item as GridEditableItem;
//Get the primary key value using the DataKeyValue.
string ID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["id"].ToString();
//Access the textbox from the edit form template and store the values in string variables.
string name = (editedItem["name"].Controls[0] as CheckBox).Text;
string insertflag = (editedItem["insertflag"].Controls[0] as CheckBox).Text;
string updateflag = (editedItem["updateflag"].Controls[0] as CheckBox).Text;
try
{
//Open the SqlConnection
SqlConnection.Open();
//Update Query to update the Datatable
string updateQuery = "UPDATE Employees set name='" + name + "',insertflag='" + insertflag + "',updateflag='" + updateflag + "' where id='" + ID + "'";
SqlCommand.CommandText = updateQuery;
SqlCommand.Connection = SqlConnection;
SqlCommand.ExecuteNonQuery();
//Close the SqlConnection
SqlConnection.Close();
}
catch (Exception ex)
{
RadGrid1.Controls.Add(
new LiteralControl("Unable to update Employee. Reason: " + ex.Message));
e.Canceled =
true;
}
Label1.Text =
"Updated";
}
I have used label to change text to know whether it get into updatecommand or not, but no changes happens.
Are there any c# additional code to add???
Please advise
Regards
7 Answers, 1 is accepted
Please examine the following example.
... |
Hashtable values = new Hashtable(); |
editedItem.ExtractValues(values); |
try |
{ |
SqlConnection sqlConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"); |
sqlConnection.Open(); |
string updateQuery = "UPDATE Categories SET CategoryName='" |
+ values["CategoryName"].ToString() + "', Description='" |
+ values["Description"].ToString() + "' WHERE CategoryID='" |
+ editedItem.GetDataKeyValue("CategoryID").ToString() + "'"; |
SqlCommand myCommand = new SqlCommand(updateQuery, sqlConnection); |
myCommand.ExecuteNonQuery(); |
sqlConnection.Close(); |
} |
catch (Exception ex) |
... |
I hope this helps.
Best regards,
Daniel
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
I have used your code but no result, my application can't access or enter update command action,
when I put only code to change label in update command action it does not executed:
protected
void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
Label1.Text = "Updated";
}
This just code I put, but no effect, should I implement other actions to fire update command action in case edit on double click in place??
Regards
Besides the integrated debugger I suggest you another way to test whether the event is fired:
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server /> |
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) |
{ |
RadAjaxManager1.Alert("Updated"); |
} |
Regarding the update problems: Please share the modified code with us (I suppose you examined my previous example).
Best regards,
Daniel
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
After I have tested and used code above, no events fire, I think in my opinion that editCommand event that should be fired not updateCommand please check my code below:
using System; |
using System.Collections; |
using System.Configuration; |
using System.Data; |
using System.Linq; |
using System.Web; |
using System.Web.Security; |
using System.Web.UI; |
using System.Web.UI.HtmlControls; |
using System.Web.UI.WebControls; |
using System.Web.UI.WebControls.WebParts; |
using System.Xml.Linq; |
using Telerik.Web.UI; |
namespace FireBall.forms.settings |
{ |
public partial class defaccessright : System.Web.UI.Page |
{ |
protected void Page_Load(object sender, EventArgs e) |
{ |
} |
protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
{ |
RadAjaxManager1.Alert("Updated"); |
////Get the GridEditableItem of the RadGrid |
//GridEditableItem editedItem = e.Item as GridEditableItem; |
////Get the primary key value using the DataKeyValue. |
//string ID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["id"].ToString(); |
////Access the textbox from the edit form template and store the values in string variables. |
//string name = (editedItem["name"].Controls[0] as CheckBox).Text; |
//string insertflag = (editedItem["insertflag"].Controls[0] as CheckBox).Text; |
//string updateflag = (editedItem["updateflag"].Controls[0] as CheckBox).Text; |
//try |
//{ |
// //Open the SqlConnection |
// SqlConnection.Open(); |
// //Update Query to update the Datatable |
// string updateQuery = "UPDATE Employees set LastName='" + LastName + "',FirstName='" + FirstName + "',Title='" + Title + "',Address='" + Address + "',City='" + City + "' where EmployeeID='" + EmployeeID + "'"; |
// SqlCommand.CommandText = updateQuery; |
// SqlCommand.Connection = SqlConnection; |
// SqlCommand.ExecuteNonQuery(); |
// //Close the SqlConnection |
// SqlConnection.Close(); |
//} |
//catch (Exception ex) |
//{ |
// RadGrid1.Controls.Add(new LiteralControl("Unable to update Employee. Reason: " + ex.Message)); |
// e.Canceled = true; |
//} |
} |
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e) |
{ |
Label1.Text = "Updated"; |
} |
} |
} |
and this my aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="defaccessright.aspx.cs" Inherits="FireBall.forms.settings.defaccessright" %> |
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI" tagprefix="asp" %> |
<%@ Register assembly="dotnetCHARTING" namespace="dotnetCHARTING" tagprefix="dotnetCHARTING" %> |
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> |
<%@ Register assembly="Infragistics35.WebUI.UltraWebTab.v8.2, Version=8.2.20082.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.WebUI.UltraWebTab" tagprefix="igtab" %> |
<%@ Register assembly="Infragistics35.WebUI.WebHtmlEditor.v8.2, Version=8.2.20082.1000, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" namespace="Infragistics.WebUI.WebHtmlEditor" tagprefix="ighedit" %> |
<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head id="Head1" runat="server"> |
<title>Untitled Page</title> |
<link href="../../CSS/MainSheet.css" rel="stylesheet" type="text/css" /> |
<style type="text/css"> |
.style2 |
{ |
} |
.RadGrid_Vista |
{ |
font:11px/14px "segoe ui",arial,sans-serif; |
} |
.RadGrid_Vista |
{ |
border:1px solid #a7bac5; |
background:#fcfcfc; |
color:#333; |
} |
.MasterTable_Vista |
{ |
border-collapse:separate !important; |
} |
.MasterTable_Vista |
{ |
font:11px/14px "segoe ui",arial,sans-serif; |
} |
</style> |
<script type="text/javascript" id="igClientScript"> |
<!-- |
function UltraWebTab1_Click(oWebTab, oTab, oEvent){ |
//Add code to handle your event here. |
} |
// --> |
</script> |
</head> |
<body> |
<form id="form1" runat="server"> |
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server"> |
</telerik:RadStyleSheetManager> |
<telerik:RadScriptManager ID="ScriptManager1" runat="server" |
EnableTheming="True"> |
</telerik:RadScriptManager> |
<div> |
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> |
<ContentTemplate> |
<fieldset width="90%"> |
<legend class="labelHead">Access rights</legend> |
<table style="width:100%;"> |
<tr> |
<td align="center" class="style2"> |
<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server"> |
<script type="text/javascript"> |
<!-- |
var hasChanges, inputs, dropdowns, editedRow; |
function RowClick(sender, eventArgs) |
{ |
if(editedRow && hasChanges) |
{ |
hasChanges = false; |
if(confirm("Update changes?")) |
{ |
$find("<%= RadGrid1.MasterTableView.ClientID %>").updateItem(editedRow); |
// window.location="slamodule.aspx"; |
} |
} |
} |
function RowDblClick(sender, eventArgs) |
{ |
editedRow = eventArgs.get_itemIndexHierarchical(); |
$find("<%= RadGrid1.MasterTableView.ClientID %>").editItem(editedRow); |
} |
function GridCommand(sender, args) |
{ |
if (args.get_commandName() != "Edit") |
{ |
editedRow = null; |
} |
} |
function GridCreated(sender, eventArgs) |
{ |
var gridElement = sender.get_element(); |
var elementsToUse = []; |
inputs = gridElement.getElementsByTagName("input"); |
for (var i = 0; i < inputs.length;i++) |
{ |
var lowerType = inputs[i].type.toLowerCase(); |
if(lowerType == "hidden" || lowerType == "button") |
{ |
continue; |
} |
Array.add(elementsToUse, inputs[i]); |
inputs[i].onchange = TrackChanges; |
} |
dropdowns = gridElement.getElementsByTagName("select"); |
for (var i = 0; i < dropdowns.length;i++) |
{ |
dropdowns[i].onchange = TrackChanges; |
} |
setTimeout(function(){if(elementsToUse[0])elementsToUse[0].focus();},100); |
} |
function TrackChanges(e) |
{ |
hasChanges = true; |
} |
--> |
</script> |
</telerik:RadCodeBlock> |
<telerik:RadGrid ID="RadGrid1" runat="server" |
DataSourceID="SqlDataSource1" GridLines="Vertical" Skin="Office2007" |
AllowAutomaticDeletes="True" AllowAutomaticInserts="True" |
AllowAutomaticUpdates="True" |
AllowMultiRowSelection="True" AllowSorting="True" BorderWidth="1px" |
ShowGroupPanel="True" ShowFooter="True" |
AutoGenerateColumns="False" onitemupdated="RadGrid1_ItemUpdated" |
onupdatecommand="RadGrid1_UpdateCommand"> |
<ItemStyle Font-Bold="False" Font-Italic="False" |
Font-Overline="False" Font-Strikeout="False" Font-Underline="False" |
Wrap="True" /> |
<MasterTableView DataSourceID="SqlDataSource1" editmode="InPlace" |
datakeynames="id"> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px" /> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px" /> |
</ExpandCollapseColumn> |
<Columns> |
<telerik:GridBoundColumn DataField="name" HeaderText="name" ReadOnly="True" |
SortExpression="name" UniqueName="name"> |
</telerik:GridBoundColumn> |
<telerik:GridCheckBoxColumn DataField="viewflag" DataType="System.Boolean" |
HeaderText="viewflag" SortExpression="viewflag" UniqueName="viewflag"> |
</telerik:GridCheckBoxColumn> |
<telerik:GridCheckBoxColumn DataField="insertflag" DataType="System.Boolean" |
HeaderText="insertflag" SortExpression="insertflag" UniqueName="insertflag"> |
</telerik:GridCheckBoxColumn> |
<telerik:GridCheckBoxColumn DataField="updateflag" DataType="System.Boolean" |
HeaderText="updateflag" SortExpression="updateflag" UniqueName="updateflag"> |
</telerik:GridCheckBoxColumn> |
<telerik:GridBoundColumn DataField="id" DataType="System.Int32" HeaderText="id" |
ReadOnly="True" SortExpression="id" UniqueName="id" Visible="False"> |
</telerik:GridBoundColumn> |
</Columns> |
<EditFormSettings> |
<FormTemplate> |
<telerik:RadUpload ID="RadUpload1" Runat="server"> |
</telerik:RadUpload> |
</FormTemplate> |
</EditFormSettings> |
<PagerStyle Mode="NextPrevAndNumeric" /> |
</MasterTableView> |
<ClientSettings> |
<selecting allowrowselect="True" /> |
<ClientEvents OnRowClick="RowClick" OnRowDblClick="RowDblClick" |
OnGridCreated="GridCreated" OnCommand="GridCommand" /> |
</ClientSettings> |
<FilterMenu EnableTheming="True"> |
<CollapseAnimation Duration="200" Type="OutQuint" /> |
</FilterMenu> |
<EditItemStyle Font-Bold="False" Font-Italic="False" |
Font-Overline="True" Font-Strikeout="False" Font-Underline="True" |
Wrap="True" /> |
</telerik:RadGrid> |
<asp:SqlDataSource ID="SqlDataSource1" runat="server" |
ConnectionString="<%$ ConnectionStrings:WorkFlowBaseConnectionString %>" |
SelectCommand="SELECT [name], [viewflag], [insertflag], [updateflag], [id] FROM [AccessRightstbl]"> |
</asp:SqlDataSource> |
</td> |
</tr> |
<tr> |
<td align="center" class="style2"> |
</td> |
</tr> |
<tr> |
<td align="center" class="style2"> |
<asp:UpdateProgress ID="UpdateProgress1" runat="server" |
AssociatedUpdatePanelID="UpdatePanel1" DisplayAfter="50" DynamicLayout="False"> |
<progresstemplate> |
<asp:Image ID="Image1" runat="server" ImageAlign="Middle" |
ImageUrl="~/App_Data/ajax-loader.gif" /> |
</progresstemplate> |
</asp:UpdateProgress> |
</td> |
</tr> |
</table> |
</fieldset> |
</ContentTemplate> |
</asp:UpdatePanel> |
<center> |
</center> |
</div> |
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableHistory="True"> |
</telerik:RadAjaxManager> |
<asp:Label ID="Label1" runat="server"></asp:Label> |
</form> |
</body> |
</html> |
Please advise;
Regards
Can you try setting AllowAutomaticUpdates/AllowAutomaticInserts property to false and see if the UpdateCommand event is getting fired.
Shinu
I am also facing the same issue as mentioned above. I tried the solution which you gave.
But it is not working, i.e., RadGrid1_UpdateCommand event is not getting fired.
Pls help
Regards
Ravi