?
Solved

Handling Ispostback in webparts

Posted on 2006-06-17
8
Medium Priority
?
887 Views
Last Modified: 2008-01-09
Hi

I have two custom control one base list and the other normal webpart control with 2 text boxs and an add button. Both the controls are connection using connectionprovider and connectionconsumer. When I click on the productId of the first control I get the relavant data in second control, this second control handles the method for update on click of the button.

Once I click the button it is posting back the even to first control and the previous values are lost. How could this situation be handled. I assume it is related to postback event.

Could anyone suggest, Thanks in advance.
0
Comment
Question by:Pralad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:kGenius
ID: 16928666
Provide
If Not Page.IsPostBack Then ....

where you need it. I think where you build the first control. Viewstate must be enabled...

kGenius
0
 
LVL 1

Author Comment

by:Pralad
ID: 16928843
Dear KGenius

Let me explain in detail. I am building Generic Controls derived from Webparts. The control has been built in .CS file and postback of the webcontrol has to be handled within the CS file, these controls doesnt have static page. These could be dragged and dropped on any page.

So not clear how to handled , please find definitions of the both classess

 public class ProductLinkList : Com.ParagonCIS.Framework.Web.Controls.BaseList, IPostBackEventHandler
{ // this control has link button
...
...
[ConnectionProvider("ProductID")]
        public ProductLinkList GetProduct()
        {
            return this;
        }
...
...
..

}

 public class ProductEdit : System.Web.UI.WebControls.WebControl,IPostBackEventHandler
    {...
...
///this control has text boxes and button
    [ConnectionConsumer("Product")]
        public void SetProduct(ProductLinkList field)
        {
            this._authorField = field;
        }
...
...
}

These two controls are placed on the dynamic web page and  on click of the link button it populated data on second control, when I click on the button the previous values are lost.

What could be the reason, if its post back problem how should it be handled.

Thanks
0
 
LVL 1

Author Comment

by:Pralad
ID: 16928936
These two controls are placed on a web page by means of drag and drop, and on click of the link button of the first control it is pouplating data onto second control but when I click on the button the previous values are lost.

what could be the reason, if its postback problem how could it be handled.

Please suggest. Thanks in advance.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:kGenius
ID: 16929000
my excuses but still it is not clear for me. (I'm quite slow on understanding, lol :) )
You say that the previous values are lost. Are those the values in your second control, after clicking the update button.
Say click the ID in the first control, second control fills with the correct data.

Or is it that when clicking the ID linkbutton all values of your first control are lost?

Can you post more code?
kGenius
0
 
LVL 1

Author Comment

by:Pralad
ID: 16929175
KGenius

Its like this

On click of the ID link button in the first control, second controls text boxes are getting those values for editing purpose  and when click on command button of the second control I am losing the ID value and the Mode settings made earlier i.e. on click of ID in first control.

the code goes like this

 public class ProductLinkList : Com.ParagonCIS.Framework.Web.Controls.BaseList
    {
        private int pageCount;
        private long selectedItem;
        private long productid;                 ///Variable for connection provider

        private Com.ParagonCIS.Modules.Shop.Web.Controls.IShopBank ShopBanksBar = null;

        public long ProductId
        { get{return  productid;}
            set { productid = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ProductLinkList"/> class.
        /// </summary>
        public ProductLinkList() : base()
        {
            this.Title = "ProductLink List";
            this.Description = "List of available ProductLink.";
            this.PageSize = 9; //Default pagesize
        }

        [ConnectionProvider("ProductID")]
        public ProductLinkList GetProduct()
        {
            return this;
        }

        /// <summary>
        /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
        /// </summary>
        ///
        protected override void CreateChildControls()
        {
            ////Method to Add records to the table
            //Session ProdSession = Com.ParagonCIS.Framework.Domain.CreateSession();
            //using (ProdSession)
            //{
            //    Com.ParagonCIS.Modules.Shop.Business.Product MyProdClass;
            //    for (int i = 1; i < 10; i++)
            //    {
            //        ProdSession.BeginTransaction();
            //        Com.ParagonCIS.Modules.Shop.Business.Product MyProd = (Com.ParagonCIS.Modules.Shop.Business.Product)ProdSession.CreateObject(typeof(Com.ParagonCIS.Modules.Shop.Business.Product));
            //        MyProd.Name = "ProductName " + i.ToString();
            //        MyProd.ProductDescription = "Paragon-Prithvi Product";
            //        ProdSession.Commit();
            //    }
            //}

            // for (int i = 0; i < 100; i++)
            //{
            //  Com.ParagonCIS.Modules.Shop.Business.ProductLink ProductLink = this.BusinessSession.CreateObject<Com.ParagonCIS.Modules.Shop.Business.ProductLink>
            //("Description" + i.ToString(), "Addressline1_" + i.ToString(), "", "", "", "someone" + i.ToString() + "@asdjh.ml");
            //    //ProductLink.Persist();
            //}

            Com.ParagonCIS.Framework.GenericModel.Module module = null;
            string searchString = null;
            int currentPage = 1;
            if (ShopBanksBar != null && ShopBanksBar.ShopModule != null)
                module = ShopBanksBar.ShopModule;
            if (SearchFreeText != null)
                searchString = this.SearchFreeText.SearchString;
            if (Pager != null)
                currentPage = Pager.CurrentPage;
             
            DataObjects.NET.QueryPager ProductStack =
                Com.ParagonCIS.Modules.Shop.Business.Product.GetProduct(
                ((Com.ParagonCIS.Framework.Web.Pages.BasePage)base.Page).BusinessSession,
                SelectedItemID.ToString(), module, currentPage - 1, PageSize);

            PageCount = ProductStack.PageCount;
            ItemCount = ProductStack.TotalCount;

            System.Web.UI.WebControls.Panel ProductLinkListPanel = new System.Web.UI.WebControls.Panel();
            System.Web.UI.HtmlControls.HtmlGenericControl myDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

            // ProductLinkListPanel.CssClass = "ProductLinkList";
            if (ProductStack != null)
            {
                  int row;
                  int cell;
                  System.Web.UI.WebControls.TableHeaderRow tableHeaderRow;
                  System.Web.UI.WebControls.TableRow tableRow = null;
                  System.Web.UI.WebControls.LinkButton linkEditButton;
                  System.Web.UI.WebControls.LinkButton linkViewButton;
                  System.Web.UI.WebControls.LinkButton linkDeleteButton;
                  System.Web.UI.WebControls.LinkButton linkAddButton;
                  System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();
                table.Attributes.Add("style","width:100%;border:solid 1px green;border-collapse:collapse;font-family:arial;font-size:9t;");
                table.Attributes.Add("border", "1");
                table.Attributes.Add("rules", "rows");
                row = table.Rows.Add(new System.Web.UI.WebControls.TableHeaderRow());
                tableHeaderRow = (System.Web.UI.WebControls.TableHeaderRow)table.Rows[row];
                tableHeaderRow.Attributes.Add("bgcolor", "#ED810E");

                cell = tableHeaderRow.Cells.Add(new System.Web.UI.WebControls.TableCell());
                tableHeaderRow.Cells[cell].Text = "Product Name";

                  cell = tableHeaderRow.Cells.Add(new System.Web.UI.WebControls.TableCell());
                  linkAddButton = new System.Web.UI.WebControls.LinkButton();
                  linkAddButton.ID = "linkAddButton";
                  linkAddButton.Text = "Add a product";
                  linkAddButton.Click += new EventHandler(linkAddButton_Click);
                  tableHeaderRow.Cells[cell].Attributes.Add("colspan", "2");
                  tableHeaderRow.Cells[cell].Attributes.Add("align", "right");
                  tableHeaderRow.Cells[cell].Controls.Add(linkAddButton);


                foreach (Com.ParagonCIS.Modules.Shop.Business.Product ProductLinkStack in ProductStack)
                {
                       row = table.Rows.Add(new System.Web.UI.WebControls.TableRow());
                       tableRow = table.Rows[row];

                       tableRow.CssClass = "ProductLinkItem";

                       cell = tableRow.Cells.Add(new System.Web.UI.WebControls.TableCell());
                       linkViewButton = new System.Web.UI.WebControls.LinkButton();
                       linkViewButton.ID = ProductLinkStack.ID.ToString();
                       linkViewButton.Text =  ProductLinkStack.Name;
                       linkViewButton.Click += new EventHandler(linkViewButton_Click);
                       tableRow.Cells[cell].Controls.Add(linkViewButton);


                       cell = tableRow.Cells.Add(new System.Web.UI.WebControls.TableCell());
                       linkEditButton = new System.Web.UI.WebControls.LinkButton();
                       linkEditButton.ID = "Edit" + ProductLinkStack.ID.ToString();
                       linkEditButton.Text = "Edit";
                       linkEditButton.Click += new EventHandler(linkEditButton_Click);
                       tableRow.Cells[cell].Controls.Add(linkEditButton);


                       cell = tableRow.Cells.Add(new System.Web.UI.WebControls.TableCell());
                       linkDeleteButton = new System.Web.UI.WebControls.LinkButton();
                       linkDeleteButton.ID = "Del" + ProductLinkStack.ID.ToString();
                       linkDeleteButton.Text = "Delete";
                       linkDeleteButton.Click += new EventHandler(linkDeleteButton_Click);
                       tableRow.Cells[cell].Controls.Add(linkDeleteButton);


                }

                 ProductLinkListPanel.Controls.Add(table);
            }
            this.Controls.Add(ProductLinkListPanel);
        }

        ////// Defines the Click event.
        ////public event EventHandler Click;

        //////Invoke delegates registered with the Click event.
        ////protected virtual void OnClick(EventArgs e)
        ////{

        ////    if (Click != null)
        ////    {
        ////        Click(this, e);
        ////    }
        ////}


        ////// Define the method of IPostBackEventHandler that raises change events.
        ////public void RaisePostBackEvent(string eventArgument)
        ////{

        ////    OnClick(new EventArgs());
        ////}


        private void linkDeleteButton_Click(object sender, EventArgs e)
        {
            this.SelectedItemID = long.Parse(((System.Web.UI.WebControls.LinkButton)sender).ID);
        }

        private void linkViewButton_Click(object sender, EventArgs e)
        {

            string strEdit = ((System.Web.UI.WebControls.LinkButton)sender).ID;
            strEdit = strEdit.Substring(4);
            this.SelectedItemID = long.Parse(strEdit);
         
        }

        private void linkEditButton_Click(object sender, EventArgs e)
        {
            string ProductIdStr = ((System.Web.UI.WebControls.LinkButton)sender).ID;
            ProductIdStr = ProductIdStr.Substring(4);
            this.SelectedItemID = long.Parse(ProductIdStr);
            this.ProductId = this.SelectedItemID;
            this.EditMode = true;
            this.AddMode = false;
            this.ViewMode = false;
            this.DeleteMode = false;
         //   this.SelectedItemID = long.Parse(((System.Web.UI.WebControls.LinkButton)sender).ID);
        }

        private void linkAddButton_Click(object sender, EventArgs e)
        {
            this.EditMode = false;
            this.AddMode = true ;
            this.ViewMode = false;
            this.DeleteMode = false;
            //call the contentplace holder to add ProductLink
        }

    }
 /// <summary>
    /// Class ProductEdit
    /// </summary>
    public class ProductEdit : System.Web.UI.WebControls.WebControl
    {
        protected Com.ParagonCIS.Framework.Web.Controls.TextBox NameTextBox;
        protected System.Web.UI.WebControls.Label NameLabel;
        protected Com.ParagonCIS.Framework.Web.Controls.TextBox DescTextBox;
        protected System.Web.UI.WebControls.Label DescLabel;
        protected System.Web.UI.WebControls.Button ProductButton;
        protected System.Web.UI.WebControls.PlaceHolder NewPlaceHolder;

        private ProductLinkList _authorField;

        public ProductEdit() : base()
        {
            NewPlaceHolder = new System.Web.UI.WebControls.PlaceHolder();
            NewPlaceHolder.ID = "NewPlaceHolder";

            NameLabel = new System.Web.UI.WebControls.Label();
            NameLabel.ID = "NameLabel";
            NameLabel.Text = "Product Name";

            NameTextBox = new Com.ParagonCIS.Framework.Web.Controls.TextBox();
            NameTextBox.ID = "NameTextData";

            DescLabel = new System.Web.UI.WebControls.Label();
            DescLabel.ID = "DescLabel";
            DescLabel.Text = "Product Description";

            DescTextBox = new Com.ParagonCIS.Framework.Web.Controls.TextBox();
            DescTextBox.ID = "DescTextData";

            ProductButton = new System.Web.UI.WebControls.Button();

            ProductButton.ID = "ProductButton";
            ProductButton.Text = "Edit";
            ProductButton.Click += new EventHandler(ProductButton_Click);


            NewPlaceHolder.Controls.Add(NameLabel);
            NewPlaceHolder.Controls.Add(NameTextBox);
            NewPlaceHolder.Controls.Add(DescLabel);
            NewPlaceHolder.Controls.Add(DescTextBox);
            NewPlaceHolder.Controls.Add(DescTextBox);
            NewPlaceHolder.Controls.Add(ProductButton);

            this.Controls.Add(NewPlaceHolder);

        }
       
        //// Defines the Click event.
        //public event EventHandler Click;

        ////Invoke delegates registered with the Click event.
        //protected virtual void OnClick(EventArgs e)
        //{
        //    if (Click != null)
        //    {
        //        Click(this, e);
        //    }
        //}

        //// Define the method of IPostBackEventHandler that raises change events.
        //public void RaisePostBackEvent(string eventArgument)
        //{
           
        //    OnClick(new EventArgs());
        //}
     
        [ConnectionConsumer("Product")]
        public void SetProduct(ProductLinkList field)
        {
            this._authorField = field;
        }
       

        protected override void  Render(HtmlTextWriter writer)
{
//    ProductButton.Attributes.Add("OnClick", "this.RaisePostBackEvent('ButtonClicked')");
        base.Render(writer);
}
        protected override void OnPreRender(EventArgs e)
        {   // this.Page.ClientScript.RegisterClientScriptResource(
            // typeof(NumericTextBox),
            //  "Obies.Web.UI.WebControls.NumericTextBox.js");
            //  long ProductId = 205;
            if (this._authorField != null)
            {
         if (this._authorField.AddMode)
            {ProductButton.Text = "ADD";
             NameTextBox.Text = "";
             DescTextBox.Text="";
               }

            if (this._authorField.EditMode)
            {
                ProductButton.Text = "Edit";
                long ProductId = this._authorField.ProductId;
                Com.ParagonCIS.Modules.Shop.Business.Product ProdDetail = Com.ParagonCIS.Modules.Shop.Business.Product.GetProduct(
                     ((Com.ParagonCIS.Framework.Web.Pages.BasePage)base.Page).BusinessSession, ProductId);
                NameTextBox.Text = ProdDetail.Name;
                DescTextBox.Text = ProdDetail.ProductDescription;
               //     Page.ClientScript.RegisterClientScriptResource(typeof(ProductEdit), "Com.ParagonCIS.Modules.Shop.Web.Controls.AjaxControl.js");
                base.OnPreRender(e);
             }
            }
        }

        /// <summary>
        /// Method to Add new Product Information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductButton_Click(object sender, EventArgs e)
        {
          //  ProductButton.Attributes.Add("EditClick", 1);

            //Com.ParagonCIS.Modules.Shop.Business.Product ProdDetail = Com.ParagonCIS.Modules.Shop.Business.Product.GetProduct(
                 //((Com.ParagonCIS.Framework.Web.Pages.BasePage)base.Page).BusinessSession, _authorField.FieldValue);
       //     this.RaisePostBackEvent("add clicked");
            if (this._authorField != null)
            {
                if (this._authorField.EditMode || this._authorField.AddMode)
                {
                    Session ProdSession = Com.ParagonCIS.Framework.Domain.CreateSession();
                    using (ProdSession)
                    {

                        ProdSession.BeginTransaction();
                        Com.ParagonCIS.Modules.Shop.Business.Product MyProd = (Com.ParagonCIS.Modules.Shop.Business.Product)ProdSession.CreateObject(typeof(Com.ParagonCIS.Modules.Shop.Business.Product));
                        MyProd.Name = NameTextBox.Text;
                        MyProd.ProductDescription = DescTextBox.Text;
                        ProdSession.Commit();
                        NameTextBox.Text = "";
                        DescTextBox.Text = "";
                    }
                }
            }
        }
    }
0
 
LVL 7

Expert Comment

by:kGenius
ID: 16929285
Can you access the _authorField.ProductID property in "private void ProductButton_Click"
is there any value returning? Are they in the object when filling the Edit mode?

Is it possible to store the selected ID and mode in Session, and read every time you need to rebuild your page?


kGenius
0
 
LVL 1

Author Comment

by:Pralad
ID: 16929340
Yes, I am able to access the object '_authorField' in the 'ProductButton_click()' method
but before it reaches here the object '_authorField' is becoming null i.e. when I click on ProductButton it goes to controls base() method and '_authoerField' is initialized to null that is creating problem
0
 
LVL 7

Accepted Solution

by:
kGenius earned 600 total points
ID: 16929681
What happens when you put if (! Page.IsPostBack) { } in public ProductEdit() : base() ?

Else Can't you store your _authorField that has been selected in a Session-variable? After clicking the ProductButton it will goes to controls base(). Then you _authorField=Session("authorField") to reload the wanted field...

kGenius
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question