This is a migrated thread and some comments may be shown as answers.

Update event is not triggering

6 Answers 215 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ash
Top achievements
Rank 1
Ash asked on 29 Nov 2010, 07:34 AM
Hi there,

I am using edit mode of type PopUp for doing update/insert using radgrid. i have used OnUpdateCommand="RadGrid1_UpdateCommand" event and defined it on code behind. but when i click on update button on popup form after modifying text fields, my update command event is not triggering and page shows like it got updated even though not. please anyone provide me solution. Delete works fine and delete event triggers as normal so problem is only with edit update event.  here is a part of my code,

<telerik:RadGrid ID="RadGrid1" AllowSorting="true" AllowPaging="true" 
PageSize="10" AutoGenerateColumns="false" runat="server" OnUpdateCommand="RadGrid1_UpdateCommand"   
OnDeleteCommand="RadGrid1_DeleteCommand" AllowAutomaticUpdates="false">
<MasterTableView DataKeyNames="i_UserId" 
EditMode="PopUp" AllowFilteringByColumn="True" Width="100%" TableLayout="Auto">
    <Columns>
        <telerik:GridBoundColumn 
Visible="False" UniqueName="FirstName"   HeaderText="First Name" DataField="s_FirstName" EditFormColumnIndex="0">                                            
        </telerik:GridBoundColumn
        <telerik:GridBoundColumn 
Visible="False" UniqueName="FirstName"   HeaderText="First Name"<BR> DataField="s_FirstName" 
                    EditFormColumnIndex="0"><BR>                                            
        </telerik:GridBoundColumn>
    <Columns/>
    <EditFormSettings 
ColumnNumber="3" CaptionFormatString="Edit details for User: {0}" 
        EditFormType="AutoGenerated" PopUpSettings-Width="70%" PopUpSettings-Modal="true" 
        EditColumn-Resizable="true"  PopUpSettings-ScrollBars="Auto" CaptionDataField="s_Name">                                                
    <FormTableItemStyleWrap="true"></FormTableItemStyle><BR>                                                
    <FormCaptionStyle CssClass="EditFormHeader" ForeColor="Blue"></FormCaptionStyle>                                               
    <FormMainTableStyle GridLines="Both" CellSpacing="0" CellPadding="3" Width="100%" HorizontalAlign="Justify"/>                                               
    <FormTableStyle GridLines="None" CellSpacing="2" CellPadding="2" CssClass="module" Height="110px"/>                                               
    <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>                                                
    <FormStyle Width="100%" BackColor="#eef2ea"></FormStyle><BR>                                                
    <EditColumn UpdateText="Update record" ButtonType="PushButton" UniqueName="EditCommandColumn1" CancelText="Cancel edit">                                                
    </EditColumn><BR>                                                
    <FormTableButtonRowStyle HorizontalAlign="Center" ForeColor="Blue" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>                                                                                      
    </EditFormSettings><BR>                                            
    <ExpandCollapseColumn ButtonType="ImageButton" Visible="False" UniqueName="ExpandColumn"><BR>                                                
    <HeaderStyle Width="15px"></HeaderStyle><BR>                                            
    </ExpandCollapseColumn>
  </MasterTableView> 
</
telerik:RadGrid>

And my code behind looks ike this,
      protected void RadGrid1_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;  
            int intUserId = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["i_UserId"].ToString());
            string strLastName = (editedItem["LastName"].Controls[0] as TextBox).Text;
            string strFirstName = (editedItem["FirstName"].Controls[0] as TextBox).Text; 
            ..........
            ..........
            ...........
            }

When debug this, control doesnt move to the above block when i click on upadte button.


Thanks & Regards,
Ash

6 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 29 Nov 2010, 06:05 PM
Hi Ash,

Make sure that you are using Advanced data-binding as opposed to Simple databinding. Most probably you are data-binding the grid somewhere in the way to the UpdateCommand, e.g. in the Page_Load event, and this leads to the incorrect behavior of the control.

Regards,
Tsvetoslav
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Ash
Top achievements
Rank 1
answered on 30 Nov 2010, 07:52 AM
Hi Tsvetoslav,

May be that is the only one reason left out. Bur Delete works absolutly fine. Can you please suggest me how to make use of advanced binding method in my code. please see attached code here.
I have tried different events liks,(RadGrid1_UpdateCommand(), RadGrid1_ItemUpdated(), btnUpdate_Click(), RadGrid1_ItemComman() and RadGrid1_ItemDataBound) to get triggered by update. But none of these seems to be working or triggering upon update button click. Upon clickong update button, RadGrid1_ItemDataBound triggers but it shows as it is not in edit mode while debugging(if() condition fails).  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using TSE.TIMS.DAL;
using TSE.TIMS.Utilities;
using Telerik.Web.Design;
using Telerik.Web.UI;
using System.Globalization;

namespace UserRegistartion
{
    public partial class Users_Lp : System.Web.UI.Page
    {
        /// <summary>
        /// Method is used to load the page and along with that rises appropriate callback events 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsCallback)
            {
                Page.Title = ConfigurationManager.AppSettings["App_Title"] + " ::My Page " + Page.Title;
                BindGrid();
              
            }
        }

        /// <summary>
        /// Method BindGrid used to Create the Object for the User Class and Supplies data for binding
        /// </summary>
        public void BindGrid()
        {           
            try
            {
                DALFactory objFactory = new DALFactory();
                Users objUser = (Users)objFactory.CreateDALObject(DALObjectType.Users);
                objUser.Query = "";
                DataSet dsUsers = objUser.ReadDataset();
                RadGrid1.DataSource = dsUsers.Tables[0];            
                RadGrid1.DataBind();
                objFactory = null;
                dsUsers = null;
                objUser = null;
            }
            catch (Exception ex)
            {
                //CreateLogFiles Err = new CreateLogFiles();
                //Err.Title = Page.Title;
                //string strErrMessage = ex.Message + "Parameters: Parameters=" + intPageNumber + ", Page size=" + Convert.ToInt32(ConfigurationManager.AppSettings["List_Fetch_Size"].Trim()) + ", CreatedBy=" + Convert.ToInt32(Session["i_UloginID"].ToString());
                //Err.ErrorLog(Err.Title, Server.MapPath("~/Logs/ErrorLog"), strErrMessage);
                //strRetValue = "Error";
            }
        }

        protected void fnAddUser(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }

        /// <summary>
        /// Method RadGrid1_DeleteCommand used to Delete user selected row
        /// </summary>

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
   
            GridDataItem item = (GridDataItem)e.Item;      
            int intUserId = Convert.ToInt32(item.OwnerTableView.DataKeyValues[item.ItemIndex]["i_UserId"].ToString());
        
             /// Assign values to Users properties.
            Users objUser = new Users()
            {   
                UserId = intUserId
            };

            try
            {
                objUser.Delete();
                objUser = null;
                BindGrid();
            }
            catch (Exception ex)
            {
            }  
        }

 

 /// <summary>
        /// Method RadGrid1_UpdateCommand used to Update user selected row
        /// </summary>
        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
           
            }
            if (e.CommandName == RadGrid.UpdateCommandName)
            {

                GridEditableItem editedItem = e.Item as GridEditableItem;
                int intUserId = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["i_UserId"].ToString());
                string strLastName = (editedItem["LastName"].Controls[0] as TextBox).Text;
                string strFirstName = (editedItem["FirstName"].Controls[0] as TextBox).Text;
            }
        }

        protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                //DisplayMessage(true, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
               // DisplayMessage(false, "Employee " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"] + " updated");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            /// Assign values to Users properties.
            Users objUser = new Users()
            {
                //FirstName = txtFirstName.Text.Trim(),
                //LastName = txtLastName.Text.Trim(),
                //OrganizationName = txtOrganization.Text.Trim(),
                //Address = txtAddress.Text.Trim(),
                //PhoneNumber = txtPhoneNumber.Text.Trim(),
                //MobileNumber = txtMobileNumber.Text.Trim(),
                //EmailId = txtEmailId.Text.Trim(),
                //LoginId = txtLoginId.Text.Trim(),
                //Password = txtPassword.Text.Trim(),
                //PurposeOfUse = txtPurposeOfUse.Text.Trim(),
                Enable = true,
                VerifyEmail = true,
                VerifyMobile = true
            };

        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.UpdateCommandName) //"Add new" button clicked
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;

            }
          
        }

 

        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.IsInEditMode)
            {
                GridDataItem dataItem = (GridDataItem)e.Item;
                (dataItem["s_MobileNumber"].Controls[0] as TextBox).Attributes.Add("onBlur", "getValue('" + (dataItem["s_MobileNumber"].Controls[0] as TextBox).ClientID + "');");

            }
        }
 
     }
  }




Please see below for aspx page,

<telerik:RadGrid ID="RadGrid1" AllowSorting="true" AllowPaging="true"
    PageSize="10" AutoGenerateColumns="false" runat="server"
      OnDeleteCommand="RadGrid1_DeleteCommand"
    OnUpdateCommand="RadGrid1_UpdateCommand" OnItemCommand="RadGrid1_ItemCommand"
    onitemdatabound="RadGrid1_ItemDataBound" onitemupdated="RadGrid1_ItemUpdated" >
    <ClientSettings>
 <ClientEvents OnPopUpShowing="PopUpShowing" />
 <Selecting AllowRowSelect="true"/>
    </ClientSettings>
    <MasterTableView DataKeyNames="i_UserId" EditMode="PopUp"  AllowFilteringByColumn="True" Width="100%" 
 TableLayout="Auto">
      <Columns>  
  <telerik:GridBoundColumn DataField="s_Name" HeaderText="User Name" ReadOnly="true" AutoPostBackOnFilter="true"  CurrentFilterFunction="Contains"
                                                  ShowFilterIcon="false"/>
  <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" HeaderStyle-Width="13px" >
    <HeaderStyle Width="13px"></HeaderStyle>
        <ItemStyle CssClass="MyImageButton" />
       </telerik:GridEditCommandColumn>
      <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow" HeaderStyle-Width="13px"
    ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
    UniqueName="DeleteColumn">
    <HeaderStyle Width="13px"></HeaderStyle>

    <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"/>                                       
       </telerik:GridButtonColumn>
  </Columns>
  
   <EditFormSettings ColumnNumber="3" CaptionFormatString="Edit details for User: {0}" EditFormType="Template" PopUpSettings-Width="1000px"
   PopUpSettings-Height="400px" PopUpSettings-Modal="true" PopUpSettings-ScrollBars="Auto" CaptionDataField="i_UserId">
      <EditColumn UniqueName="EditColumn"></EditColumn>
     <FormTemplate>
       <table width="99%" cellspacing="1" cellpadding="1">
   <tr>
       <td class="style6" align="left" valign="top" style="white-space: nowrap;">
    <asp:Label ID="lblFirstName" runat="server" Text="First Name"></asp:Label>

       </td>
       <td class="style1" valign="top">
    <telerik:RadTextBox ID="txtFirstName" Text='<%# Bind("s_FirstName") %>' runat="server" Width="200" TabIndex="1">
    </telerik:RadTextBox>
      </td>
        </tr>
       </table>
         <asp:Button ID="Button1" CommandName="Update"  Text="Update" runat="server"
       OnClick="btnUpdate_Click" ViewStateMode="Disabled"/>
         <asp:Button ID="Button2" CommandName="Cancel"
   Text="Cancel" runat="server" />
     </FormTemplate>
 </EditFormSettings>
     </MasterTableView>
     <HeaderContextMenu EnableImageSprites="True" CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
</telerik:RadGrid>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>



Regards,
Ash




0
Princy
Top achievements
Rank 2
answered on 30 Nov 2010, 01:51 PM
Hello Ash,

Attach 'OnNeedDataSource' event to RadGrid and in event handler, you should set the data source for the RadGrid. RadGrid calls this event each time it needs a data source and hence no need of calling DataBind() explicitly.

For more information on this please take a look at the following documentation and Demo.
Advanced Data-binding (using NeedDataSource event)
Grid / Advanced Data Binding

Thanks,
Princy.
0
Ash
Top achievements
Rank 1
answered on 02 Dec 2010, 09:00 AM
Hello Princy,

Thanks for your suggestions. But it got worked out without using needdata source also. Problem was with view state mode.


Anybody can help me in getting a updated value of rad textbox using javascript for client side validation purpose. Here is my aspx code. i am getting exception as object expected in the 2nd line of java script.

function fnUpdate() {
var strAddress = document.getElementById("txtAddress").value;
alert("Hi!");
return true;
}

<FormTemplate>
<telerik:RadTextBox ID="txtAddress"  Text='<%# Bind("s_Address") %>' runat="server" Width="250"
TextMode="MultiLine" Height="100px" TabIndex="8">
</telerik:RadTextBox>

 <asp:Button ID="btnUpdateUser" CommandName="Update"  Text="Update" runat="server" ViewStateMode="Disabled"
          OnClientClick="fnUpdate()" />
 <asp:Button ID="btnCancelUser" CommandName="Cancel" Text="Cancel" runat="server" />
</FormTemplate>

  

Thanks & Regards,

Ash
0
Princy
Top achievements
Rank 2
answered on 02 Dec 2010, 11:00 AM
Hello Ash,

One easy method is using one HiddenField to store the Client ID of RadTextBox inside FormTemplate. Then in client event access the RadTextBox using this HiddenField value. Check out the following sample code.

ASPX:
<asp:HiddenField ID="HiddenField1" runat="server" />

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
        {
            GridEditFormItem editItem = (GridEditFormItem)e.Item;
            RadTextBox txtBox = (RadTextBox)editItem.FindControl("txtAddress");
            HiddenField1.Value = txtBox.ClientID; // storing Id in HiddenField
        }
    }

Java Script:
<script type="text/javascript">
    function fnUpdate() {
        var strAddress = $find(document.getElementById('HiddenField1').value);// accessing RadTextBox using HiddenField value
        . . . . .
    }
</script>

Thanks,
Princy.
0
Ash
Top achievements
Rank 1
answered on 04 Dec 2010, 06:59 AM
Thanks a lot princy.
Tags
Grid
Asked by
Ash
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Ash
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or