Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 17th, 2004, 12:12 PM
Registered User
 
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing a Value to a User Control

I have a user control named uc.ascx and an ASP.NET page named detail.aspx. Both of which are using codebehind, if it makes a difference.

In the aspx page I have the Register directive:
Code:
<%@ Register TagPrefix="Employees" TagName="uc" Src="uc.ascx" %>
and
Code:
<Employees:uc id="uc" Runat="server" />
In the user control, I have a dropdownlist named "MySelect", and in the codebehind, a string variable named "MySelectValue".

In the aspx page, I have a value that will determine the selected value of MySelect.

Here are some code snippets:

detail.aspx.cs:
Code:
private void Page_Load(object sender, System.EventArgs e) {
uc.MySelectValue = "SomeValue";
}
uc.ascx.cs:
Code:
public class uc : System.Web.UI.UserControl {
    public System.Web.UI.WebControls.DropDownList MySelect;

    public string MySelectValue {
        get {return MySelect.SelectedValue;}
        set {MySelect.SelectedValue = MySelectValue;}
    }
}

When I build the solution, I receive a CS0120 error ("An object reference is required for the nonstatic field, method or property 'xxx.Employees.uc.MySelectValue'") on the 'uc.MySelectValue = "SomeValue";' line in detail.aspx.cs.

I've tried everything I can think of, and I am at a total loss. I just can't figure this one out.
Thanks for your help.

 
Old February 17th, 2004, 04:30 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Did you add a declaration for the User Control in your code behind?

Add the following:

protected Your.Name.Space.YourControl uc;

There is a bug in VS, so it doesn't add the declaration in the Code Behind automatically.....

Cheers,

Imar



---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old February 17th, 2004, 06:38 PM
Registered User
 
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much! I've been struggling with this for three days!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a DataSource to a user control hpox ASP.NET 2.0 Professional 1 January 8th, 2007 11:04 AM
passing parameters to user control jbeynon ASP.NET 2.0 Professional 0 August 30th, 2006 06:18 AM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Passing as object as a Param to a Web User Control MixedCode General .NET 2 December 8th, 2004 11:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.