[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Page Load after a button click code is executed?

Posted on 2004-10-25
16
Medium Priority
?
408 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
Comment
Question by:FIM2003
[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
  • 3
  • +2
16 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12400861
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
ID: 12401067
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
ID: 12401183
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

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

Best, Nauman.
0
 

Author Comment

by:FIM2003
ID: 12401337
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
ID: 12401447
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
ID: 12401554
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
ID: 12401709
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:
raterus earned 750 total points
ID: 12401801
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
ID: 12402358
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
ID: 12405489
try overloading the OnPreRender sub
0
 
LVL 33

Expert Comment

by:raterus
ID: 12729086
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
ID: 12736932
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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