Solved

Passing User Controls By Reference From a Stand Alone Class File in C#.net

Posted on 2007-04-04
6
1,054 Views
Last Modified: 2013-12-16
Hi there,

Hoping someone can tell me if this is even possible.  I'm having trouble passing a user control by ref .  
Heres the scenario.:

File 1 : General_Functions.cs  (located in the App_Code folder)
------------------------------------------------------------------------------
public class General_Functions
{
        :
        :
        public void myFunc(ref UserControl uc){
              // blah blah blah
        }
}


File 2 : MyPage.aspx.cs  
------------------------------------------------------------------------------

        General_Functions gf = new General_Functions();
        gf.MyFunc(ref my_uc);   // my_uc is some user control on my page


PROBLEM :  baseicallly, the compiler continues to tell me that my syntax is incorrect. the error message is :

         cannot convert from 'ref ASP.user_controls_sample_uc_ascx' to 'ref System.Web.UI.UserControl'      

I clearly get that somehow it doesn't see the two as the same but i don't understand why.  Isn't an .ascx file a USER CONTROL ? So why won't it let me pass in an instance (by ref) of the user control that I am using on MyPage.aspx ?  Please HELP. I've been staring this down all day and I've run out of ideas.
0
Comment
Question by:sbelgrave
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Bill-Hanson
ID: 18851210
You might be able to cast the parameter to the right datatype:

gf.MyFunc(ref (System.Web.UI.UserControl)my_uc);

You would only be able to use properties and methods from UserControl inside of myFunc, though.
0
 

Author Comment

by:sbelgrave
ID: 18851481
I tried your suggestion but it looks like it still gives error.  The error when i try to cast it as a UserControl is :  

"A ref or out argument must be an assignable variable"  

My thinking would be that this can't work because when you're passing by ref you are 'pointing' to that object so it makes no difference what you cast it as, it just wants to know what to point to when you write 'ref X'  . But of course, that could be completely wrong. Any other suggestions ?
0
 
LVL 6

Accepted Solution

by:
dash420 earned 500 total points
ID: 18857169
This works out for me. I used web control in aspx page. let say Default.aspx.
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="uc" TagName="ucName" Src="~/MyUserControl.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
 
</head>
<body>
    <form id="form1" runat="server">
    <div>
     
        <uc:ucName ID="UCControl" runat="server" />
               
    </div>
    </form>
</body>
</html>

Code Behind c# file
---------------------

General_Functions gf = new General_Functions();

UserControl uc = (UserControl)UCControl;

gf.myFunc(ref uc);  

This building perfectly without any compliation error. Try this it working or not.

Regards,
Dash





0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:sbelgrave
ID: 18857659
Hey Dash,

Maybe my example above wasn't clear enough.  Referencing a user control from a code behind page is not the problem.  The two files i have in my example above show a code behind page (MyPage.aspx.cs  ) and a Stand ALONE cs class defined in the App_Code directory.  The goal is to try and pass in a user control located on the MyPage.aspx page.  So in the code behind for MyPage.aspx I need to be able to pass the user control by reference to the method located in the App_Code folder . I hope that makes this clearer. Thank you for looking into it.
0
 

Author Comment

by:sbelgrave
ID: 18858414
My apologies Dash,

While youre example didn't exactly mimic my situation it was completely correct in what to do.  Apparently if you have a user contol on your aspx page it is not really of type UserContol unless you cast it as another variable like you did in your code above. While I find that really strange it is clearly A SOLUTION to the problem and I glady accept it. Thank you! - Scott
0
 
LVL 6

Expert Comment

by:dash420
ID: 18874233
Good to know problem got Solved.
Thanks
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

726 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