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

[Solved] Edit form and filtered view

6 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yen
Top achievements
Rank 1
Yen asked on 15 May 2009, 08:47 PM
Dear Telerik,

I am using build 2009.1.311.35.  I have a RadGrid with a UserControl edit form.  The edit form is a bit unconventional as it manually performs database updates instead of invoking the RadGrid's "Update" command.  For us, it is desirable to keep the edit form open as it does other things in addition to updating an item.  To close the edit form, I have a linkbutton with the "Cancel" command.

The RadGrid contains filters so that at any given time only filtered items are shown.  When the user makes certain changes to an item's attributes in the edit form, the RadGrid's Rebind() method is called.  If the item's attributes no longer satisfy the filter's criteria, it is no longer shown.  The edit form is now automatically opened for the next item in the view.  In a sense, this behavior is desirable as it is convenient for a user to make changes to multiple items without having to manually edit each one.

However, a problem is that when the edit form is automatically opened, the OnItemCommand or OnEditCommand events are NOT fired.  We must rely on these events to perform initialization when a new item is edited.  Unfortunately we cannot use the UserControl's DataBinding or PreRender events as these also get invoked when the UserControl does postbacks (yes, we're probably using the UserControl Edit Form in ways you didn't intend :)

Ideally for us, the edit form should not be open automatically once the RadGrid is re-bound.  Do you have any suggestions or alternatives? 

Any help is greatly appreciated. Thanks,

Yen

6 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 19 May 2009, 06:20 AM
Hello Yen,

Are you using any extra code to keep the user control open? Or perhaps it stays open after an update operation? How is the grid control populated with data? Based on this information, I cannot determine the cause of the issue.

Kind regards,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Yen
Top achievements
Rank 1
answered on 20 May 2009, 07:01 PM
Hello Yavor,

Thank you for your reply. To answer your questions:

- Are you using any extra code to keep the user control open?
No.  To close the usercontrol, the user is required to click a LinkButton, which has the "Cancel" command.

- Or perhaps it stays open after an update operation?
The usercontrol does its own update operation independently from the RadGrid.

- How is the grid control populated with data?
Via the normal ASP.NET data source controls.

It is difficult to describe the exact behavior.  I created a simple test application using the Northwind database to illustrate the issue.  Please see the 2 files pasted below .

To see the behavior, select a value for the TitleOfCourtesy filter, say "Ms.", which reduces the number of rows to just a few.  Edit one of the row and change Title to a different value.  Note that the RadGrid's ItemCommand event fires.  The RadGrid rebinds and the updated row disappears.  But the usercontrol is still open for a new row.   This is acceptable; however, the RadGrid's ItemCommand event does not fire for the new row.  This is an issue because we need to perform some initialization for each new row.  I'm interested in finding out whether there's another event besides ItemCommand that always get fired when a new row is opened; if not, could you suggest other alternatives? 

Thanks.

<!-- RadGrid.aspx --> 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<%@ Page Language="C#" Inherits="System.Web.UI.Page" %> 
 
<!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 runat="server">  
    <title>Repro case</title> 
</head> 
<body> 
    <script language="CS" runat="server">  
 
        protected void OnGridItemCommand(object source, GridCommandEventArgs e)  
        {  
            // This event handler is not triggered   
        }  
          
    </script> 
      
 
    <form id="form1" runat="server">  
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">  
    </telerik:RadScriptManager> 
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None" OnItemCommand="OnGridItemCommand">  
        <MasterTableView AutoGenerateColumns="False" CellSpacing="-1" DataSourceID="SqlDataSource1" 
            DataKeyNames="EmployeeID" AllowFilteringByColumn="true">  
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
            <telerik:GridEditCommandColumn EditText="Edit"></telerik:GridEditCommandColumn> 
                <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32" HeaderText="EmployeeID" 
                    ReadOnly="True" SortExpression="EmployeeID" UniqueName="EmployeeID" AllowFiltering="false">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" SortExpression="LastName" 
                    UniqueName="LastName" AllowFiltering="false">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" 
                    UniqueName="FirstName" AllowFiltering="false">  
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy" 
                    SortExpression="TitleOfCourtesy" UniqueName="TitleOfCourtesy">  
                    <FilterTemplate> 
                        <telerik:RadComboBox runat="server" ID="ddlFilter" OnClientSelectedIndexChanged="OnFilterByTitle" 
                            SelectedValue='<%#  ((GridItem)Container).OwnerTableView.GetColumn("TitleOfCourtesy").CurrentFilterValue  %>'>  
                            <Items> 
                                <telerik:RadComboBoxItem Text="All" Value="All" /> 
                                <telerik:RadComboBoxItem Text="Dr." Value="Dr." /> 
                                <telerik:RadComboBoxItem Text="Mr." Value="Mr." /> 
                                <telerik:RadComboBoxItem Text="Ms." Value="Ms." /> 
                                <telerik:RadComboBoxItem Text="Mrs." Value="Mrs." /> 
                            </Items> 
                        </telerik:RadComboBox> 
                        <telerik:RadScriptBlock ID="ScriptBlock1" runat="server">  
                            <script type="text/javascript">  
                                function OnFilterByTitle(sender, args) {  
                                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");  
                                    var selectedValue = args.get_item().get_value();  
 
                                    if (selectedValue == "All") {  
                                        tableView.filter("TitleOfCourtesy", "", Telerik.Web.UI.GridFilterFunction.NoFilter);  
                                    }  
                                    else {  
                                        tableView.filter("TitleOfCourtesy", selectedValue, Telerik.Web.UI.GridFilterFunction.EqualTo);  
                                    }  
                                }  
                            </script> 
                        </telerik:RadScriptBlock> 
                    </FilterTemplate> 
                </telerik:GridBoundColumn> 
            </Columns> 
            <EditFormSettings EditFormType="WebUserControl" UserControlName="EditForm.ascx">  
                <EditColumn UniqueName="EditCommandColumn1">  
                </EditColumn> 
            </EditFormSettings> 
        </MasterTableView> 
    </telerik:RadGrid> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Server=localhost;Database=Northwind;Trusted_Connection=True;" 
        SelectCommand="select EmployeeID, LastName, FirstName, TitleOfCourtesy from dbo.Employees">  
    </asp:SqlDataSource> 
    </form> 
</body> 
</html> 
 

<!-- EditForm.ascx --> 
<
%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<%@ Control Language="C#" CodeBehind="EditForm.ascx.cs" Inherits="System.Web.UI.UserControl" %> 
<%@ Import Namespace="System" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 
<%@ Import Namespace="Telerik.Web.UI" %> 
 
<script language="CS" runat="server">  
        
    private object _dataItem = null;  
 
    protected object DataItem  
    {  
        get { return this._dataItem; }  
        set { this._dataItem = value; }  
    }  
 
    protected void OnEdit(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)  
    {  
        using (SqlConnection conn = new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"))  
        {  
            string sql = "update Employees set TitleOfCourtesy=@title where employeeid=@id";  
            using (SqlCommand cmd = new SqlCommand(sql, conn))  
            {  
                SqlParameter[] parameters = new SqlParameter[2];  
                parameters[0] = new SqlParameter("@id", Convert.ToInt32(lblEmployeeID.Text));  
                parameters[1] = new SqlParameter("@title", ddlTitle.SelectedValue);  
                cmd.Parameters.AddRange(parameters);  
 
                conn.Open();  
                cmd.ExecuteNonQuery();  
            }  
        }  
        (this.Parent.NamingContainer.NamingContainer as GridTableView).Rebind();  
    }  
 
</script> 
 
<asp:LinkButton ID="lnkCancel" Text="Close" runat="server" CommandName="Cancel" /> 
<br /> 
<table> 
    <tr> 
        <td> 
            EmployeeID:  
        </td> 
        <td> 
            <asp:Label ID="lblEmployeeID" runat="server" Text='<%# Eval("EmployeeID") %>'></asp:Label> 
        </td> 
    </tr> 
    <tr> 
        <td> 
            Name:  
        </td> 
        <td> 
            <asp:Label ID="lblName" runat="server" Text='<%# Eval("FirstName") + " " + Eval("LastName") %>'></asp:Label> 
        </td> 
    </tr> 
    <tr> 
        <td> 
            Title:  
        </td> 
        <td> 
            <telerik:RadComboBox ID="ddlTitle" runat="server" SelectedValue='<%# Eval("TitleOfCourtesy") %>' 
                OnSelectedIndexChanged="OnEdit" AutoPostBack="true">  
                <items> 
                                    <telerik:RadComboBoxItem Text="Dr." Value="Dr." /> 
                                    <telerik:RadComboBoxItem Text="Mr." Value="Mr." /> 
                                    <telerik:RadComboBoxItem Text="Ms." Value="Ms." /> 
                                    <telerik:RadComboBoxItem Text="Mrs." Value="Mrs." /> 
                                </items> 
            </telerik:RadComboBox> 
        </td> 
    </tr> 
</table> 
 
0
Yavor
Telerik team
answered on 25 May 2009, 11:07 AM
Hello Yen,

Thank you for getting back to us.
I tested the setup, and the control behaved as expected.
The ItemComand of the grid was fired only when entering edit mode (edit command name), or when triggering another command.
The onEdit command was raised for the combo consistently. Attached to this message, is the code, which I used for testing. Please, take a look at it and let me know if this is the expected behavior, or if I am leaving something out.

Best wishes,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Yen
Top achievements
Rank 1
answered on 03 Jun 2009, 09:44 PM
Hi Yavor,

Thanks for your time.  In your test code, in the OnEdit event handler, you removed the code which updates the database when a new value is selected.  So you probably weren't able to observe the behavior I was describing.  With the update code intact, here's a scenario:
1- Select Ms. from the filter
2- Click Edit for Nancy Davolio
3- Under the title drop-down, select a different value (e.g., Dr.).  This should update the database. Note that Nancy Davolio disappears and the row immediately below (probably Janet Leverling) is now open.

That Nancy Davolio disappears is expected, since her title no longer matches the title selected in the filter.

What I'm interested in is how to detect when another row is automatically open in the user control, since the RadGrid's ItemCommand event doesn't fire in such a case.  Is it even possible or is another approach warranted? I looked at events for the user control, including DataBinding, Load and PreRender, but I can't use them because they fire whenever the user interacts with the user control, whereas I only want to detect when a different row is opened.  Please let me know if anything isn't clear.

Thanks.
0
Accepted
Georgi Krustev
Telerik team
answered on 09 Jun 2009, 10:45 AM
Hello Yen,

In order to understand whether another row is created in edit mode you need to wire the ItemCreated/ItemDataBound event and check if the row is GridEditableItem and if it is in edit mode. Here is a code snippet showing what you need to done in order to accomplish suggested task:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)  
        { 
           //you need to track whether a new item is in edit mode. 
        } 
    } 


Let me know if I am missing something.

Best wishes,
Georgi Krustev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Yen
Top achievements
Rank 1
answered on 09 Jun 2009, 10:58 PM
Hello Georgi,

The information you provided is definitely helpful.  We do have another workaround, but I think we can use your approach as well.  Thanks for your help.
Tags
Grid
Asked by
Yen
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Yen
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or