?
Solved

C# asp.net-web user control pass data

Posted on 2009-02-11
6
Medium Priority
?
991 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:CharlieDev
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Anurag Agarwal
ID: 23609475
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
 

Author Comment

by:CharlieDev
ID: 23609521
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
 
LVL 3

Accepted Solution

by:
Blomholm earned 1000 total points
ID: 23609602

//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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Assisted Solution

by:Anurag Agarwal
Anurag Agarwal earned 1000 total points
ID: 23609604
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
 
LVL 3

Expert Comment

by:Blomholm
ID: 23609650
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
 

Author Closing Comment

by:CharlieDev
ID: 31545478
Thanks for your help :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question