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

ListView Click twice to edit.

3 Answers 161 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Richard Maly
Top achievements
Rank 1
Richard Maly asked on 29 Jun 2010, 01:58 PM
Hi

I have a problem with the listview I have a close proximity to the demo, however when you click on the insert button you need to do so twice before the Insert Template is used.   Stepping through the code, the same routines are called, using the same branches etc.

<telerik:RadListView ID="RadListBox1" runat="server" OnNeedDataSource="RadListView1_NeedDataSource" 
                                        ItemPlaceholderID="ProductsHolder" OnItemCommand="RadListView1_ItemCommand" DataKeyNames="CountrySapInfoId"
                                        <LayoutTemplate> 
                                            <fieldset style="width: 700px;" id="FieldSet1"
                                                <legend>SAP Country Info</legend> 
                                                <asp:Panel ID="ProductsHolder" runat="server" /> 
                                                <div style="clear: both" /> 
                                                <div style="padding: 10px"
                                                    <asp:LinkButton ID="btnInitInsert" runat="server" Text="New Sap Country Info" CommandName="InitInsert" /> 
                                                </div> 
                                            </fieldset> 
                                        </LayoutTemplate> 
                                        <ItemTemplate> 
                                            <fieldset style="border-style: none; float: left; width: 300px;"
                                                <table cellpadding="0" cellspacing="0" width="100%" style="margin: 10px"
                                                    <tr> 
                                                        <td width="100px"
                                                            Company Code: 
                                                        </td> 
                                                        <td> 
                                                            <%#  Eval("SAPCompanyCode") %> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td> 
                                                            Sales Org: 
                                                        </td> 
                                                        <td> 
                                                            <%#  Eval("SAPSalesOrg") %> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td style="padding-top: 5px"
                                                            <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" /> 
                                                        </td> 
                                                    </tr> 
                                                </table> 
                                            </fieldset> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <fieldset style="border-style: none; float: left; width: 300px;"
                                                <table cellpadding="0" cellspacing="0" width="100%" style="margin: 10px"
                                                    <tr> 
                                                        <td> 
                                                            Company Code: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtCompanyCode" runat="server" Text='<%# Bind("SAPCompanyCode") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td> 
                                                            Sales Org: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtSalesOrg" runat="server" Text='<%# Bind("SAPSalesOrg") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td style="padding-top: 5px" colspan="2"
                                                            <asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" 
                                                                Width="70px" /> 
                                                            <asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" 
                                                                Width="70px" /> 
                                                            <asp:LinkButton ID="BtnCancel" runat="server" Text="Cancel" CommandName="Cancel" 
                                                                CausesValidation="false" Width="70px" /> 
                                                        </td> 
                                                    </tr> 
                                                </table> 
                                            </fioeldset> 
                                        </EditItemTemplate> 
                                        <InsertItemTemplate> 
                                            <fieldset style="border-style: none; float: left; width: 300px;"
                                                <table cellpadding="0" cellspacing="0" width="100%" style="margin: 10px"
                                                    <tr> 
                                                        <td width="50%"
                                                            Company Code: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtCompanyCode" runat="server" Text='<%# Bind("SAPCompanyCode") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td> 
                                                            Sales Org: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtSalesOrg" runat="server" Text='<%# Bind("SAPSalesOrg") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td style="padding-top: 5px" colspan="2"
                                                            <asp:LinkButton ID="btnInsert" runat="server" Text="Insert" CommandName="PerformInsert" 
                                                                Width="70px" /> 
                                                            <asp:LinkButton ID="BtnCancel" runat="server" Text="Cancel" CommandName="Cancel" 
                                                                CausesValidation="false" Width="70px" /> 
                                                        </td> 
                                                    </tr> 
                                                </table> 
                                            </fieldset> 
                                        </InsertItemTemplate> 
                                    </telerik:RadListView> 

and my code behind is

 protected void RadListView1_ItemCommand(object source, RadListViewCommandEventArgs e) 
    { 
        RadListView rlv = (source as RadListView); 
        if ((e.CommandName == RadListView.PerformInsertCommandName) || (e.CommandName == RadListView.UpdateCommandName) || (e.CommandName == RadListView.CancelCommandName)) 
        { 
            rlv.InsertItemPosition = RadListViewInsertItemPosition.None; 
        } 
        else if (e.CommandName == RadListView.InitInsertCommandName) 
        { 
            rlv.InsertItemPosition = RadListViewInsertItemPosition.LastItem; 
            //e.Canceled = true; 
        } 
 
        if ((e.CommandName == RadListView.PerformInsertCommandName) || (e.CommandName == RadListView.UpdateCommandName)) 
        { 
            SaveCountrySapInfo(e.ListViewItem); 
        } 
 
        if (e.CommandName == "Delete"
        { 
            DeleteCountrySapInfo(e.ListViewItem); 
        } 
    } 

Any ideas?

TIA
Richard


3 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 29 Jun 2010, 03:30 PM
Hello Richard,

Can you verify that you are not databinding RadListView control on every postback?
If you continue to experience difficulties please paste the entire page's markup and code behind.

Best wishes,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Richard Maly
Top achievements
Rank 1
answered on 29 Jun 2010, 03:46 PM
I am using needDataSource, so I do not think I am binding unneccesarily. Here is my entire source.  You will notice the control is embedded in a RADGrid. I am using Session to store the state of the objects

Many thanks

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/App_MasterPages/Oxygen.master" 
    CodeFile="Maintain_Country.aspx.cs" Inherits="Maintain_Country" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder_Main" runat="Server"
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
    </telerik:RadScriptManager> 
    <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server"
    </telerik:RadStyleSheetManager> 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" UpdatePanelsRenderMode="Block"
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="rg_Results"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="rg_Results" /> 
                    <telerik:AjaxUpdatedControl ControlID="lbl_error" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server"
        <Windows> 
            <telerik:RadWindow ID="RadWindow1" runat="server" Visible="true" Title="Audit Trail" 
                Width="800px" Height="400px" Modal="true"
            </telerik:RadWindow> 
        </Windows> 
    </telerik:RadWindowManager> 
    <asp:Button ID="Button2" runt="server" Text="Button" OnClick="Button2_Click" /> 
    <table width="100%"
        <tr> 
            <td align="left"
                <asp:Label ID="lblTitle" runat="server" CssClass="TitleLabel">Maintain Country</asp:Label> 
            </td> 
            <td align="right"
                <asp:Label ID="lbl_error" runat="server" CssClass="ErrorLabel"></asp:Label> 
                <asp:Label ID="lblUserMessage" runat="server" CssClass="UserMessageLabel"></asp:Label> 
            </td> 
        </tr> 
    </table> 
    <hr style="color: Navy" /> 
    <script type="text/javascript"
 
        function ShowDialog(empid) { 
            window.radopen("Audit_Search.aspx?tablename=Country&&tablekey=" + empid, "RadWindow1"); 
            return false; 
        } 
    </script> 
    <telerik:RadGrid ID="rg_Results" runat="server" AutoGenerateColumns="false" AllowPaging="True" 
        EnableAjaxSkinRendering="true" AllowFilteringByColumn="true" AllowSorting="True" 
        GridLines="None" ShowStatusBar="true" OnItemCommand="rg_Results_ItemCommand" 
        OnItemDataBound="rg_Results_ItemDataBound" OnPreRender="rg_Results_PreRender" 
        OnInsertCommand="rg_Results_InsertCommand" OnNeedDataSource="rg_Results_NeedDataSource" 
        OnUpdateCommand="rg_Results_UpdateCommand"
        <ExportSettings> 
            <Excel Format="ExcelML" /> 
        </ExportSettings> 
        <MasterTableView DataKeyNames="CountryID" EditMode="EditForms" AllowNaturalSort="true" 
            AllowFilteringByColumn="true" PageSize="10" PagerStyle-Mode="NextPrevAndNumeric" 
            CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add"
            <Columns> 
                <telerik:GridBoundColumn UniqueName="CountryID" DataField="CountryID" DataType="System.Int32" 
                    Visible="false" AllowFiltering="false" ItemStyle-Width="15%" HeaderText="Country Id"
                    <ItemStyle Width="15%"></ItemStyle> 
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn UniqueName="CountryGroupID" DataField="CountryGroupID" DataType="System.Int32" 
                    Visible="false" AllowFiltering="false" ItemStyle-Width="15%" HeaderText="Country Group Id"
                    <ItemStyle Width="15%"></ItemStyle> 
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn UniqueName="CountryName" DataField="CountryName" DataType="System.String" 
                    Visible="true" AllowFiltering="false" ItemStyle-Width="15%" HeaderText="Country Name"
                    <ItemStyle Width="15%"></ItemStyle> 
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn UniqueName="CountryCode" DataField="CountryCode" DataType="System.String" 
                    Visible="true" AllowFiltering="false" ItemStyle-Width="15%" HeaderText="Country Code"
                    <ItemStyle Width="15%"></ItemStyle> 
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn UniqueName="CountryGroupName" DataField="CountryGroupName" 
                    DataType="System.String" Visible="true" AllowFiltering="false" ItemStyle-Width="15%" 
                    HeaderText="Country Group Name"
                    <ItemStyle Width="15%"></ItemStyle> 
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn UniqueName="Currency" DataField="Currency" DataType="System.String" 
                    Visible="true" AllowFiltering="false" HeaderText="Currency"
                </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn AllowFiltering="false" ItemStyle-Width="5%"
                    <ItemTemplate> 
                        <asp:ImageButton ID="btn_edit" runat="server" ImageUrl="~/App_Images/Icons/Edit.gif" 
                            ToolTip="Edit" CommandName="Edit" Visible="True" /> 
                    </ItemTemplate> 
                    <ItemStyle Width="5%"></ItemStyle> 
                </telerik:GridTemplateColumn> 
                <telerik:GridTemplateColumn AllowFiltering="false" ItemStyle-Width="5%"
                    <ItemTemplate> 
                        <asp:ImageButton ID="btn_Audit" runat="server" ImageUrl="~/App_Images/Icons/Audit.gif" 
                            ToolTip="Get Audit for this record" CommandName="GetAuditTrail" Visible="True" 
                            Value="test" /> 
                    </ItemTemplate> 
                    <ItemStyle Width="5%"></ItemStyle> 
                </telerik:GridTemplateColumn> 
            </Columns> 
            <EditFormSettings EditFormType="Template" PopUpSettings-Width="500px"
                <FormTemplate> 
                    <div> 
                        <table> 
                            <tr> 
                                <td> 
                                    <label> 
                                        Country Name:</label> 
                                </td> 
                                <td> 
                                    <asp:TextBox ID="txt_CountryName" runat="server" CssClass="txt-input" Text='<%# Bind("CountryName") %>' 
                                        OnTextChanged="TextChanged"></asp:TextBox> 
                                    <asp:RequiredFieldValidator ID="req_desc" runat="server" ControlToValidate="txt_CountryName" 
                                        ErrorMessage="*"></asp:RequiredFieldValidator> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    <label> 
                                        Country Code:</label> 
                                </td> 
                                <td> 
                                    <asp:TextBox ID="txt_CountryCode" runat="server" CssClass="txt-input" Text='<%# Bind("CountryCode") %>' 
                                        OnTextChanged="TextChanged"></asp:TextBox> 
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_CountryCode" 
                                        ErrorMessage="*"></asp:RequiredFieldValidator> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    <label> 
                                        Country Group Name:</label> 
                                </td> 
                                <td> 
                                    <asp:DropDownList ID="txt_CountryGroupId" runat="server" CssClass="txt-input" SelectedValue='<%# checkCountryGroupName(Eval("CountryGroupName")) %>' 
                                        OnSelectedIndexChanged="TextChanged" AppendDataBoundItems="true" DataSource='<%# GetCountry() %>'
                                    </asp:DropDownList> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    <label> 
                                        Currency:</label> 
                                </td> 
                                <td> 
                                    <asp:TextBox ID="txt_Currency" runat="server" CssClass="txt-input" Text='<%# Bind("Currency") %>' 
                                        OnTextChanged="TextChanged"></asp:TextBox> 
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt_Currency" 
                                        ErrorMessage="*"></asp:RequiredFieldValidator> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td colspan="2"
                                    <telerik:RadListView ID="RadListBox1" runat="server" OnNeedDataSource="RadListView1_NeedDataSource" 
                                        ItemPlaceholderID="ProductsHolder" OnItemCommand="RadListView1_ItemCommand" DataKeyNames="CountrySapInfoId"
                                        <LayoutTemplate> 
                                            <fieldset style="width: 700px;" id="FieldSet1"
                                                <legend>SAP Country Info</legend> 
                                                <asp:Panel ID="ProductsHolder" runat="server" /> 
                                                <div style="clear: both" /> 
                                                <div style="padding: 10px"
                                                    <asp:LinkButton ID="btnInitInsert" runat="server" Text="New Sap Country Info" CommandName="InitInsert" /> 
                                                </div> 
                                            </fieldset> 
                                        </LayoutTemplate> 
                                        <ItemTemplate> 
                                            <fieldset style="border-style: none; float: left; width: 300px;"
                                                <table cellpadding="0" cellspacing="0" width="100%" style="margin: 10px"
                                                    <tr> 
                                                        <td width="100px"
                                                            Company Code: 
                                                        </td> 
                                                        <td> 
                                                            <%#  Eval("SAPCompanyCode") %> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td> 
                                                            Sales Org: 
                                                        </td> 
                                                        <td> 
                                                            <%#  Eval("SAPSalesOrg") %> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td style="padding-top: 5px"
                                                            <asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" /> 
                                                        </td> 
                                                    </tr> 
                                                </table> 
                                            </fieldset> 
                                        </ItemTemplate> 
                                        <EditItemTemplate> 
                                            <fieldset style="border-style: none; float: left; width: 300px;"
                                                <table cellpadding="0" cellspacing="0" width="100%" style="margin: 10px"
                                                    <tr> 
                                                        <td> 
                                                            Company Code: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtCompanyCode" runat="server" Text='<%# Bind("SAPCompanyCode") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td> 
                                                            Sales Org: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtSalesOrg" runat="server" Text='<%# Bind("SAPSalesOrg") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td style="padding-top: 5px" colspan="2"
                                                            <asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" 
                                                                Width="70px" /> 
                                                            <asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" 
                                                                Width="70px" /> 
                                                            <asp:LinkButton ID="BtnCancel" runat="server" Text="Cancel" CommandName="Cancel" 
                                                                CausesValidation="false" Width="70px" /> 
                                                        </td> 
                                                    </tr> 
                                                </table> 
                                            </fioeldset> 
                                        </EditItemTemplate> 
                                        <InsertItemTemplate> 
                                            <fieldset style="border-style: none; float: left; width: 300px;"
                                                <table cellpadding="0" cellspacing="0" width="100%" style="margin: 10px"
                                                    <tr> 
                                                        <td width="50%"
                                                            Company Code: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtCompanyCode" runat="server" Text='<%# Bind("SAPCompanyCode") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td> 
                                                            Sales Org: 
                                                        </td> 
                                                        <td> 
                                                            <asp:TextBox ID="txtSalesOrg" runat="server" Text='<%# Bind("SAPSalesOrg") %>' /> 
                                                        </td> 
                                                    </tr> 
                                                    <tr> 
                                                        <td style="padding-top: 5px" colspan="2"
                                                            <asp:LinkButton ID="btnInsert" runat="server" Text="Insert" CommandName="PerformInsert" 
                                                                Width="70px" /> 
                                                            <asp:LinkButton ID="BtnCancel" runat="server" Text="Cancel" CommandName="Cancel" 
                                                                CausesValidation="false" Width="70px" /> 
                                                        </td> 
                                                    </tr> 
                                                </table> 
                                            </fieldset> 
                                        </InsertItemTemplate> 
                                    </telerik:RadListView> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td> 
                                    <asp:Button ID="btn_add" Text='<%# (Container is GridEditFormInsertItem) ? "Add" : "Update" %>' 
                                        runat="server" CssClass="btn" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'
                                    </asp:Button> 
                                </td> 
                                <td> 
                                    <asp:Button ID="btn_cancel" runat="server" CssClass="btn" CommandName="Cancel" Text="Cancel" 
                                        CausesValidation="false" /> 
                                </td> 
                            </tr> 
                        </table> 
                    </div> 
                </FormTemplate> 
                <PopUpSettings Width="500px"></PopUpSettings> 
            </EditFormSettings> 
            <NoRecordsTemplate> 
                No records found.</NoRecordsTemplate> 
            <CommandItemSettings AddNewRecordText="Add a new Country"></CommandItemSettings> 
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
        </MasterTableView> 
        <ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True"
            <Selecting AllowRowSelect="True" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
</asp:Content> 
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder_Footer" runat="Server"
</asp:Content> 


using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Oxygen; 
using Oxygen.App_DataConnections; 
using Telerik.Web.UI; 
 
public partial class Maintain_Country : System.Web.UI.Page 
    const string _MaintainCountryId = "MaintainCountryId"
    const string _MaintainCountryobjCountry = "MaintainCountryobjCountry"
    const string _MaintainCountryIsDirty = "MaintainCountryIsDirty"
 
    public Array GetCountry() 
    { 
        ArrayList country = new ArrayList(); 
 
        foreach (objCountryGroup obj in CountryGroups) 
        { 
            country.Add(obj.CountryGroupCode); 
        } 
 
        return country.ToArray(); 
    } 
 
    private bool isDirty 
    { 
        get 
        { 
            if (Session[_MaintainCountryIsDirty] != null
            { 
                return bool.Parse(Session[_MaintainCountryIsDirty].ToString()); 
            } 
            else 
            { 
                Session[_MaintainCountryIsDirty] = false
                return false
            } 
        } 
        set 
        { 
            Session[_MaintainCountryIsDirty] = value.ToString(); 
        } 
    } 
 
    private int id 
    { 
        get 
        { 
            if (Session[_MaintainCountryId] != null
            { 
                return int.Parse(Session[_MaintainCountryId].ToString()); 
            } 
            else 
            { 
                id = 0; 
                return 0; 
            } 
        } 
        set 
        { 
            Session[_MaintainCountryId] = value.ToString(); 
            //this has changed clear it out. could get the new one here! 
            Session[_MaintainCountryobjCountry] = null
        } 
    } 
 
    private objCountry country 
    { 
        get 
        { 
            if (Session[_MaintainCountryobjCountry] != null
            { 
                return (Session[_MaintainCountryobjCountry] as objCountry); 
            } 
            else 
            { 
                return new objCountry(); 
            } 
        } 
        set 
        { 
            Session[_MaintainCountryobjCountry] = value; 
        } 
    } 
 
    protected IList<objCountryGroup> CountryGroups 
    { 
        get 
        { 
            IList<objCountryGroup> obj = null
 
            try 
            { 
                obj = (IList<objCountryGroup>)Session["CountryGroups"]; 
                if (obj == null
                { 
                    AMRefData rd = new AMRefData(); 
                    obj = rd.getCountryGroups(); 
                    if (obj != null
                    { 
                        objCountryGroup objcountryGroup = new objCountryGroup(); 
                        objcountryGroup.CountryGroupCode = "Please Select ..."
                        objcountryGroup.CountryGroupID = 0; 
 
                        obj.Insert(0, objcountryGroup); 
                        Session["CountryGroups"] = obj; 
                    } 
                    else 
                    { 
                        obj = new List<objCountryGroup>(); 
                    } 
                } 
                Session["CountryGroups"] = obj; 
            } 
            catch (Exception ex) 
            { 
                Session["CountryGroups"] = null
                ErrorLogging.log4NetError(ex.Message, this.GetType()); 
                DisplayMessage(true, ex.Message); 
            } 
            return obj; 
        } 
 
        set 
        { 
            Session["CountryGroups"] = value; 
        } 
    } 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        try 
        { 
            if (!IsPostBack) 
            { 
                isDirty = false
                id = 0; 
                country = null
 
                AMRefData rd = new AMRefData(); 
                // objPersonAddress al = new objPersonAddress(); 
                List<objCountry> ar = new List<objCountry>(); 
                ar = rd.GetCountryDetails(); 
                foreach (Control ctl in rg_Results.Controls) 
                { 
                    if (ctl.ID == "txt_CountryGroupId"
                    { 
                        DropDownList ddl = (DropDownList)ctl; 
                        ddl.DataSource = ar; 
                        ddl.DataBind(); 
                    } 
                } 
            } 
 
            if (country == null
            { 
                country = new objCountry(); 
            } 
        } 
        catch (Exception ex) 
        { 
            ErrorLogging.log4NetError(ex.Message, this.GetType()); 
            DisplayMessage(true, ex.Message); 
        } 
    } 
 
    protected void TextChanged(object sender, EventArgs e) 
    { 
        isDirty = true
    } 
    #region " Grid Events " 
 
    protected void rg_Results_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            Telerik.Web.UI.DataKey key = rg_Results.MasterTableView.DataKeyValues[e.Item.ItemIndex]; 
            int id = (int)key["CountryID"]; 
            ((ImageButton)e.Item.FindControl("btn_Audit")).Attributes.Add("onclick""return ShowDialog('" + id + "')"); 
        } 
    } 
 
    protected String checkCountryGroupName(object CountryGroupName) 
    { 
        try 
        { 
            if (CountryGroupName == null || CountryGroupName.ToString() == ""
            { 
                return "Please Select ..."
            } 
            else 
            { 
                return CountryGroupName.ToString(); 
            } 
        } 
        catch 
        { 
            return null
        } 
    } 
 
    protected void RadListView1_ItemCommand(object source, RadListViewCommandEventArgs e) 
    { 
        RadListView rlv = (source as RadListView); 
        if ((e.CommandName == RadListView.PerformInsertCommandName) || (e.CommandName == RadListView.UpdateCommandName) || (e.CommandName == RadListView.CancelCommandName)) 
        { 
            rlv.InsertItemPosition = RadListViewInsertItemPosition.None; 
        } 
        else if (e.CommandName == RadListView.InitInsertCommandName) 
        { 
            rlv.InsertItemPosition = RadListViewInsertItemPosition.LastItem; 
            //e.Canceled = true; 
        } 
 
        if ((e.CommandName == RadListView.PerformInsertCommandName) || (e.CommandName == RadListView.UpdateCommandName)) 
        { 
            SaveCountrySapInfo(e.ListViewItem); 
        } 
 
        if (e.CommandName == "Delete"
        { 
            DeleteCountrySapInfo(e.ListViewItem); 
        } 
    } 
 
    private void DeleteCountrySapInfo(RadListViewItem radListViewItem) 
    { 
        //probably need to pop up an are you sure message 
 
        var am = new AMPerson(); 
        var id = int.Parse(((RadListViewEditableItem)radListViewItem).GetDataKeyValue("CountrySapInfoId").ToString()); 
        am.DeleteCountrySapInfo(id, Convert.ToInt32(Session["UserId"])); 
    } 
 
    private void SaveCountrySapInfo(RadListViewItem radListViewItem) 
    { 
        Hashtable newValues = new Hashtable(); 
        int CountrySapInfoId = 0; 
 
        RadListViewEditableItem editedItem = (RadListViewEditableItem)radListViewItem; 
 
        editedItem.ExtractValues(newValues); 
        object strCountrySapInfoId = null
 
        try 
        { 
            strCountrySapInfoId = editedItem.GetDataKeyValue("CountrySapInfoId"); 
        } 
        catch 
        { 
            //okay I know this is bad, but on insert the GetDataKeyValue is null, and rather than null being returned and Object Not Set error is. 
        } 
 
        if (strCountrySapInfoId != null
        { 
            int.TryParse(strCountrySapInfoId.ToString(), out CountrySapInfoId); 
            CountrySapInfo obj = country.CountrySapInfo.Where(p => p.CountrySAPInfoId == CountrySapInfoId).FirstOrDefault(); 
 
            obj.SAPCompanyCode = newValues["SAPCompanyCode"].ToString(); 
            obj.SAPSalesOrg = newValues["SAPSalesOrg"].ToString(); 
        } 
        else 
        { 
            var obj = new CountrySapInfo() 
            { 
                CountryId = id, 
                CountrySAPInfoId = CountrySapInfoId, 
                SAPCompanyCode = newValues["SAPCompanyCode"].ToString(), 
                SAPSalesOrg = newValues["SAPSalesOrg"].ToString() 
            }; 
 
            if (country.CountrySapInfo == null
            { 
                country.CountrySapInfo = new List<CountrySapInfo>() { obj }; 
            } 
            else 
            { 
                country.CountrySapInfo.Add(obj); 
            } 
        } 
 
        isDirty = true
    } 
 
    protected void RadListView1_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e) 
    { 
        var rlv = (sender as RadListView); 
        //var am = new AMPerson(); 
        rlv.DataSource = country.CountrySapInfo;//am.GetCountrySapInfo(id); 
    } 
 
    protected void rg_Results_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        //int id = 0; 
        if (e.Item.ItemIndex != -1)  //"add" does not have a row number. 
        { 
            if (!isDirty) 
            { 
                Telerik.Web.UI.DataKey key = rg_Results.MasterTableView.DataKeyValues[e.Item.ItemIndex]; 
                id = (int)key["CountryID"]; 
 
                AMPerson am = new AMPerson(); 
                country = am.GetCountryById(id); 
            } 
        } 
        else if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked 
        { 
            e.Canceled = true
 
            country = new objCountry(); 
            country.CountrySapInfo = new List<CountrySapInfo>(); 
 
            id = 0; 
            country = new objCountry(); 
            e.Item.OwnerTableView.InsertItem(country); 
        } 
 
        if (e.CommandName == "Retire"
        { 
            if (Lock(id, 1) == true
            { 
                //Telerik.Web.UI.DataKey key = rg_Results.MasterTableView.DataKeyValues[e.Item.ItemIndex]; 
                if (SaveData(id, 1, (GridDataItem)e.Item) == true
                { 
                    rg_Results.Rebind(); 
                } 
            } 
            else 
            { 
                DisplayMessage(false"Item locked"); 
                e.Canceled = true
            } 
        } 
        else if (e.CommandName == RadGrid.RebindGridCommandName && e.Item.OwnerTableView.IsItemInserted) 
        { 
            e.Canceled = true
        } 
        else if (e.CommandName == RadGrid.PerformInsertCommandName || e.CommandName == RadGrid.UpdateCommandName) 
        { 
            Lock(id, 0); 
        } 
        else  //capture update. 
        { 
            if (Lock(id, 1) == false
            { 
                DisplayMessage(false"Item locked"); 
                e.Canceled = true
            } 
        } 
    } 
 
    protected void btnInitInsert_Click(object sender, System.EventArgs e) 
    { 
        // 
    } 
 
    protected void rg_Results_PreRender(object sender, EventArgs e) 
    { 
        if (!Page.IsPostBack) 
        { 
            rg_Results.Rebind(); 
        } 
    } 
 
    protected void rg_Results_InsertCommand(object source, GridCommandEventArgs e) 
    { 
        if (SaveData(0, 0, e) == false
        { 
            e.Canceled = true
        } 
    } 
 
    protected void rg_Results_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        populateGrid(); 
    } 
 
    protected void rg_Results_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        Telerik.Web.UI.DataKey key = rg_Results.MasterTableView.DataKeyValues[e.Item.ItemIndex]; 
        if (SaveData((int)key["CountryID"], -1, e) == false
        { 
            e.Canceled = true
        } 
    } 
    #endregion " Grid Events " 
 
    private bool Lock(int id, int createLock) 
    { 
        bool result = true
        //check to see if the row is locked 
        AMLocking lockingam = new AMLocking(); 
        List<objLock> bllocks = lockingam.RowLocking(Session["UserName"].ToString(), "Country", id, createLock); 
        //lockingam.RowLocking("", "Users", (int)key["UserID"], 1); 
 
        if (createLock == 1) 
        { 
            foreach (objLock bllock in bllocks) 
            { 
                if (bllock.locked == 1 && bllock.UserName != Session["UserName"].ToString()) 
                { 
                    result = false
                } 
            } 
        } 
 
        return result; 
    } 
    #region " Aux Functions " 
 
    private void populateGrid() 
    { 
        try 
        { 
            AMRefData db = new AMRefData(); 
            var countries = db.getCountries(); 
 
            rg_Results.DataSource = countries; 
        } 
        catch (Exception ex) 
        { 
            ErrorLogging.log4NetError(ex.Message, this.GetType()); 
            DisplayMessage(true, ex.Message); 
        } 
    } 
 
    private void DisplayMessage(bool isError, string text) 
    { 
        if (isError) 
        { 
            lbl_error.CssClass = "ErrorLabel"
        } 
        else 
        { 
            lbl_error.CssClass = "InfoLabel"
        } 
        lbl_error.Text = text; 
    } 
 
    protected DateTime? checkDate(object date) 
    { 
        try 
        { 
            if (date == null || date.ToString() == ""
            { 
                return null
            } 
            else 
            { 
                DateTime d = Convert.ToDateTime(date); 
                return d; 
            } 
        } 
        catch 
        { 
            return null
        } 
    } 
 
    protected string convertDeleted(object value) 
    { 
        try 
        { 
            if (value.ToString() == "0"
            { 
                return "No"
            } 
            else 
            { 
                return "Yes"
            } 
        } 
        catch 
        { 
            return null
        } 
    } 
 
    private bool SaveData(int id, int isDeleted, GridCommandEventArgs e) 
    { 
        try 
        { 
            if (isDirty == true
            { 
                country.CountryID = id; 
                country.CountryName = (e.Item.FindControl("txt_CountryName"as TextBox).Text; 
                country.CountryCode = (e.Item.FindControl("txt_CountryCode"as TextBox).Text; 
                foreach (objCountryGroup obj in CountryGroups) 
                { 
                    if (obj.CountryGroupCode == (e.Item.FindControl("txt_CountryGroupId"as DropDownList).SelectedValue) 
                    { 
                        country.CountryGroupID = obj.CountryGroupID; 
                    } 
                } 
                // d.CountryGroupID = Convert.ToInt32((e.Item.FindControl("txt_CountryGroupId") as DropDownList).Text); 
 
                country.Currency = (e.Item.FindControl("txt_Currency"as TextBox).Text; 
 
                AMPerson db = new AMPerson(); 
                int countryId = db.SaveCountry(country, Variables.UserId); 
 
                foreach (CountrySapInfo csi in country.CountrySapInfo) 
                { 
                    if (csi.CountryId == 0) 
                    { 
                        csi.CountryId = countryId; 
                    } 
 
                    db.SaveCountrySapInfo(csi, Variables.UserId); 
                } 
            } 
 
            isDirty = false
 
            DisplayMessage(false""); 
            //clear lock 
            Lock(id, 0); 
            return true
        } 
        catch (Exception ex) 
        { 
            ErrorLogging.log4NetError(ex.Message, this.GetType()); 
            DisplayMessage(true, ex.Message); 
            return false
        } 
    } 
 
    private bool SaveData(int id, int isDeleted, GridDataItem Item) 
    { 
        try 
        { 
            country.CountryID = id; 
            country.CountryName = Item["CountryName"].Text; 
            country.CountryCode = Item["CountryCode"].Text; 
            country.CountryGroupID = Convert.ToInt32(Item["CountryGroupID"].Text); 
            country.Currency = Item["Currency"].Text; 
 
            AMPerson db = new AMPerson(); 
            int countryId = db.SaveCountry(country, Variables.UserId); 
 
            foreach (CountrySapInfo csi in country.CountrySapInfo) 
            { 
                if (csi.CountryId == 0) 
                { 
                    csi.CountryId = countryId; 
                } 
 
                db.SaveCountrySapInfo(csi, Variables.UserId); 
            } 
 
            isDirty = false
 
            DisplayMessage(false""); 
            //clear lock 
            Lock(id, 0); 
            return true
        } 
        catch (Exception ex) 
        { 
            ErrorLogging.log4NetError(ex.Message, this.GetType()); 
            DisplayMessage(true, ex.Message); 
            return false
        } 
    } 
    #endregion " Aux Functions " 
 
    protected void Yes_Click(object sender, EventArgs e) 
    { 
        int i = 0; 
    } 

0
Rosen
Telerik team
answered on 02 Jul 2010, 12:54 PM
Hi Richard,

I have ran the page, after stripping the missing resources, but unfortunately I'm still unable to recreate the behavior you have described.
Usually such behavior can be experienced if the controls collection is modified during the postback and the command cannot be handled. Therefore please ensure you are not changing the controls collection, for example by rebinding the grid or other parent control.

Kind regards,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
ListView
Asked by
Richard Maly
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Richard Maly
Top achievements
Rank 1
Share this question
or