Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 30th, 2007, 08:38 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing Params to User Controls in ASP 2.0 (VB)

Hi all,

Having lots of fun converting my code snippets into user controls, however I've run into a problem. I'm assuming it's something simple, but I just can't figure it out.

If I declaritively specify an attribute for a user control, the user control acts on the ID without any problems (for example, it will pull out a database record using the attribute in the sql in the user control code):

<gc:UserControl ID="TestRecord" recordID="4" />

But if I try to set the same attribute in a page method (e.g. sub Button_Click) like so:

TestRecord.recordID = Args.CommandArgument

the value doesn't pass across to the control.

In the user control, I have set up public (set and get) variables so I don't think that's causing the problem, but if any of you guys can help, that would be great. I'm reasonably new to ASP.Net 2, but I should be able to understand your code if you send any across.

If this description details the problem enough, then great. But if you need to see any code, then please let me know.

GC



Reply With Quote
  #2 (permalink)  
Old April 30th, 2007, 10:18 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Is it possible that the Button_Click fires too late for you? That is, where in the control do you expect the recordID to be available?

When you set it in the markup, it's assigned to the property early in the control's life cycle. However, the Button's Click event fires after the Load event of the control, so you may be setting it too late...

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #3 (permalink)  
Old April 30th, 2007, 11:34 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

That's an excellent point. The button (a linkbutton to be precise) sits on a row in a gridview on the left of the page. So when you click the linkbutton, the user control should display the information related to the selected row. I assumed that the code I exlained in my first post would refresh the user control. If the timing of the button firing is the problem, how would I go about remedying this problem (I have a feeling that it is a timing issue; I tried this method using a session variable, and although the method fired and set the session variable, the user control didn't ick the value up until the second time I hit the same linkbutton).

Could you post a quick example? That would be great!

GC

Reply With Quote
  #4 (permalink)  
Old April 30th, 2007, 11:40 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

As I asked in my previous post: where do you need the recordID in the user control?

What code do you have in the control that uses this value?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #5 (permalink)  
Old April 30th, 2007, 11:52 AM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again,

Thanks for such a quick response.
I don't have the specific code with me at the moment (I'll be able to supply that later on this evening), but in brief:

I have dimmed a string variable called 'adid' right at the top of the control script, and made it publicly available using the 'Public Property' method (as RecordID) and using 'Get' and 'Set'.

The 'adid' value (pulled from the page calling the control) is then used in a 'Page_Load' method as part of an SQL string to populate a repeater-type table.

I could have done all this in master-detail relationship with a gridview/detailsview set up, but I want to re-use the code in this user control elsewhere and I want full control over the styling. This part is the only sticking point.

GC

Reply With Quote
  #6 (permalink)  
Old April 30th, 2007, 12:03 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

In that case, you could solve it in a couple of ways:

1. Create a public method on the control that accepts the record ID and then binds the data. E.g.:

Public Sub BindGrid(ByVal recordID as Integer)
'Do what you previously did in Page_Load for the control here
End Sub

This way, you don't rely on Page_Load (which fires too early; before the Button's Click event is fired and sets the public recordID property) but instead call the binding method explicitly. Calling code (e.g. the page) now looks like this:

TestRecord.BindGrid(Args.CommandArgument)

2. Call the databinding in the property setter. That is, delay calling DataBind (or whatever it is you're using to bind the data) until the value for the property is set:

Public Property RecordId() As Integer
Get
    Return _recordId
End Get
Set(ByVal Value As Integer)
    _recordId = Value
BindData()
End Set
End Property

What option you choose depends on whether you find the second solution a bit awkward (property setters calling methods). Also, in the second scenario, you may need avoid the BindData being called twice; once when the default from the markup is set, and once when the page sets the RecordID explicitly.

Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #7 (permalink)  
Old April 30th, 2007, 12:09 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thanks for that. I think this will solve my problem! Once I've tried it out I'll let you know if it worked. I have a feeling it will though, because, as you rightly said, the 'Page_load' happens before the button click, so that makes sense to me now!

I will let you know what happens later! Fingers crossed!

Thanks again!

GC

Reply With Quote
  #8 (permalink)  
Old April 30th, 2007, 01:29 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You're welcome.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote
  #9 (permalink)  
Old April 30th, 2007, 01:35 PM
Registered User
 
Join Date: Jul 2006
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

It worked!! Thanks for that; that was excellent advice! I've learned another thing about ASP.Net in the process!

I can finally stop pulling my hair out! Thanks again!

GC

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
DropDownList User controls asp.net 2.0 ButtonsBob ASP.NET 1.0 and 1.1 Basics 1 January 9th, 2006 06:18 AM
passing params on an href function call geogomez Javascript How-To 1 November 29th, 2005 06:26 AM
Passing user defined parameters to vb dll Smita VB How-To 0 March 29th, 2004 04:49 AM



All times are GMT -4. The time now is 07:50 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.