Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button Control and Event Handler at runtime 1

Status
Not open for further replies.

tman24m

Programmer
May 21, 2001
93
US
Help! I swear I've tried everything and getting nothing. I'm trying to add a button at run time but when the button is clicked all I get is a postback event. The event that I want to fire, never does. Thanks in advance....here's my code.

Currently this is in the Private Sub InitializeComponent
Protected WithEvents btnUpdateAdj As New System.Web.UI.WebControls.Button

this is all being called in a sub If Not Page.IsPostBack
'Private WithEvents btnUpdateAdj As New Button

'btnUpdateAdj = New Button
btnUpdateAdj.Text = "Update Adjudication"
'btnUpdateAdj.ID = "btnUpdateAdj"
'btnUpdateAdj.Attributes.Add("onClick", "update")
'btnUpdateAdj.CommandArgument = "test"
'btnUpdateAdj.CommandName = "add"


Dim tRowHeader As New TableRow
Dim tCell As New TableCell

tRowHeader = GetTableHeaderRow(tableResults)

tCell.BackColor = System.Drawing.Color.White
tCell.HorizontalAlign = HorizontalAlign.Center
tCell.ColumnSpan = 2
tRowHeader.Cells.Add(tCell)
tcell.Controls.Add(btnUpdateAdj)
AddHandler btnUpdateAdj.Click, AddressOf update





and here's the event
Public Sub update(ByVal sender As Object, ByVal e As System.EventArgs)

Response.Write("test")

End Sub
 
When you use the attributes collection, you are wiring the onclick JavaScript event handler, not the server event.

Use a delegate instead.

I don't know VB, but in C# a delegate looks like:

Code:
this.Button1.Click += new System.EventHandler(this.Button1_Click);



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Right. The attributes line is commented out and I have tried AddHandler btnUpdateAdj.Click, AddressOf update which is the equivalent of EventHandler in c#
Here is pretty much where it's at without being commented out

dim btnUpdateAdj as new Button= New Button
btnUpdateAdj.Text = "Update Adjudication"
btnUpdateAdj.ID = "btnUpdateAdj"

Dim tRowHeader As New TableRow
Dim tCell As New TableCell

tRowHeader = GetTableHeaderRow(tableResults)

tCell.BackColor = System.Drawing.Color.White
tCell.HorizontalAlign = HorizontalAlign.Center
tCell.ColumnSpan = 2
tRowHeader.Cells.Add(tCell)
tcell.Controls.Add(btnUpdateAdj)
AddHandler btnUpdateAdj.Click, AddressOf update
 
right - meaning the attributes.add is clientside which i had commented out. i think i did what you're saying and it's still not working. the event never fires.
 
if it helps, the button is causing the page to do a postback.
 
This works exactly as expected:

Code:
Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim btnUpdateAdj As New Button
        btnUpdateAdj.Text = "Update Adjudication"
        btnUpdateAdj.ID = "btnUpdateAdj"
        AddHandler btnUpdateAdj.Click, AddressOf update

        Page.FindControl("Form1").Controls.Add(btnUpdateAdj)



    End Sub

    Public Sub update(ByVal sender As Object, ByVal e As System.EventArgs)

        Response.Write("test")
        Response.End()

    End Sub

End Class

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Thanks for the help Tom. I was starting to think it couldn't be done. I took what you wrote and tested it. Then added in, piece by piece, the rest of my code. It came down to the pageload and the If Not Page.IsPostBack
. Once I took out the condition, it did exactly what it should do.

Here's another question for ya. The page that calls this page uses the window.open method with querystring parameters. How can I repopulate the same window without the querystring params? Cookies not allowed :-(

Thanks again for the help

Trent
 
Let me make sure I understand:

Page1 with "data" ----window.open("page2?data").

You want to pass ASP.NET form data from page1 to page2 without resorting to cookies, JavaScript, or QueryStrings.

If that's right, look at "Server.Transfer()". It's like doing a "focus" change, on the server, from one webform to another. One of its overloads takes a boolean. If the boolean is set to true (if my memory serves), then all of the form data is transferred as well.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
You are on the ball today.

Page1 uses cLink.Attributes.Add("onclick", "window.open('GetTransactionDetails.aspx? to open page2 with all of the data on it (page 2 is the same page I was having trouble with the button). Page2 now has the update event which needs to send data back to a stored procedure and redisplay the updated data (also on page 2). Can't use cookies. If theres a way to do it with a querystring that would work. Would Server.Transfer still work in this case?

Thanks
Trent
 
No... What Server.Transfer("Webform2.aspx",True) does is, in essence, Post the data form Webform1 to Webform2. Webform2's Page_Load event handler could effectively do "Request.Form('textbox1')", to retrieve the value of Webform1's textbox1.

However, the user would see Webform1 "navitate to" Webform2. It wouldn't open a second window.

I guess I don't see the problem.

1. Page1 contains a hyperlink, with QS, to Page2. Is that working?

2. Page2 presumably has a Page_Load method that gets the data, commits the data to the database. Correct?

3. Page2 could redisplay that data, by building dynamic controls and displaying them. Right?

I see no need to do a Server.Transfer() or use cookies or even JavaScript. Note on that: you don't need to use "window.open()" in this case. You can use a standard anchor tag such as:

<a href="Page2.aspx?key=value&key2=value2" target="_blank">Go to details page.</a>

with proper CSS, you can even make that link look and act like a button...

umm, what was the question?



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
1. Yes, that's working fine.
2. Yes, that's correct.
3. No idea how. Here's what I've got going on...

Page 1 opens Page 2 with a querystring
The Page_Load event calls PopulateTransactionDetails()
within the PopulateTransactionDetails() I've got this to populate the data
objReader = objDb.GetTransactionDetails(Request.QueryString("SessionID"), _
Request.QueryString("RecordID"), Request.QueryString("TransactionType"), strMsg)

On the postback, the querystring values are no longer available to me. So I guess the question is how can I repopulate the reader?
 
What is objReader, a DataReader? How can a Reader write data... nevermind. I'll just assume that code does what you want it do.

Get your querystring values EXPLICITLY. I mean declare some strings to hold them. I don't code VB, remember, but something like:

Dim str_RecordID As New String
str_RecordID = Request.QueryString("RecordID")

Then you have that string. You can then use that string to build a label:

Dim lbl_RecordID As New Label
lbl_RecordID.Text = str_RecordID
Page.Controls.Add("lbl_RecordID")

AND use the SAME string to do your database update.

You mention you don't have the querystring "on the postback". What PostBack?

Page1 links to Page2, and the Page_Load of Page2 can parse and store the QueryStrings, create dynamic controls, write to the database, all in one fell swoop.

Wait, I think I just had an "AHA" moment. Must be getting late. Here's what you're doing:

1. Page1 loads.
2. User clicks a link to Page2, with querystring
3. Page 2 needs to display all that querystring data to user, along with a button to confirm/commit that data to db.
4. User clicks that button.
5. You need to write that data to db now.

You don't know how to get the data from the querystring, into a webform.

You just do it. Build a form dynamically the first time Page2 loads. Use my example of adding a dynamic button to the form. You can just as easily add textboxes, radio buttons, dropdown lists, etc out of the querystring data, just as I did in this post with a label. What page2 submits, it will submit that data.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
We're getting much closer to being on the same page.

1. Page1 loads. - DONE
2. User clicks a link to Page2, with querystring - DONE
3. Page 2 needs to display all that querystring data to user, along with a button to confirm/commit that data to db. - DONE
4. User clicks that button. - DONE
5. You need to write that data to db now. - DONE

and
6. refresh Page 2 with the new data and all the controls that were on it prior to submitting.
Remember, page 2 was initially created with parameters from the QS on page 1. Those parameters were used in the dataReader object to populate a table on page 2.

Page 2 is complete with the exception of refreshing itself with the updated data that was sent back to the db.
 
Ah. That pesky Page2. The poor thing has no controls. The controls are either created by Page1 sending it a querystring, or, the user clicking a button and... then what?

Re-create the same controls all over again, in your database update method.

Or get fancy, and write a new method, once that explicitly creates controls based on strings passed to it. Build those string via 1) querystring data and/or 2) webform posted data.

Your Page_Load gathers the QueryString data into strings, and the calls "buildMyControls()". Your update() method receives the posted data, builds new strings, updates the database, and calls "buildMyControls()".

Sorry for not "getting this" the first time. I have my own "coding patterns" and it often takes me a while to comprehend the WHY/WHAT/HOW of other's approaches.

If I've helped you, you can thank me by visiting my site, and clicking some ads!



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Thanks. You've been a huge help.

Your site is cool. Simple, yet elegant. Very nice.

Thanks again
Trent
 
You're welcome. Thinking about this was fun. You could, in your Page_Load method, do a simple test: either you've got data in Request.QueryString or data in Request.Form. Either way, gather the data and build the controls. I'd like to see your app, if possible. Email me a link?



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
I'd like to be able to share with it with you. It's a secure site for Third Party Administrators (health insurance). We provide a solution where you can use a debit card to pay for doctor visits and what not with your card. Instead of dealing with the insurance company for a reimbursement, it comes directly out of your account(pretax amt set up at the beginning of each year)



and the site we're currently building in .net


sorry....not much to see
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top