Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Saturday, March 31, 2012

Server side functionality

Hi All,

I have an html button on .aspx page.

<INPUT class="sbttn" id="btnComplete0" onclick="onComplete
()" type="button" value="Mark Completed"
name="btnComplete0"
When the user clicks on the html button the OnComplete
function is called.

<script language="javascript">
function onComplete()
{

// Client side code

// Clicking the server control via code
WorkItemForm.btnComplete.click();

}
</script
I need to perform some server side functinality whenever
user clicks the html button. So I place a hidden server
control (id = btnComplete) on the .aspx page. In the
client side code I automatically click the server control
( WorkItemForm.btnComplete.click();).

On the click event of hidden server control, I write the
server side code in .aspx.cs page.

// Code in .aspx.cs page

private void btnComplete_Click(object sender,
System.EventArgs e)
{
// Calling Server Side Code
}

I am using btnComplete server control as an intermediate
to perform server side functionality.

Problem:

I want to get rid of server control.
Is there a way to perform server side functionality
directly without using server control.

I tried document.FormName.Submit();

This doesn't work. Is there some other way to do so?

Thanks & Regards,
-ReetuHi Reetu,

Here's some VB code that might help you. It uses the submit() method. Not sure
why it didn't work for you.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Literal1.Text = "<script>function
onCompleted(){alert('action');document.forms[0].submit();}</script>"
If IsPostBack Then
Label2.Text = "I was posted back at: " & Now.TimeOfDay.ToString
End If
End Sub

<form id="Form1" method="post" runat="server">
<p>
<asp:textbox id="TextBox1" runat="server"></asp:textbox></p>
<p>
<asp:label id="Label2" runat="server"></asp:label></p>
<p>
<asp:label id="Label1" runat="server"></asp:label></p>
<p>
<asp:literal id="Literal1" runat="server"></asp:literal></p>
<p> </p>
<p> </p>
<input class="sbttn" id="btnComplete0" onclick="onCompleted()"
type="button" value="Mark Completed"
name="btnComplete0">
</form
"Reetu" <reetu@.ascentn.com> wrote in message
news:06c901c35c60$5aabcc10$a301280a@.phx.gbl...
Hi All,

I have an html button on .aspx page.

<INPUT class="sbttn" id="btnComplete0" onclick="onComplete
()" type="button" value="Mark Completed"
name="btnComplete0"
When the user clicks on the html button the OnComplete
function is called.

<script language="javascript">
function onComplete()
{

// Client side code

// Clicking the server control via code
WorkItemForm.btnComplete.click();

}
</script
I need to perform some server side functinality whenever
user clicks the html button. So I place a hidden server
control (id = btnComplete) on the .aspx page. In the
client side code I automatically click the server control
( WorkItemForm.btnComplete.click();).

On the click event of hidden server control, I write the
server side code in .aspx.cs page.

// Code in .aspx.cs page

private void btnComplete_Click(object sender,
System.EventArgs e)
{
// Calling Server Side Code
}

I am using btnComplete server control as an intermediate
to perform server side functionality.

Problem:

I want to get rid of server control.
Is there a way to perform server side functionality
directly without using server control.

I tried document.FormName.Submit();

This doesn't work. Is there some other way to do so?

Thanks & Regards,
-Reetu

When your page is loaded, checl the HTML source of your page. Your
hidden button will not be rendered at all.

Instead, you can convert your button to a aspx button and add
Javascript event to it from the code behind using

Add this line of code to your page load event.
ButtonName.Attributes.add("onClick","return onComplete()")

Now when you click on your button it will raise the onclick event on
the client side and will call onComplete Javascript function. Inside
the Javascript if you return false, the form will not be submitted and
the server side event of your button will not fire. If you return
true, form will be submitted to the server and your button's onclick
event will be fired on the server.

--
Ram

"Reetu" <reetu@.ascentn.com> wrote in message news:<06c901c35c60$5aabcc10$a301280a@.phx.gbl>...
> Hi All,
> I have an html button on .aspx page.
> <INPUT class="sbttn" id="btnComplete0" onclick="onComplete
> ()" type="button" value="Mark Completed"
> name="btnComplete0">
> When the user clicks on the html button the OnComplete
> function is called.
> <script language="javascript">
> function onComplete()
> {
> // Client side code
> // Clicking the server control via code
> WorkItemForm.btnComplete.click();
> }
> </script>
>
> I need to perform some server side functinality whenever
> user clicks the html button. So I place a hidden server
> control (id = btnComplete) on the .aspx page. In the
> client side code I automatically click the server control
> ( WorkItemForm.btnComplete.click();).
> On the click event of hidden server control, I write the
> server side code in .aspx.cs page.
> // Code in .aspx.cs page
> private void btnComplete_Click(object sender,
> System.EventArgs e)
> {
> // Calling Server Side Code
> }
> I am using btnComplete server control as an intermediate
> to perform server side functionality.
> Problem:
> I want to get rid of server control.
> Is there a way to perform server side functionality
> directly without using server control.
> I tried document.FormName.Submit();
> This doesn't work. Is there some other way to do so?
> Thanks & Regards,
> -Reetu
Thanks a lot, it works.

Regards,
-Reetu

>--Original Message--
>When your page is loaded, checl the HTML source of your
page. Your
>hidden button will not be rendered at all.
>Instead, you can convert your button to a aspx button and
add
>Javascript event to it from the code behind using
>Add this line of code to your page load event.
>ButtonName.Attributes.add("onClick","return onComplete()")
>Now when you click on your button it will raise the
onclick event on
>the client side and will call onComplete Javascript
function. Inside
>the Javascript if you return false, the form will not be
submitted and
>the server side event of your button will not fire. If
you return
>true, form will be submitted to the server and your
button's onclick
>event will be fired on the server.
>--
>Ram
>"Reetu" <reetu@.ascentn.com> wrote in message
news:<06c901c35c60$5aabcc10$a301280a@.phx.gbl>...
>> Hi All,
>>
>> I have an html button on .aspx page.
>>
>> <INPUT class="sbttn" id="btnComplete0"
onclick="onComplete
>> ()" type="button" value="Mark Completed"
>> name="btnComplete0">
>>
>> When the user clicks on the html button the OnComplete
>> function is called.
>>
>> <script language="javascript">
>> function onComplete()
>> {
>>
>> // Client side code
>>
>> // Clicking the server control via code
>> WorkItemForm.btnComplete.click();
>>
>> }
>> </script>
>>
>>
>> I need to perform some server side functinality
whenever
>> user clicks the html button. So I place a hidden server
>> control (id = btnComplete) on the .aspx page. In the
>> client side code I automatically click the server
control
>> ( WorkItemForm.btnComplete.click();).
>>
>> On the click event of hidden server control, I write
the
>> server side code in .aspx.cs page.
>>
>> // Code in .aspx.cs page
>>
>> private void btnComplete_Click(object sender,
>> System.EventArgs e)
>> {
>> // Calling Server Side Code
>> }
>>
>> I am using btnComplete server control as an
intermediate
>> to perform server side functionality.
>>
>> Problem:
>>
>> I want to get rid of server control.
>> Is there a way to perform server side functionality
>> directly without using server control.
>>
>> I tried document.FormName.Submit();
>>
>> This doesn't work. Is there some other way to do so?
>>
>> Thanks & Regards,
>> -Reetu
>.

Tuesday, March 13, 2012

Server.Mappath

I can't use Server.Mappath() inside a class file. Is there any way to do
this..?Use HttpContext.Current.Server.MapPath.

Hope this helps,

Mun

--
Munsifali Rashid
http://www.munsplace.com/

"Onur Bozkurt" <onur.bozkurt@.softhome.net> wrote in message
news:OIiWZ9t2DHA.1736@.TK2MSFTNGP09.phx.gbl...
> I can't use Server.Mappath() inside a class file. Is there any way to do
> this..?
It is working.
Thanks a lot...
"Onur Bozkurt" <onur.bozkurt@.softhome.net> wrote in message
news:OIiWZ9t2DHA.1736@.TK2MSFTNGP09.phx.gbl...
> I can't use Server.Mappath() inside a class file. Is there any way to do
> this..?
It is working.
Thanks a lot...
"Onur Bozkurt" <onur.bozkurt@.softhome.net> wrote in message
news:OIiWZ9t2DHA.1736@.TK2MSFTNGP09.phx.gbl...
> I can't use Server.Mappath() inside a class file. Is there any way to do
> this..?

server.mappath

In the code behind server.mappath works fine, yet in a class I get 'Server
operation is not available in this context'. System.Web.HttpApplication is
inherited in the class, why is this so?

Colin.Colin,

It's not enough to just inherit it. The page fills many properties of the
class. If you pass the current instance of the HttpApplication object into
your class then you'll be able to access it the way you want.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Colin" <c@...cl> wrote in message
news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
> In the code behind server.mappath works fine, yet in a class I get 'Server
> operation is not available in this context'. System.Web.HttpApplication
> is
> inherited in the class, why is this so?
> Colin.
Ah, I see. Classes are new'ish to me. How did I pass HttpApplication to
the class when instantiated? I'm using Vb.Net.

Colin.

"S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
message news:ehY8jOP4FHA.2600@.tk2msftngp13.phx.gbl...
Colin,

It's not enough to just inherit it. The page fills many properties of the
class. If you pass the current instance of the HttpApplication object into
your class then you'll be able to access it the way you want.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Colin" <c@...cl> wrote in message
news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
> In the code behind server.mappath works fine, yet in a class I get 'Server
> operation is not available in this context'. System.Web.HttpApplication
> is
> inherited in the class, why is this so?
> Colin.
Inside a class you could use:
System.Web.HttpContext.Current.Server.MapPath("~/mydir/")

"Colin" <c@...cl> schreef in bericht
news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
> In the code behind server.mappath works fine, yet in a class I get 'Server
> operation is not available in this context'. System.Web.HttpApplication
> is
> inherited in the class, why is this so?
> Colin.
Colin,

If you always want it to be included then require it as a parameter of your
class' sub new:

Private _HttpApplication

Public Sub New(ByRef httpApplication As System.Web.HttpApplication)
_HttpApplication = httpApplication
End Sub

If you just need it for one method inside of your class declare it as a
parameter of that method itself

Public Sub MyMethod(ByRef httpApplication As System.Web.HttpApplication)

End Sub

You should use ByRef instead of ByVal so that if you store something in the
HttpApplication.Context object or make other changes to it's properties they
will be reflected throughout the application.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Colin" <c@...cl> wrote in message
news:OEeJS0P4FHA.632@.TK2MSFTNGP10.phx.gbl...
> Ah, I see. Classes are new'ish to me. How did I pass HttpApplication to
> the class when instantiated? I'm using Vb.Net.
> Colin.
>
> "S. Justin Gengo" <justin@.[no_spam_please]aboutfortunate.com> wrote in
> message news:ehY8jOP4FHA.2600@.tk2msftngp13.phx.gbl...
> Colin,
> It's not enough to just inherit it. The page fills many properties of the
> class. If you pass the current instance of the HttpApplication object into
> your class then you'll be able to access it the way you want.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Colin" <c@...cl> wrote in message
> news:u2uu60O4FHA.3540@.TK2MSFTNGP10.phx.gbl...
>> In the code behind server.mappath works fine, yet in a class I get
>> 'Server
>> operation is not available in this context'. System.Web.HttpApplication
>> is
>> inherited in the class, why is this so?
>>
>> Colin.
>>
>>

server.mappath

i want use a private collection of fonts. i want to tell the class file using the server.mappath method the location of the fonts folder.
but the class file throws an error when i used the server method there. is there any other ways to do it.

as of now i have avoided the problem by hardcoding it[by passing the physical location as a string]

But i would like to have another solution to my problem.

Try using HttpContext.Current.Server.MapPath

Thanks


That didn't work in the business logic. i added the system.web namespace and use the statement you gave me..but still it wont work

try

using

System.IO;

string

file =Path.Combine(Request.PhysicalApplicationPath,"/fonts/arial.ttf");

string fontlocation = HttpContext.Current.Server.MapPath("./Fonts") + fontName;

i used the above code,but it didnt' work


You cannot use . in the path.

string fontlocation = HttpContext.Current.Server.MapPath("/Fonts" + fontName);

Thanks


I just copied your code and tried to run the application, it threw my System.InvalidOperationException

My fonts folders resides in the application root folder. so how am i supposed to access it from the BLL[business logic layer]


Did you keep a breakpoint and check, where the exception was raised?

The above code is how you access the server.mappath in class file. (Make sure you are writing code in C# as the above code is in C#

Thanks


try

string fontsLocation = Request.ApplicationPath + "/Fonts";

Happy Coding


as i had said in my previous post, it threw, System.InvalidOperationException.

and yes i am writing them in C#.


Keep a breakpoint and check where it is throwing the xception.

Thanks


Yes it does, it did threw the exception i mentioned above sonow i have my coding in a try catch block.

can you post your complete code?

Thanks


PrivateFontCollection FontCollection =new PrivateFontCollection();try {string fontlocation =this.fontPath+"Fonts/"+this.FamilyName +".ttf";//string fontlocation = HttpContext.Current.Server.MapPath("./Fonts") + fontName; FontCollection.AddFontFile(fontlocation); FontFamily fontFamily = FontCollection.Families[0];this.imgFont =new Font(fontFamily,this.FontSize, style, GraphicsUnit.Pixel); }catch (System.IO.FileNotFoundException ex2) {this.familyName = System.Drawing.FontFamily.GenericSerif.Name;this.imgFont =new Font(this.FamilyName,this.FontSize, style, GraphicsUnit.Pixel); }

Server.MapPath Failing When Moved to Class Module

In a VB/ASP.NET project, the following lines work as advertised when placed
in a code-behind file (whatever.aspx.vb) - meaning that I get the full
physical path to MyDatabase.MDB in the variable PathToMDB. However, when the
same two lines are moved to a separate class file (whatever.vb), the project
fails to build - with the message "Name 'Server' is not declared."

Dim PathToMDB As String
PathToMDB = Server.MapPath("/MySite/db/") + "MyDatabase.mdb"

Why is this? What can I do about it? I need to obtain the physical path to
the MDB file from within the class file.

Thanks in advance."Jeff S" <JeffS@.asdf.net> wrote in message
news:uloQjlvoDHA.1928@.TK2MSFTNGP12.phx.gbl...
> In a VB/ASP.NET project, the following lines work as advertised when
placed
> in a code-behind file (whatever.aspx.vb) - meaning that I get the full
> physical path to MyDatabase.MDB in the variable PathToMDB. However, when
the
> same two lines are moved to a separate class file (whatever.vb), the
project
> fails to build - with the message "Name 'Server' is not declared."
> Dim PathToMDB As String
> PathToMDB = Server.MapPath("/MySite/db/") + "MyDatabase.mdb"
>
> Why is this? What can I do about it? I need to obtain the physical path to
> the MDB file from within the class file.
> Thanks in advance.

Try
HttpContext.Current.Server.MapPath("/MySite/db/") + "MyDatabase.mdb"

--
[[((hillarie))]]

To reply by email, remove ".SPAMBLOCK" from email address
Try it in this format:
MyConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
server.mappath("/") & "../data/myDB.mdb;"

David Wier
MCP, MVP ASP.NET, ASPInsider
http://aspnet101.com
http://aspexpress.com

"Jeff S" <JeffS@.asdf.net> wrote in message
news:uloQjlvoDHA.1928@.TK2MSFTNGP12.phx.gbl...
> In a VB/ASP.NET project, the following lines work as advertised when
placed
> in a code-behind file (whatever.aspx.vb) - meaning that I get the full
> physical path to MyDatabase.MDB in the variable PathToMDB. However, when
the
> same two lines are moved to a separate class file (whatever.vb), the
project
> fails to build - with the message "Name 'Server' is not declared."
> Dim PathToMDB As String
> PathToMDB = Server.MapPath("/MySite/db/") + "MyDatabase.mdb"
>
> Why is this? What can I do about it? I need to obtain the physical path to
> the MDB file from within the class file.
> Thanks in advance.

server.mappath in a vb class

If I use server.mappath to get to the root dir of my application from within
codebehind, it works fine. I also want to use this from a vb class I have in
my application, and I think I have an issue with context because
server.mappath does not work there.
I was thinking I'd just need to be more specific with the namespace and
tried HttpServerUtility.MapPath() (found this in help) but if I try to use
that directly, Intellisense won't accept it saying "Reference to a
non-shared member requires an object reference." so I declared a variable as
a HttpServerUtility, but that fails at runtime with a null object reference
error.
So all I really want to do is be able to refer to my application root folder
from a vb class in my asp.net application. Not sure if I'm close in my
approach above, but if someone could give me a hint on how to do this I'd
really appreciate it. I just want it to work like Server.MapPath in a
codebehind page. Thanks!
MattServer and HttpServerUtility are equivalent overall. The best way to use it
is to create a HttpServerUtility object and pass it in to your class file.
Server is an instantiated copy of HttpServerUtility; you just do not see the
instantiation.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
****************************************
********
Think Outside the Box!
****************************************
********
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2pgv13Fknc98U1@.uni-berlin.de...
> If I use server.mappath to get to the root dir of my application from
within
> codebehind, it works fine. I also want to use this from a vb class I have
in
> my application, and I think I have an issue with context because
> server.mappath does not work there.
> I was thinking I'd just need to be more specific with the namespace and
> tried HttpServerUtility.MapPath() (found this in help) but if I try to use
> that directly, Intellisense won't accept it saying "Reference to a
> non-shared member requires an object reference." so I declared a variable
as
> a HttpServerUtility, but that fails at runtime with a null object
reference
> error.
> So all I really want to do is be able to refer to my application root
folder
> from a vb class in my asp.net application. Not sure if I'm close in my
> approach above, but if someone could give me a hint on how to do this I'd
> really appreciate it. I just want it to work like Server.MapPath in a
> codebehind page. Thanks!
> Matt
>
Thanks for the response!
If possible I'd like to not have to pass it in. Is there anyway I can
instantiate it in my vb class so I don't need to modify code that calls this
class elsewhere?
Cowboy (Gregory A. Beamer) [MVP] wrote:
> Server and HttpServerUtility are equivalent overall. The best way to
> use it is to create a HttpServerUtility object and pass it in to your
> class file. Server is an instantiated copy of HttpServerUtility; you
> just do not see the instantiation.
>
> ****************************************
********
> Think Outside the Box!
> ****************************************
********
> "MattB" <somedudeus@.yahoo.com> wrote in message
> news:2pgv13Fknc98U1@.uni-berlin.de...
You don't need to instantiate it. You need to reference it. It already
exists in the HttpContext of the Current Request:
string s = System.Web.HttpContext.Current.Server.MapPath(url);
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2pgvuuFjph3aU1@.uni-berlin.de...
> Thanks for the response!
> If possible I'd like to not have to pass it in. Is there anyway I can
> instantiate it in my vb class so I don't need to modify code that calls
this
> class elsewhere?
> Cowboy (Gregory A. Beamer) [MVP] wrote:
>
>

server.mappath in a vb class

If I use server.mappath to get to the root dir of my application from within
codebehind, it works fine. I also want to use this from a vb class I have in
my application, and I think I have an issue with context because
server.mappath does not work there.
I was thinking I'd just need to be more specific with the namespace and
tried HttpServerUtility.MapPath() (found this in help) but if I try to use
that directly, Intellisense won't accept it saying "Reference to a
non-shared member requires an object reference." so I declared a variable as
a HttpServerUtility, but that fails at runtime with a null object reference
error.

So all I really want to do is be able to refer to my application root folder
from a vb class in my asp.net application. Not sure if I'm close in my
approach above, but if someone could give me a hint on how to do this I'd
really appreciate it. I just want it to work like Server.MapPath in a
codebehind page. Thanks!

MattServer and HttpServerUtility are equivalent overall. The best way to use it
is to create a HttpServerUtility object and pass it in to your class file.
Server is an instantiated copy of HttpServerUtility; you just do not see the
instantiation.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"MattB" <somedudeus@.yahoo.com> wrote in message
news:2pgv13Fknc98U1@.uni-berlin.de...
> If I use server.mappath to get to the root dir of my application from
within
> codebehind, it works fine. I also want to use this from a vb class I have
in
> my application, and I think I have an issue with context because
> server.mappath does not work there.
> I was thinking I'd just need to be more specific with the namespace and
> tried HttpServerUtility.MapPath() (found this in help) but if I try to use
> that directly, Intellisense won't accept it saying "Reference to a
> non-shared member requires an object reference." so I declared a variable
as
> a HttpServerUtility, but that fails at runtime with a null object
reference
> error.
> So all I really want to do is be able to refer to my application root
folder
> from a vb class in my asp.net application. Not sure if I'm close in my
> approach above, but if someone could give me a hint on how to do this I'd
> really appreciate it. I just want it to work like Server.MapPath in a
> codebehind page. Thanks!
> Matt
Thanks for the response!
If possible I'd like to not have to pass it in. Is there anyway I can
instantiate it in my vb class so I don't need to modify code that calls this
class elsewhere?

Cowboy (Gregory A. Beamer) [MVP] wrote:
> Server and HttpServerUtility are equivalent overall. The best way to
> use it is to create a HttpServerUtility object and pass it in to your
> class file. Server is an instantiated copy of HttpServerUtility; you
> just do not see the instantiation.
>
> ************************************************
> Think Outside the Box!
> ************************************************
> "MattB" <somedudeus@.yahoo.com> wrote in message
> news:2pgv13Fknc98U1@.uni-berlin.de...
>> If I use server.mappath to get to the root dir of my application
>> from within codebehind, it works fine. I also want to use this from
>> a vb class I have in my application, and I think I have an issue
>> with context because server.mappath does not work there.
>> I was thinking I'd just need to be more specific with the namespace
>> and tried HttpServerUtility.MapPath() (found this in help) but if I
>> try to use that directly, Intellisense won't accept it saying
>> "Reference to a non-shared member requires an object reference." so
>> I declared a variable as a HttpServerUtility, but that fails at
>> runtime with a null object reference error.
>>
>> So all I really want to do is be able to refer to my application
>> root folder from a vb class in my asp.net application. Not sure if
>> I'm close in my approach above, but if someone could give me a hint
>> on how to do this I'd really appreciate it. I just want it to work
>> like Server.MapPath in a codebehind page. Thanks!
>>
>> Matt
You don't need to instantiate it. You need to reference it. It already
exists in the HttpContext of the Current Request:

string s = System.Web.HttpContext.Current.Server.MapPath(url) ;

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"MattB" <somedudeus@.yahoo.com> wrote in message
news:2pgvuuFjph3aU1@.uni-berlin.de...
> Thanks for the response!
> If possible I'd like to not have to pass it in. Is there anyway I can
> instantiate it in my vb class so I don't need to modify code that calls
this
> class elsewhere?
> Cowboy (Gregory A. Beamer) [MVP] wrote:
> > Server and HttpServerUtility are equivalent overall. The best way to
> > use it is to create a HttpServerUtility object and pass it in to your
> > class file. Server is an instantiated copy of HttpServerUtility; you
> > just do not see the instantiation.
> > ************************************************
> > Think Outside the Box!
> > ************************************************
> > "MattB" <somedudeus@.yahoo.com> wrote in message
> > news:2pgv13Fknc98U1@.uni-berlin.de...
> >> If I use server.mappath to get to the root dir of my application
> >> from within codebehind, it works fine. I also want to use this from
> >> a vb class I have in my application, and I think I have an issue
> >> with context because server.mappath does not work there.
> >> I was thinking I'd just need to be more specific with the namespace
> >> and tried HttpServerUtility.MapPath() (found this in help) but if I
> >> try to use that directly, Intellisense won't accept it saying
> >> "Reference to a non-shared member requires an object reference." so
> >> I declared a variable as a HttpServerUtility, but that fails at
> >> runtime with a null object reference error.
> >>
> >> So all I really want to do is be able to refer to my application
> >> root folder from a vb class in my asp.net application. Not sure if
> >> I'm close in my approach above, but if someone could give me a hint
> >> on how to do this I'd really appreciate it. I just want it to work
> >> like Server.MapPath in a codebehind page. Thanks!
> >>
> >> Matt

Server.MapPath in a class file

Hi All!

When I moved some code into a .cs file in the App_Code directory, it brokeServer.MapPath. I understand that this is becauseServer isn't directly available to the .cs file. What I'm wondering is if I can access theServer class that the code calling my .cs file so I can get an accurate physical path. Passing the physical path to the class in my .cs file isn't exactly practical. If I can't access the .aspx pageServer, how else can a .cs file retrieve the physical path of the application page that calls upon it?

Thanks for any help!

Brandon

You can access it from a class file as System.Web.HttpContext.Current.Server.
That's perfect, worked great! Thank you!

Server.MapPath from a vb class

I've used this a bit from codebehind with no problems, but now I'm
having some issues when I try to use it from a vb class in my application.
Looking in help, it looks like I need to do something like this:
dim Server As HttpServerUtility
Dim path As String = Server.MapPath("myfile.txt")
But any time I use that Server variable I get a null refrence exception.
What am I doing wrong? Thanks!
MattYou have to use httpcontext to get the server object when you are on a
class. Hope this helps
snt
http://www.flexoweb.com
Don't write "dim Server As HttpServerUtility"
just
Dim path As String = Server.MapPath("myfile.txt")
Ibrahim ULUDAG
www.ibrahimuludag.com
"MattB" <somedudeus@.yahoo.com> wrote in message
news:3gu0paFedcjhU1@.individual.net...
> I've used this a bit from codebehind with no problems, but now I'm having
> some issues when I try to use it from a vb class in my application.
> Looking in help, it looks like I need to do something like this:
> dim Server As HttpServerUtility
> Dim path As String = Server.MapPath("myfile.txt")
> --
> But any time I use that Server variable I get a null refrence exception.
> What am I doing wrong? Thanks!
> Matt
tint wrote:
> You have to use httpcontext to get the server object when you are on a
> class. Hope this helps
> snt
> http://www.flexoweb.com
>
Yep. HttpContext.Current to be exact. Thanks!
Matt
Great, I had the same problem, HttpContext.Current.Server.MapPath works perf
ect!

Server.MapPath from a vb class

I've used this a bit from codebehind with no problems, but now I'm
having some issues when I try to use it from a vb class in my application.
Looking in help, it looks like I need to do something like this:

dim Server As HttpServerUtility
Dim path As String = Server.MapPath("myfile.txt")

--

But any time I use that Server variable I get a null refrence exception.
What am I doing wrong? Thanks!

MattYou have to use httpcontext to get the server object when you are on a
class. Hope this helps

snt
http://www.flexoweb.com
Don't write "dim Server As HttpServerUtility"
just
Dim path As String = Server.MapPath("myfile.txt")

Ibrahim ULUDAG
www.ibrahimuludag.com

"MattB" <somedudeus@.yahoo.com> wrote in message
news:3gu0paFedcjhU1@.individual.net...
> I've used this a bit from codebehind with no problems, but now I'm having
> some issues when I try to use it from a vb class in my application.
> Looking in help, it looks like I need to do something like this:
> dim Server As HttpServerUtility
> Dim path As String = Server.MapPath("myfile.txt")
> --
> But any time I use that Server variable I get a null refrence exception.
> What am I doing wrong? Thanks!
> Matt
tint wrote:
> You have to use httpcontext to get the server object when you are on a
> class. Hope this helps
> snt
> http://www.flexoweb.com

Yep. HttpContext.Current to be exact. Thanks!

Matt
Great, I had the same problem, HttpContext.Current.Server.MapPath works
perfect!

--
kris kilowatt
----------------------
Posted via http://www.codecomments.com
----------------------