Showing posts with label system. Show all posts
Showing posts with label system. Show all posts

Saturday, March 31, 2012

Set application variable from a class

Hi
I am trying to set an application variable from a class file in my website.
Currently, the class file inherits from System.Web.UI.Page
I also referenced all the namespaces a normal webform would.
However, when i try save my application variable like such:
Application["test"] = "hello";
I get the following error:
Object reference not set to an instance of an object
How can i set an application variable from a class
It works fine from a webform
TIA
GrantGot It
HttpContext.Current.Application[ ...
"Grant Merwitz" <grant@.workshare.com> wrote in message
news:%23hZBPQ9NGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> I am trying to set an application variable from a class file in my
> website.
> Currently, the class file inherits from System.Web.UI.Page
> I also referenced all the namespaces a normal webform would.
> However, when i try save my application variable like such:
> Application["test"] = "hello";
> I get the following error:
> Object reference not set to an instance of an object
> How can i set an application variable from a class
> It works fine from a webform
> TIA
> Grant
>

Set application variable from a class

Hi

I am trying to set an application variable from a class file in my website.

Currently, the class file inherits from System.Web.UI.Page
I also referenced all the namespaces a normal webform would.

However, when i try save my application variable like such:
Application["test"] = "hello";
I get the following error:
Object reference not set to an instance of an object

How can i set an application variable from a class
It works fine from a webform

TIA

GrantGot It

HttpContext.Current.Application[ ...

"Grant Merwitz" <grant@.workshare.com> wrote in message
news:%23hZBPQ9NGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> I am trying to set an application variable from a class file in my
> website.
> Currently, the class file inherits from System.Web.UI.Page
> I also referenced all the namespaces a normal webform would.
> However, when i try save my application variable like such:
> Application["test"] = "hello";
> I get the following error:
> Object reference not set to an instance of an object
> How can i set an application variable from a class
> It works fine from a webform
> TIA
> Grant

Thursday, March 29, 2012

Set controls to nothing when finished?

Hello,
I recently saw some example code that looked similar to the following...

...
Dim literalBr As New System.Web.UI.LiteralControl()
literalBr.Text = "<br>"
literalBr.ID = "anId"
Panel1.Controls.Add(literalBr)
literalBr = Nothing
...

Is there a reason why the literalBr control was set to nothing after adding it to the panel? Does it cause memory usage to go up if you dont set controls to nothing after creating them programmatically?The garbage collector will take care cleaning up literalBr in this case. Setting literalBr to nothing is a waste of time. SeeProgramming for Garbage Collection for more details.

Set Custom Page properties declaratively

Hi,
I have a base class (which inherits from System.Web.UI.Page) for all the
pages in my application. I have a property defined on this class that I wan
t
to set declaratively in the .aspx (i.e., not in the .aspx.cs) file.
Is there any way to do this declaratively? When I try to use the <@dotnet.itags.org. Page
...> directive, it only allows the predefined attributes to be used.
Thanks,
Richard Brown<script language="CSharp" runat="server">
// put your code in here
</script>
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a horse to water,
but you can't make him think.
"Richard Brown" <Richard Brown@.discussions.microsoft.com> wrote in message
news:70E934C5-8E5D-40C8-ACE0-4A1D2D7587E9@.microsoft.com...
> Hi,
> I have a base class (which inherits from System.Web.UI.Page) for all the
> pages in my application. I have a property defined on this class that I
> want
> to set declaratively in the .aspx (i.e., not in the .aspx.cs) file.
> Is there any way to do this declaratively? When I try to use the <@. Page
> ...> directive, it only allows the predefined attributes to be used.
> Thanks,
> Richard Brown
>
Thanks for replying so quickly. Unfortunately the example you have provided
does not meet my requirements on two counts:
1. This is not declarative. This is code, and I would add this to the
code-behind class.
2. The <script> tags allow the client to define functions, but not run code
.
If the code requires to be run it needs the old-style <% %> breakout.
However this code is then run during the rendering of the page, which is
invoked from the .Net Framework (i.e,. the client does not have control over
when this code is executed).
I was wondering if there is a way of declaratively setting the properties of
the page class before the OnInit (in much the same way that properties are
set in a server control).
Thanks,
Richard Brown
"Kevin Spencer" wrote:

> <script language="CSharp" runat="server">
> // put your code in here
> </script>
> --
> HTH,
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> You can lead a horse to water,
> but you can't make him think.
> "Richard Brown" <Richard Brown@.discussions.microsoft.com> wrote in message
> news:70E934C5-8E5D-40C8-ACE0-4A1D2D7587E9@.microsoft.com...
>
>

Saturday, March 24, 2012

Set focus and cursor at end of text?

I use this code to set focus to a textbox when I load a page:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

I would also like the cursor to be positioned at the end of the text field.
Using only the above code, the cursor is positioned at the beginning of the
textbox even if there are some text present in the field.

How can I position the cusrsor at the end of the text?

OlavOlav,

Here we go:

Create a new function in your <head> tag in the HTML section:

function SetEnd (TB)
{
if (TB.createTextRange)
{
var FieldRange = TB.createTextRange();
FieldRange.moveStart('character', TB.value.length);
FieldRange.collapse();
FieldRange.select();
}
}

Create a new onfocus JavaScript event in your asp text box tag (asp:textBox)
that calls the SetEnd function above:

<asp:textbox id="TextBox1" runat="server" Width="65px"
onfocus="SetEnd(this)">Existing text</asp:textbox
Make sure you keep your existing code that calls the SetFocus function as it
is:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

Don't forget to call the above SetFocus() function in your Body tag on page
load ....( <body onload="SetFocus()"> )

Good luck!

Regards,

Mohammad Samara.

ICS (London) Ltd.

"Olav Tollefsen" <x@.y.com> wrote in message
news:uxrxLYt3DHA.3656@.TK2MSFTNGP11.phx.gbl...
> I use this code to set focus to a textbox when I load a page:
> private void SetFocus(System.Web.UI.Control ctrl)
> {
> string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
> ctrl.ID + "').focus()</SCRIPT>";
> RegisterStartupScript("focus", s);
> }
> I would also like the cursor to be positioned at the end of the text
field.
> Using only the above code, the cursor is positioned at the beginning of
the
> textbox even if there are some text present in the field.
> How can I position the cusrsor at the end of the text?
> Olav

Thursday, March 22, 2012

Set focus to textbox in gridview

Is there an easy way with a gridview so that when the user clicks on edit, the system can automatically set the focus to a particular textbox ?Is this a custom edit box?
It's a template field. I think I have most of the code figured out (I'll post it when I'm back at my desk), but can't get it to fire the first time a user clicks 'Edit' (but it does fire every time after). Also, how do you set it so the entire value in the field is selected ? Thanks.

Set Header Title from within a normal object

Is it possible to set the title for a page within a normal object; e.g.

1using System;
2using System.Web;
3using System.Web.UI;
4using System.Collections;
5using System.Web.UI.WebControls;
6using System.Web.UI.HtmlControls;
78namespace Utils
9{
10// Program start class11public class Misc
12 {
13public static void setSubject(Page page, String subject) {
14 page.Header.Title=subject;
15 }
16}

Compiling it gives me following error:

Utils.cs(16,4): error CS0117: `System.Web.UI.Page' does not contain a definition for `Header' Compilation failed: 1 error(s), 0 warnings

Thanks in advance,

jerous.

isn't it just Page.Title?

You'll need to ensure that the calling page has the runat="server" in it's <title..> tag.


Use page.Title property. It would set your page Title.

Set mode in FormView on Page_Load

I'm trying to set the FormView in insert mode but nothing happens with
this code:
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
FormView1.ChangeMode(FormViewMode.Insert)
End Sub
If I click the New button on the form everything works but not with
code.
Can someone please help me?
Regards,
Stry
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) handles Me.Load
Try this inside your Page_Load:
If (Not IsPostBack) Then
FormView1.DefaultMode = FormViewMode.Insert
End If
Then again, I'm not sure if you're showing enough code.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."
<staeri@.gmail.com> wrote in message
news:1144144823.902803.185090@.i39g2000cwa.googlegroups.com...
> I'm trying to set the FormView in insert mode but nothing happens with
> this code:
> Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> FormView1.ChangeMode(FormViewMode.Insert)
> End Sub
> If I click the New button on the form everything works but not with
> code.
> Can someone please help me?
> Regards,
> S
>

Set mode in FormView on Page_Load

I'm trying to set the FormView in insert mode but nothing happens with
this code:

Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
FormView1.ChangeMode(FormViewMode.Insert)
End Sub

If I click the New button on the form everything works but not with
code.

Can someone please help me?

Regards,

Stry

Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) handles Me.Load
Try this inside your Page_Load:

If (Not IsPostBack) Then
FormView1.DefaultMode = FormViewMode.Insert
End If

Then again, I'm not sure if you're showing enough code.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

<staeri@.gmail.com> wrote in message
news:1144144823.902803.185090@.i39g2000cwa.googlegr oups.com...
> I'm trying to set the FormView in insert mode but nothing happens with
> this code:
> Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> FormView1.ChangeMode(FormViewMode.Insert)
> End Sub
> If I click the New button on the form everything works but not with
> code.
> Can someone please help me?
> Regards,
> S