Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Thursday, 6 July 2017

Maintain The State Of Dynamic Added User Control On Each PostBack

 asp.net, C#, user-control     7 comments   


Introduction
In this article we will learn how to maintain the state of dynamically created controls in asp.net using c#.  Retain the state of dynamic created controls or maintain the data of dynamic created/ added user control on postback in asp.net. Maintain the dynamic control events, data during postback in asp.net

Previous Updates
In previous articles we have learnt Load the Usercontrol In Aspx Page.  Get TextBox , Dropdown, CheckBox  control Values In Aspx.cs Page From User Control Using C#.  Binding Data To HTML using AngularJS.  CRUD operation using AngularJS.


Problem With Dynamic Created Controls
When you working with dynamic controls in any WebApplication then the biggest task is to understand the postback and maintain the state of newly created dynamic control at the time of Postback.
If you are not familiar with the postback then  you will get the best answer here.

Postback is a process when you submit a asp.net  web page to the server for processing. In other words -
Postback is a process of sending  roundtrip request from Client Machine/ Browser to server for processing the request  and  then back to the client. 

For Example if you are working with any website which have registration page or any form filling page like a login page. There is validation used in that Login page. 
                    Now suppose when you click on login button for performing login operation and application have validations for check the requested mail id and Password is correct or not  , if it is failed then server sends the validation message back to client and remain on the same login page. That's the postback when the server sends back the message to client.


Asp.net  controls state manage by their viewstate. All asp.net controls maintain their data in viewstate during postback. But when you are with dynamic controls the will not able to maintain its state during postback and their data lost.
Means if you fill all your detail in web registration form and click on submit then after postback if the page again comes back with any validation error issue then all of your filled data lost from all textboxes and other controls on form. This only happens if these controls will not maintaining there state. 
Same data lost issue with our dynamic controls. Cause they dont have any viewstate to maintain their data .So here we learn how to overcome from this problem and retain the data as like normal controls.


Generate Dynamic Controls And Maintain State
Here i am using the same example which i use in my previous updates Load the user control in aspx page and get TextBox , Dropdown, CheckBox  control Values In Aspx.cs Page From User Control Using C# .
UserControl Code ---
<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="uc_MyUserControl.ascx.cs" Inherits="UserControls_uc_MyUserControl" %>

 <table>
     <thead> <h3> User Control Loaded </h3></thead>
   <tr>
      <td> <asp:Label ID="lblName" Text="First Name" runat="server"></asp:Label> </td>
      <td> <asp:TextBox ID="txtFName" Text="" runat="server"></asp:TextBox> </td>
   </tr>
   <tr>
     <td> <asp:Label ID="lblGender" Text="Gender" runat="server"></asp:Label> </td>
     <td> <asp:DropDownList ID="ddlGender" runat="server" Width="173px">
                            <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
                            <asp:ListItem Text="Male" Value="M"></asp:ListItem>
                            <asp:ListItem Text="Female" Value="F"></asp:ListItem>
                        </asp:DropDownList> </td>
    </tr>
     <tr>
          <td> <asp:Label ID="lblAdi" Text="Adult" runat="server"></asp:Label> </td>
         <td><asp:CheckBox ID="chkAdult" runat="server" Checked="false" /></td>
     </tr>
</table>

Aspx Code ---
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyWebForm1.aspx.cs" 
    Inherits="UserControls_MyWebForm1" %>
<%@ Register Src="~/UserControls/uc_MyUserControl.ascx" 
    TagPrefix="uc1" TagName="uc_MyUserControl" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> </head>
<body>
    <form id="form1" runat="server">
         <div>
            <asp:Button ID="btnGenrate" runat="server" Text="Generate Dynamic Controls"
                       OnClick="btnGenrate_Click" />
        </div>
     <div>
         <asp:Panel ID="pnlMyUC" runat="server">
            <uc1:uc_MyUserControl runat="server" ID="uc_MyUserControl" />
             </asp:Panel>
     </div>
    </form>
</body>
</html>

Now the main code where we generate the dynamic controls and maintain their values during postback. 
Aspx.cs Code  ---
#region Gloabal Variables
    private Dictionary<Guid, string> tempList = new Dictionary<Guid, string>();

    public Dictionary<Guid, string> UcList
    {
        get { return ViewState["MyUcIds"] != null 
                 ? (Dictionary<Guid, string>)ViewState["MyUcIds"]  : new Dictionary<Guid, string>(); }
        set { ViewState["MyUcIds"] = value; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadControl();
        else
            InitializeUC();
    }

    public void LoadControl()
    {
        UserControls_uc_MyUserControl objUC = 
                             (UserControls_uc_MyUserControl)Page.FindControl("uc_MyUserControl");
        pnlMyUC.Controls.Add(objUC);
    }

   //Initialize the control during postback and retain their values
    public void InitializeUC()
    {
        int index = 1;
        foreach (var item in UcList)
        {
            var myUc =
 (UserControls_uc_MyUserControl)LoadControl("~/UserControls/uc_MyUserControl.ascx");
            myUc.ID = item.Value;
            pnlMyUC.Controls.AddAt(index, myUc);
            index++;
        }
    }

    protected void btnGenrate_Click(object sender, EventArgs e)
    {
        UserControls_uc_MyUserControl myUc;
        Guid lastKey = Guid.NewGuid();      //Generate random keys for dynamic controls
        tempList = UcList;
        if (UcList.Count > 0)
        {
            lastKey = UcList.Last().Key;
            lastKey = Guid.NewGuid();
        }
        tempList[lastKey] = "objUc" + lastKey.ToString().Replace('-', '_');
        UcList = tempList;
        myUc =
 (UserControls_uc_MyUserControl)LoadControl("~/UserControls/uc_MyUserControl.ascx");
        myUc.ID = UcList[lastKey];
        pnlMyUC.Controls.Add(myUc);
    }
In this code i define a tempList variable of Dictionary type for containing key and value of usercontrol. and a UcList type property which required for store usercontrol data temporarily on InitializeUC method.
When our page first time loads without postback it will call the LoadControl Method and loads the Usercontrol first time but after this when clicking on Genrate Dynamic Control button it will always call the InitializeUC method on postback true condition.
InitializeUC method reads the user controls list from viewstate and assigning back to the aspx page thats why our values will remain same during postback or any event click.

OutPut 





  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home
View mobile version

7 comments:

  1. Ricardo Ramalho10 December 2019 at 03:55

    What if you have a Repeater or ListView, and the ItemTemplate has dynamically generated controls?

    How do I manage to have those values between postbacks?

    ReplyDelete
    Replies
      Reply
  2. Unknown3 January 2020 at 02:24

    can you please explain what is UserControls_uc_MyUserControl. I am getting an error while writing this as such namesapce does not exist.

    ReplyDelete
    Replies
      Reply
  3. Alp8 August 2023 at 01:17

    yurtdışı kargo
    resimli magnet
    instagram takipçi satın al
    yurtdışı kargo
    sms onay
    dijital kartvizit
    dijital kartvizit
    https://nobetci-eczane.org/
    ZBLİ2

    ReplyDelete
    Replies
      Reply
  4. Polat18 August 2023 at 20:34

    Hollanda yurtdışı kargo
    İrlanda yurtdışı kargo
    İspanya yurtdışı kargo
    İtalya yurtdışı kargo
    Letonya yurtdışı kargo
    PQ1

    ReplyDelete
    Replies
      Reply
  5. Cevat24 August 2023 at 02:30

    Angila yurtdışı kargo
    Andora yurtdışı kargo
    Arnavutluk yurtdışı kargo
    Arjantin yurtdışı kargo
    Antigua ve Barbuda yurtdışı kargo

    SWWCJ0

    ReplyDelete
    Replies
      Reply
  6. Veysel25 August 2023 at 02:05

    Azerbaycan yurtdışı kargo
    Aruba yurtdışı kargo
    Avustralya yurtdışı kargo
    Azor Adaları yurtdışı kargo
    Bahamalar yurtdışı kargo

    W714

    ReplyDelete
    Replies
      Reply
  7. Anonymous30 April 2025 at 01:21

    شركة مكافحة حشرات بالاحساء
    WiVv1ZfJYH

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write