Link to home
Start Free TrialLog in
Avatar of CharlieDev
CharlieDevFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of Anurag Agarwal
Anurag Agarwal
Flag of India image

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
Avatar of CharlieDev

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Blomholm
Blomholm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Blomholm
Blomholm

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.
Thanks for your help :)