Add New Record is not firing after second time inserting using dynamic radgrid

2 posts, 0 answers
  1. Karthi
    Karthi avatar
    2 posts
    Member since:
    Mar 2014

    Posted 31 Jan 2015 Link to this post

    I build dynamic Radgrid with edit form template. First time, i can add new record, when i click add new record second time, the event is not firing but third time event is firing. Kindly help me to solve this problem.
    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 AjaxControlToolkit;
    using Telerik.Web.UI;
    using System.Collections.Specialized;
    using System.Collections;
    public partial class ViewCase : System.Web.UI.Page
    {
        SQLHelper sqlhelper = new SQLHelper();
        static DataTable dt1 = new DataTable();
        static DataTable dt = new DataTable();
        int j = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            BindAccordion();
        }
         
        private void BindAccordion()
        {
            try
            {
                DataSet ds = sqlhelper.ExecuteDataSet("DBP_GetCaseList 1");
                dt = ds.Tables[0];
                dt1 = ds.Tables[1];
                Accordion.Panes.Clear();
     
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    AccordionPane ap1 = new AccordionPane();
     
                    Label lbl = new Label();
                    lbl.Text = Convert.ToString(dr["strCase"]);
                    ap1.ID = lbl.Text;
                    lbl.ID = lbl.Text;
                    RadGrid RadGrid1 = new RadGrid();
                    RadGrid1.ID = lbl.Text + i;
                    RadGrid1.MasterTableView.DataKeyNames = new string[] { "iid" };
                    RadGrid1.Skin = "Default";
                    RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_ItemDataBound);
                    RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
                    RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
                     
                    RadGrid1.Width = Unit.Percentage(100);
                    RadGrid1.PageSize = 15;
                    RadGrid1.AllowPaging = true;
                     
                    RadGrid1.AutoGenerateColumns = false;
                    //GridBoundColumn boundColumn;
                    GridBoundColumn boundColumn = new GridBoundColumn();
                    boundColumn.DataField = "Sno";
                    boundColumn.HeaderText = "S.No";
                    RadGrid1.MasterTableView.Columns.Add(boundColumn);
                    boundColumn = new GridBoundColumn();
                    boundColumn.DataField = "strHeaderNote";
                    boundColumn.HeaderText = "Header Note";
                    boundColumn.UniqueName = "Header";
                    RadGrid1.ClientIDMode = ClientIDMode.Static;
                    RadGrid1.MasterTableView.Columns.Add(boundColumn);
                    RadGrid1.MasterTableView.CommandItemSettings.AddNewRecordText = "Add New Record";
                    RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.TopAndBottom;
                    RadGrid1.MasterTableView.EditMode = GridEditMode.EditForms;
                    GridEditCommandColumn editColumn = new GridEditCommandColumn();
                    editColumn.ButtonType = GridButtonColumnType.ImageButton;
                    editColumn.UniqueName = "EditColumn";
                    RadGrid1.MasterTableView.Columns.Add(editColumn);
     
                    //form setting
                    RadGrid1.MasterTableView.EditFormSettings.CaptionFormatString = "Header Note Entry";
                    RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template;
     
                    RadGrid1.MasterTableView.EditFormSettings.FormTemplate = new MyEditFormTemplate();
     
                    RadGrid1.Width = Unit.Percentage(100);
                    RadGrid1.PageSize = 15;
                    RadGrid1.AllowPaging = true;
                    RadGrid1.AutoGenerateColumns = false;
                    //form setting
                    RadGrid1.MasterTableView.EditFormSettings.CaptionFormatString = "Header Note Entry";
                    RadGrid1.MasterTableView.EditFormSettings.EditFormType = GridEditFormType.Template;
                    ap1.HeaderContainer.Controls.Add(lbl);
                    ap1.ContentContainer.Controls.Add(RadGrid1);
     
                    Accordion.Panes.Add(ap1);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
      
        public void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            for (int i = j; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                DataView dv = new DataView();
                dv = dt1.DefaultView;
                dv.RowFilter = "imstrid=" + dr["iid"];
     
                foreach (AjaxControlToolkit.AccordionPane accPane in Accordion.Panes)
                {
                    RadGrid rg = (RadGrid)accPane.ContentContainer.FindControl(Convert.ToString(dr["strCase"]) + j);
                    if (rg != null) { rg.DataSource = dv.ToTable(); break; }
     
                }
                j++;
                break;
            }
        }
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;
                Button Submit = editFormItem.FindControl("Submit") as Button;
                if (parentItem != null)
                {
                    TextBox box = editFormItem.FindControl("txtHeaderNote") as TextBox;
                    box.Text = parentItem["Header"].Text;
     
                    Submit.Text = "Update";
                    Submit.CommandName = "Update";
                }
                else
                {
                    Submit.Text = "Insert";
                    Submit.CommandName = "Insert";
                }
     
            }
     
     
        }
        public class MyEditFormTemplate : IBindableTemplate
        {
            public void InstantiateIn(Control container)
            {
                GridEditFormItem item = ((GridEditFormItem)(container.NamingContainer));
                Table t = new Table();
                t.Attributes.Add("style", "margin:auto;width:500px");
     
                TableRow tr = new TableRow();
                TableCell td = new TableCell();
                Label lbl = new Label();
                lbl.Text = "Header Note";
                td.VerticalAlign = VerticalAlign.Top;
                td.Controls.Add(lbl);
                tr.Controls.Add(td);
                TextBox tb1 = new TextBox();
                tb1.ID = "txtHeaderNote";
                tb1.TextMode = TextBoxMode.MultiLine;
                tb1.Width = new Unit("400");
                tb1.Height = new Unit("100");
                tb1.Text = " ";// item.ParentItem.OwnerTableView.DataKeyValues[((GridDataItem)(item.ParentItem)).ItemIndex]["strHeaderNote"].ToString();           
                td = new TableCell();
                td.Controls.Add(tb1);
                tr.Controls.Add(td);
                t.Controls.Add(tr);
                tr = new TableRow();
                td = new TableCell();
                td.VerticalAlign = VerticalAlign.Top;
                lbl = new Label();
                lbl.Text = " ";
                td.Controls.Add(lbl);
                tr.Controls.Add(td);
                Button btn1 = new Button();
                btn1.CommandName = "Update";
                btn1.Text = "Update";
                btn1.ID = "Submit";
                td = new TableCell();
                td.Controls.Add(btn1);
                tr.Controls.Add(td);
                t.Controls.Add(tr);
                container.Controls.Add(t);
                //container.Controls.Add(btn1);
            }
            public System.Collections.Specialized.IOrderedDictionary ExtractValues(System.Web.UI.Control container)
            {
                OrderedDictionary od = new OrderedDictionary();
                od.Add("iid", ((TextBox)(((GridEditFormItem)(container)).FindControl("txtHeaderNote"))).Text);
                return od;
            }
        }
     
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                //Response.Write(e.CommandName);
                if (e.CommandName == "Delete")
                {
     
                    GridDataItem item = e.Item as GridDataItem;
     
                    string iid = item.GetDataKeyValue("iid").ToString();
                    string[] paramNames = { "@indication", "@iid", "istatus" };
                    string[] paramValues = { "4", iid, "1" };
                    sqlhelper.ExecuteQuery("DBP_PhysicianSpecialty", paramNames, paramValues);
                    ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Successfully Deleted');", true);
                    BindAccordion();
     
     
                }
     
                else if (e.CommandName == "Insert")
                {              
                   
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    // HiddenField hdnUniqueAuthorID = (editedItem.FindControl("hdnUniqueID") as HiddenField);
                    TextBox txtHeaderNote = (editedItem.FindControl("txtHeaderNote") as TextBox);
                    string[] paramNames = { "@indication", "@iMstrID", "@strheadernote" };
                    string[] paramValues = { "2", "1", txtHeaderNote.Text };
                    sqlhelper.ExecuteQuery("DBP_GetCaseList", paramNames, paramValues);
                 //   ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Successfully Inserted');", true);
                    BindAccordion();
                
     
                }
                else if (e.CommandName == "Update")
                {
                    /*
                    GridEditableItem editedItem = e.Item as GridEditableItem;
                    string[] paramNames = { "@indication", "@iMstrID", "@strheadernote" };
                    TextBox txtHeaderNote = (editedItem.FindControl("txtHeaderNote") as TextBox);
                    string[] paramValues = { "2", "1", txtHeaderNote.Text };
                    sqlhelper.ExecuteQuery("DBP_GetCaseList", paramNames, paramValues);
                    ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Successfully Updated');", true);
                   */
     
                    BindAccordion();
                }
     
            }
            catch (Exception ex)
            {
                string msg = ex.Message.Replace("\r\n", "").Replace("'", "`");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "err", "alert('" + msg + "');", true);
                //RadGrid1.Controls.Add(new LiteralControl("Unable to update Reason: " + ex.Message));
     
            }
        }
     
    }
     
    .aspx
       <ajax:Accordion ID="Accordion" runat="server" SelectedIndex="0" HeaderCssClass="accordionHeader"
                HeaderSelectedCssClass="accordionHeaderSelected" ContentCssClass="accordionContent"
                FadeTransitions="true" SuppressHeaderPostbacks="true" TransitionDuration="250"
                Width="100%" FramesPerSecond="40" RequireOpenedPane="false" AutoSize="None">
            </ajax:Accordion>
  2. Karthi
    Karthi avatar
    2 posts
    Member since:
    Mar 2014

    Posted 31 Jan 2015 in reply to Karthi Link to this post

    I solved my problem. I just bind the datasource instead of re-creating controls after inserting record. 
Back to Top