FAQ: Why do dynamic controls disappear on postback and not raise events?

Rate It (13)

Last post 11-05-2009 12:36 PM by mausimo. 48 replies.

Sort Posts:

  • Re: FAQ: Why do dynamic controls disappear on postback and not raise events?

    10-25-2009, 2:05 PM
    • Member
      6 point Member
    • rlnd
    • Member since 03-14-2007, 1:54 PM
    • Posts 8

    Is it possible that this approach doesn't work for custom events? I've got a ul to which I'm programmatically adding (ASP.net user control) lis. I'm re-adding these user controls in the Page_Load event and 'ordinary' events (that is: LinkButton.Clicks) work fine - only my custom events somehow lose their handlers during the postback.

    Here is how I'm adding the controls to the list (the IDs are persistent over postbacks):

    Newsletter_TextPart control = (Newsletter_TextPart)LoadControl("~/Newsletter/TextPart.ascx");
            control.ID = "part_" + value.PartID;
    
    
            control.Deleted += new Newsletter_TextPart.DeletedEventHandler(part_Deleted);
    
            AsyncPostBackTrigger t2 = new AsyncPostBackTrigger();
            t2.ControlID = control.ID;
            t2.EventName = "Deleted";
    
            upnlText.Triggers.Add(t2);
    
    
            ulTextParts.Controls.Add(control);
            control.Part = value;


    Here is the relevant code in the UserControl:

        public delegate void DeletedEventHandler(object sender, PartEventArgs e);
    
        public event DeletedEventHandler Deleted;
    
        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            if (Deleted != null)
                Deleted(this, new PartEventArgs(Part)); //'Part' is a property of the control
        }


  • Re: dynamic control help plzzzzzzzzzzzzzzz.....

    10-28-2009, 5:07 AM

    amitha:

    this is my program logic

    i am new to this web developement so looking forward for your help

    plzzzzzzzzzz go thru

    reply asap

    having error with find control part

    do suggest all changes nedeed

    or else

    if possible  can u modify  my code n send rest of my project is going good i am really stuck with this plzz

    i hav to take values frm text box n calculate base on it


    <form id="frmPension" runat="server">

    <asp:Panel id="pnlone" runat="server" Height="74px" Width="384px">

    <asp:Label ID="lblAmnt" Text=" Enter the pension amount:" runat="server" Width="180px"></asp:Label>

    <asp:TextBox id="tbAmnt" runat="server"/><br />

    <asp:Label ID="lblNo" Text=" Enter the number of persons:" runat="server" Width="180px"></asp:Label>

    <asp:TextBox id="tbNo" runat="server"/><br />

    <center> <asp:button id="Enter" text="Enter" OnClick="SubmitBtn_Click" runat="server"/></center><br />

    <p><asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder></p>

    <table id="tblReslt" runat="server">

    <tr>

    <td id="col1" runat="server"></td>

    <td id="col2" runat="server"></td>

    </tr>

    <tr>

    <td><asp:Button ID="submit" runat="server" visible="false" OnClick="calInheritAmnt" Text="submit" /></td>

    </tr>

    </table>

    </asp:Panel>

     

    <asp:table id="tblInheritDetails" runat="server" width="100%"/>

    </form>

     

    Partial Class testajax

    Inherits System.Web.UI.Page

    Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)

    If IsPostBack Then

    SubmitBtn_Click(Sender, E)

    End If

    End Sub

    Public Sub SubmitBtn_Click(ByVal Sender As Object, ByVal E As EventArgs)

    Dim I As Integer

    For I = 1 To CInt(tbNo.Text.Trim())

    Dim tbInhrtrName As New TextBox

    tbInhrtrName.ID = "tbDynamic" & I.ToStringtbInhrtrName.Text = "Control" & I

    Form.Controls.Add(tbInhrtrName)

    'Area1.Controls.Add(tbInhrtrName)

    Dim MyLiteral = New LiteralControl

    MyLiteral.Text = "<BR><BR>"

    Form.Controls.Add(MyLiteral)

    'Area1.Controls.Add(MyLiteral)

    Next

    submit.Visible = "true"

    tblInheritDetails.Visible = "true"

    End Sub

    Protected Sub calInheritAmnt(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click

    Dim id As String

    Dim I As Integer

    'Dim tb As New TextBox

    For I = 1 To CInt(tbNo.Text.Trim())

    Dim tb As New TextBox

    id = "tbDynamic" & I.ToStringtb = DirectCast(sender, TextBox)

    Response.Write(tb.Text.ToString())

    'Response.Write(Session(id) & "<br/>")

    Next

    End Sub

    End Class


  • Re: dynamic control help plzzzzzzzzzzzzzzz.....

    10-29-2009, 1:11 AM

    hi,

    you have written --  having error with find control part

    but where is the code for finding control.

    Filed under:
  • Re: dynamic control help plzzzzzzzzzzzzzzz.....

    11-05-2009, 12:36 PM
    • Member
      32 point Member
    • mausimo
    • Member since 10-05-2009, 2:58 PM
    • Edmonton, Ab, Canada
    • Posts 58

    For everyone that wanted viewstate code in C#....

    const bool CREATE_SELECTED = true;
        const bool VIEW_SELECTED = true;
        const bool CURRENT_SELECTED = true;
    
        //store property value in viewstate so that it will survive postbacks
         private bool p_Create
         {
             set
             {
                ViewState["CREATE_SELECTED"] = value;
             }
             get
             {
                return Convert.ToBoolean(ViewState["CREATE_SELECTED"]);
             }
          }
    
         private bool p_View
         {
             set
             {
                 ViewState["VIEW_SELECTED"] = value;
             }
             get
             {
                 return Convert.ToBoolean(ViewState["VIEW_SELECTED"]);
             }
         }
    
         private bool p_Current
         {
             set
             {
                 ViewState["CURRENT_SELECTED"] = value;
             }
             get
             {
                 return Convert.ToBoolean(ViewState["CURRENT_SELECTED"]);
             }
         }


     

     protected void Page_Load(object sender, EventArgs e)
        {
            if (p_Create)
            {
                this.CreateWasClicked();
            }
            if (p_View)
            {
                this.ViewWasClicked();
            }
            if (p_Current)
            {
                this.CurrentWasClicked();
            }       
        }


     

        /// <summary>
        /// button used to view the view Articles
        /// </summary>
        protected void btnView_Click(object sender, EventArgs e)
        {
            //if the add article interface is already visible then hide it
            if (PlaceHolder_View.Visible == true)
            {
                PlaceHolder_View.Visible = false;
                btnView.Text = "View";
    
                //VERY IMPORTANT -> remember that we created these controls for the next postback
                this.p_View = false;
            }
            //if the add article interface is not visible then show it
            else
            {
                //displaying the add/create arcticle interface
                PlaceHolder_View.Visible = true;
                //changing button text to hide the create article interface.
                btnView.Text = "Hide View";
    
                //VERY IMPORTANT -> remember that we created these controls for the next postback
                this.p_View = true;
            }
        }
    
        /// <summary>
        /// for keeping view visible after post back if it was clicked
        /// </summary>
        protected void ViewWasClicked()
        {
            PlaceHolder_View.Visible = true;
        }


    This is the jist of using viewsates to keep post back variables in C#. This is from my code, it is showing you how to use the functionality. Manipulate this how you want, you can use any type (does not have to be bool etc).

    Good-Luck

    -Mike 

Page 4 of 4 (49 items) < Previous 1 2 3 4