CharlieDev
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
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);
}
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?
So what should I be using instead of ProjectViewTableGuid , or where should i be defining it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
so the code currently in page_load needs to execute after you have set the projectId.
ASKER
Thanks for your help :)
but in project.aspx.cs in function ProjectListSelected you are accesing
ProjectViewTable1.ProjectV
where this "ProjectViewTableGuid " is defined??
Anurag