user1753377
user1753377

Reputation:

How to use properties in an ASP.NET web user control's code behind

This code comes from the .aspx.cs file for a Web User Control the code below gives two errors: the first { under public void Button2_Click says: "} expected". then, the last } in the code says "type or namespace definition, or end-of file expected". how can I make this code better? and how can I actually make it function?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tutorial_Examples_1
{
public partial class UserInfoBoxControl : System.Web.UI.UserControl
{
    public void Page_Load(object sender, EventArgs e)
    {

    }

    public void Button2_Click(object sender, EventArgs e)
    {
        private string userName;
        private int userAge;
        private string userCountry;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        public int UserAge
        {
            get { return userAge; }
            set { userAge = value; }
        }

        public string UserCountry
        {
            get { return userCountry; }
            set { userCountry = value; }
        }            
    }
}
}

Upvotes: 0

Views: 513

Answers (2)

Sami
Sami

Reputation: 3800

using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tutorial_Examples_1
{
public partial class UserInfoBoxControl : System.Web.UI.UserControl
{

private string userName;
private int userAge;
private string userCountry;

public string UserName
{
     get { return userName; }
     set { userName = value; }
}

public int UserAge
{
     get { return userAge; }
     set { userAge = value; }
}

public string UserCountry
{
     get { return userCountry; }
     set { userCountry = value; }
} 

public void Page_Load(object sender, EventArgs e)
{

}

public void Button2_Click(object sender, EventArgs e)
{

}

}//class


}//namespace

Better way is to create a separate class for User like

public class UserInfo
{
private string userName;
private int userAge;
private string userCountry;

public string UserName
{
     get { return userName; }
     set { userName = value; }
}

public int UserAge
{
     get { return userAge; }
     set { userAge = value; }
}

public string UserCountry
{
     get { return userCountry; }
     set { userCountry = value; }
} 

}


public partial class UserInfoBoxControl : System.Web.UI.UserControl
{

    public void Page_Load(object sender, EventArgs e)
    {

    }

    public void Button2_Click(object sender, EventArgs e)
    {
        UserInfo user = new UserInfo();
    //Do you stuff with user object
    }
}

Hope this helps!

Upvotes: 1

uncoder
uncoder

Reputation: 1876

You're nesting member variables and properties within a method body. This is not allowed in C#. That should be done outside that block, and within the class body instead.

Upvotes: 1

Related Questions