Link to home
Start Free TrialLog in
Avatar of Contestoas
ContestoasFlag for Norway

asked on

How to run a class method in an aspx file?

I have a recived an old web project, which I have translated from VB to C#.
All works fine except one thing, a dropdownlist which is databound will not run a class method.

I'll try to explain better, I have:

1. SqlDataSource sqlOwner with current select: SELECT SYSTEM_ID, OWNER FROM C_OWNER
2. Dropdownlist ddlOwner which has sqlOwner as DataSourceID and OWNER as DataTextField(see code 2)
3. A Common.cs class which lies under App_Code(see code 3)

The custom binding for the ddlOwner is: Common.GetSelectedDDLValue(Eval("OWNER"), string.Empty)

But when I try to run this code I get an error, see attachment error_msg.png

I manage to run the Common methods from the underlying aspx.cs files but not from the aspx files.
I'm kinda new to this, but I though the reason for the App_Code folder was so that the Classes/Methods in there could be used in the aspx files?
Can anyone help me with this, as I have a deadline soon on this project and need to get it working.

Regards
Marius
2. 
<asp:DropDownList ID="ddlOwner" runat="server" Width="350px"
                            DataSourceID="sqlOwner" AppendDataBoundItems="true" DataTextField="OWNER" DataValueField="SYSTEM_ID"  
                            SelectedValue='<%# Common.GetSelectedDDLValue(Eval("OWNER"), string.Empty) %>' >
                            <asp:ListItem Text="" Value="" />
</asp:DropDownList>
 
3.
public class Common
{
        public static string GetSelectedDDLValue(object evalValue, string notSelectedValue)
        {
           //CODE
        }
}

Open in new window

error-msg.PNG
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Hi,

Is the common.cs file placed in the App_Code directory of your application?

/Carl.
Hi,

Sorry, I meant to ask have You tried using the fully qualified namespace in the aspx file? I.e. yourproject.common.GetSelectedDDLValue?

/Carl.
ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Yes, you will need to use the fully qualified value as carl mentioned. And make sure Common.cs is in the same namespace as your page class. That will resolve the error you are having.
However, it still will not work because you cannot use Eval in that context. Eval expects to evaluate a gridview row is used to decide which row of the data to bind against. In a stand alone dropdown selected value cannot be bound against EACH of the rows, it is just a single value.
Hmm, I must have deleted a line in my answer. Should read:

However, it still will not work because you cannot use Eval in that context. Eval expects to evaluate a column of data or a list. For example a dropdownlist contained inside a gridview. A gridview row is used to decide which row of the data to bind against. In a stand alone dropdown, selectedvalue cannot be bound against EACH of the rows, it is just a single value.
Avatar of Contestoas

ASKER

If I use the fully qualified namespace in the aspx file, I get following error message:

Compiler Error Message: CS0433: The type 'RRProject.Common' exists in both 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\fc33a5d0\24fe9e1\assembly\dl3\2b6c6af7\7a3c0ace_9574c901\RRProject.DLL' and 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\fc33a5d0\24fe9e1\App_Code.gfigoazy.dll'

As I said I took over this project and converted it from VB to C#, but it was working fine before the translation. Wouldnt that mean that the '<%# Common.GetSelectedDDLValue(Eval("OWNER"), string.Empty) %>' was working, since it has not been changed?
I might add that the dropdownlist is inside a Formview in the InsertItemTemplate
Here's the thing:

You are within the ASPX page, so the new class you are declaring is actually a sub-class within the Page class (well, the class to which your Page belongs).

The DataBinder may have trouble locating it directly, since (I believe) does not run in the context of the Page.  You'll have to reference it from the Page:

      Page.Common.GetSelectedDDLValue

     -dZ.
Hi,

Since the original project was in VB.NET and You only translated it to C#, all the markup "should" still work. When You converted the project, did you by any chance convert it into a Web Application Project? If so, you shouldn't use the App_Code folder from the original project because files in that folder will get compiled twice. In a WAP you should rename the App_Code folder to something else to fix it.

Some more information and workarounds on this error can be found here:

http://social.msdn.microsoft.com/forums/en-US/msbuild/thread/aa9f90e8-534d-4e74-b656-99187eb3f281/

/Carl.
Couldnt get it to work by using Page.Common.GetSelectedDDLValue or renaming the App_Code folder, but I used the workaround NazoUK suggested to get it working. Simply have an extra method in the page.aspx.cs file which returns the result of the class method, and run this method in the dropdown list.

Thanks to all who have replied to me:)