C# asp.net-web user control pass data

Hi,
I am having troubles passing data to a web user control from an aspx page.
I have a dropdown list of projects which is populated through a database, i want to pass the selected projects ID to the web control page but I cant see how to get the value, or i've done something else wrong.
Does anyone know what I need to change to get the projectID in the control page?

Thanks

control.ascx file
******************************************************************************************************************
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ProjectViewTableControl.ascx.cs" Inherits="ProjectViewTableControl" %>
 
control.ascx.cs file
******************************************************************************************************************
public partial class ProjectViewTableControl : System.Web.UI.UserControl
{
    private Guid projectID;
 
    public Guid ProjectID
    {
        get { return projectID; }
        set { projectID = Guid value; }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
      
 
        DBConnect.OpenConnection();
 
        SqlCommand commandviewprojects = new SqlCommand("exec usp_GetProjectView @ProjectID", DBConnect.conn);
 
        commandviewprojects.Parameters.Add("ProjectID", SqlDbType.UniqueIdentifier);
        commandviewprojects.Parameters["ProjectID"].Value = ProjectID;
 
        gvwProject.DataSource = commandviewprojects.ExecuteReader();
 
        gvwProject.DataBind();
 
        DBConnect.CloseConnection();
    }
    
  }
 
project.aspx file
******************************************************************************************************************
<%@ Register TagPrefix="ProjectViewTableControl" TagName="ProjectViewTable" src="ProjectViewTableControl.ascx"%>
 
 
 <asp:DropDownList ID="ClientList" runat="server" OnSelectedIndexChanged="ClientListSelected" AutoPostBack="true" >
    </asp:DropDownList>
     <asp:DropDownList ID="ProjectList" runat="server" OnSelectedIndexChanged="ProjectListSelected" AutoPostBack="true" >
    </asp:DropDownList>
    
 
        
        <ProjectViewTableControl:ProjectViewTable ID="ProjectViewTable1"  runat="server" />
 
project.aspx.cs
******************************************************************************************************************
 SqlCommand ProjectCommand = new SqlCommand("exec usp_GetProjectForClient @ClientID", DBConnect.conn);
 
        ProjectCommand.Parameters.Add("ClientID", SqlDbType.UniqueIdentifier);
        ProjectCommand.Parameters["ClientID"].Value = ClientID;
 
        SqlDataAdapter projectadapter = new SqlDataAdapter(ProjectCommand);
 
         DataSet projectds = new DataSet();
 
         projectadapter.Fill(projectds);
 
         ProjectList.DataTextField = "ProjectName";
         ProjectList.DataValueField = "ProjectID";
         ProjectList.DataSource = projectds;
         ProjectList.DataBind();
 
         DBConnect.CloseConnection();
 
        
    }
 
    public void ProjectListSelected(object sender, EventArgs e)
    {
        string stringProjectID = ProjectList.SelectedValue;
        Guid ProjectID = new Guid(stringProjectID);
        ProjectViewTable1.ProjectViewTableGuid = new Guid(ProjectList.SelectedValue);
    }

Open in new window

CharlieDevAsked:
Who is Participating?
 
BlomholmCommented:

//First change the property ProjectId to this:
 
 public Guid ProjectID
    {
        get { return projectID; }
        set { projectID = value; }
    }
 
 
//Then change ProjectListSelected to :
public void ProjectListSelected(object sender, EventArgs e)
    {
        string stringProjectID = ProjectList.SelectedValue;
        Guid ProjectID = new Guid(stringProjectID);
        ProjectViewTable1.ProjectID = ProjectID;
    }
 
//Should work

Open in new window

0
 
Anurag AgarwalPython DeveloperCommented:
you have defined the property name in ProjectViewTableControl user control for Project Id as "ProjectID"
but in project.aspx.cs in function ProjectListSelected you are accesing
 ProjectViewTable1.ProjectViewTableGuid = new Guid(ProjectList.SelectedValue);
where this  "ProjectViewTableGuid " is defined??
Anurag
0
 
CharlieDevAuthor Commented:
lol, ProjectViewTableGuid is not defined anywhere, I was trying to use some code that someone else advised me to use on here, i did try and use ProjectViewTable where i am now using ProjectViewTableGuid . I dont really know what i'm doing!! hence me asking for help!!

So what should I be using instead of ProjectViewTableGuid , or where should i be defining it?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Anurag AgarwalPython DeveloperCommented:
you should use "ProjectID" instead of "ProjectViewTableGuid "
also move the code written in page load function of  "ProjectViewTableControl " to some public function and call that function from function ProjectListSelected in project.aspx.cs after assigning the projectId property.
Anurag
0
 
BlomholmCommented:
Yes, page_load gets called before your ProjectID gets a value, which is no good =)
so the code currently in page_load needs to execute after you have set the projectId.
0
 
CharlieDevAuthor Commented:
Thanks for your help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.