Solved

Page Load after a button click code is executed?

Posted on 2004-10-25
405 Views
Last Modified: 2012-06-27
I know that the Page Load event runs before the button code event is clicked.  How can a do a page load after the button code is executed.

Here is what I'm doing:

I'm creating dynamic textboxs.  When the user clicks a button, it creates a set of textboxes and uses a ViewState count to recreate the boxes in the page load

*****************
            Sub Page_Load
      Dim intCounter as Integer
      
      If Not IsPostBack Then
            ViewState("CVICount") = 1
      ElseIf ViewState("CVICount") > 1 Then
            For intCounter = 2 To ViewState("CVICount")
                  AddCVI(intCounter)
            Next
      End If
******************

I have a clear button to remove all the dymanic textboxes that the user has created.  I do this by resetting the ViewState("CVICount") to 1

*******************
            Sub tClear_Click (s as Object, e as EventArgs)
      ViewState("CVICount") = 1
            End Sub
*******************

This works, but only if the Clear button is clicked twice.  Is there anything that will fix this?
            End Sub
0
Question by:FIM2003
    13 Comments
     
    LVL 28

    Expert Comment

    by:mmarinov
    Hi FIM2003,

    have you tried :

    Sub GenerateCl( count as Integer )
              For intCounter = 2 To count
                   AddCVI(intCounter)
              Next
    End Sub

    Sub Page_load
         Dim intCounter as Integer
         
         If Not IsPostBack Then
              ViewState("CVICount") = 1
         ElseIf ViewState("CVICount") > 1 Then
             GenerateCl ( ViewState("CVICount")  )
         End If

                   Sub tClear_Click (s as Object, e as EventArgs)
                     ViewState("CVICount") = 1
                    GenerateCl ( ViewState("CVICount")  )
                End Sub
    Regards!
    B..M
    0
     

    Author Comment

    by:FIM2003
    B..M

    I tried your suggestion, but I still have to click the Clear button twice for the textboxes to go away.
    0
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    Hi FIM2003,

    I will recommend you name your dynamic controls such as cust_ctrl_txtUserName etc.
    Dim Form1 As HtmlForm
    Dim txt As New TextBox
    txt.ID = "cust_ctrl_test"
    Form1.Controls.Add(txt)

    After that you can remove all these dynamic control in the following way:

    for each Control c in this.Controls
       if c.ID IS NOT NULL and c.ID.IndexOf("cust_ctrl_test") >=0 then
         Form1.Controls.Remove(c)
       end if
    Next

    Place the above code in the clear button click event.

    HTH, Nauman.
    0
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    Plz replace for each Control c in this.Controls with for each Control c in Form1.Controls

    Best, Nauman.
    0
     

    Author Comment

    by:FIM2003
    Nauman,

    I'm new to ASP.NET, so please bear with me.

    I'm not sure if your suggestion will work, the user can add as many set of boxes as the want.  I have four boxes per set and this is how I name them:
    First set: Q1, D1, P1, A1
    Second set: Q2, D2, P2, A2
    and so on..

    I have another set at the bottom that has 5 textboxes, these need to be cleared by a second Clear button.

    THanks,
    FIM2003
    0
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    ok..in this cas what I will recommend is add a server side div. Add all of your control set to this div and when you want to remove the controls call FormName.DivName.Controls.Clear()

    .aspx
    ------------
    <div id="divCustomControls" runat="Server">

    </div>


    .aspx.vb
    --------------------
    Dim divCustomControls As Generic HtmlControl

    Sub  AddCVI()

    'Create control

    'Add it to the Div
    divCustomControls.Controls.Add(YOURCONTROL)

    end sub

    Sub tClear_Click (s as Object, e as EventArgs)
       divCustomControls.Controls.Clear()
       Response.Redirect("YOURASPXPAGE.aspx")
    end sub

    Best, Nauman.
    0
     
    LVL 33

    Expert Comment

    by:raterus
    I see new people thinking they need to be using dynamic controls which I doubt you need to.  Can you just declare all the controls you need on the aspx page and set their visibility?  That is usually a much easier solution to code around.  Dynamic controls are NOT persisted across postbacks, so you have to be pretty good to keep track of them, I would consider them pretty advanced and to be avoided at all costs.
    0
     

    Author Comment

    by:FIM2003
    Nauman,

    I don't think your suggestion would work, because when I do the redirect the second set would not postback and I need this to happen.

    raterus,

    The reason I am using the dynamic controls is because I don't know how many "set" of text boxes the user will need. I agree that setting the visibility setting would be much easier, but I just don't think I can use that in this case.  I think I have the controls working in the postback.  What exactly do you mean by not persisted across the postbacks?

    Thanks,
    Fim2003
    0
     
    LVL 33

    Accepted Solution

    by:
    Well when you add a bunch of textboxes dynamically, and then click a button to submit the form, all those textboxes you just spent a bunch of time creating..you then have to recreate all those textboxes just like you originally did, in page_load, if you ever want to access their values.  If you don't do this, not only will the controls themself "dissapear" but there isn't a way to get at the values posted (though you can use Request.Form).  

    When I first started asp.net, I really didn't know any better, and ran into the same things you are running into using dynamic controls.  Here is how I've since adapted my thinking.

    If you don't know the number you need, you might look at using a Repeater control (or a DataList, or a DataGrid).  After postback, you can iterate through the items collection and get submitted values, also these controls maintain their state, so the textboxes will be persisted across postbacks.  It may be a little unnatural how you have to get the values out of these controls, but I can guarantee it is much easier than using dynamic controls!

    Hope this helps,
    --Michael
    0
     
    LVL 25

    Expert Comment

    by:nauman_ahmed
    FIM2003, why you want the second set of controls to generate a postback event? Your question is still not clear.

    -Nauman.
    0
     
    LVL 3

    Expert Comment

    by:nitrogenx
    try overloading the OnPreRender sub
    0
     
    LVL 33

    Expert Comment

    by:raterus
    I provided a good alternative to the bad idea he originally had.  I think I should get at least some of the points.
    0
     

    Author Comment

    by:FIM2003
    Raterus,

    I have not been able to try your suggestion yet.  I was moved to another project and have not had the time to try your solution.  I apologize and will award you the points.

    FIM2003
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now