Saturday, March 24, 2012

object reference not set to an instance of an object

hi there,

I looked up posts on how to get a javascript confirm on the onclick of a
button to determine whether to run server code and i wrote this on the page
load..

string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
false) return false";
btnEdit.Attributes["onclick"]=jsalert;

however I'm getting the error message in my subject heading. what have i
done wrong? when i start to write it i get intellisense so i know it
recognises btnEdit.Hi,

Make sure in your HTML that your id tag is exactly "btnEdit" with the
correct case. You receive it in intellisense because it is declared in your
code behind which doesn't necessarily mean it is in your .aspx file. Good
luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"louise raisbeck" <louiseraisbeck@.discussions.microsoft.com> wrote in
message news:25C2CE14-0C92-41CC-B349-4F82055E0336@.microsoft.com...
> hi there,
> I looked up posts on how to get a javascript confirm on the onclick of a
> button to determine whether to run server code and i wrote this on the
page
> load..
> string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
> false) return false";
> btnEdit.Attributes["onclick"]=jsalert;
> however I'm getting the error message in my subject heading. what have i
> done wrong? when i start to write it i get intellisense so i know it
> recognises btnEdit.
Try the following:

btnEdit.Attributes.Add("onclick", jsalert);

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"louise raisbeck" <louiseraisbeck@.discussions.microsoft.com> wrote in
message news:25C2CE14-0C92-41CC-B349-4F82055E0336@.microsoft.com...
> hi there,
> I looked up posts on how to get a javascript confirm on the onclick of a
> button to determine whether to run server code and i wrote this on the
page
> load..
> string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
> false) return false";
> btnEdit.Attributes["onclick"]=jsalert;
> however I'm getting the error message in my subject heading. what have i
> done wrong? when i start to write it i get intellisense so i know it
> recognises btnEdit.
hiya. yes. its as btnEdit in both my aspx, my declarations and my code ?

"Ken Dopierala Jr." wrote:

> Hi,
> Make sure in your HTML that your id tag is exactly "btnEdit" with the
> correct case. You receive it in intellisense because it is declared in your
> code behind which doesn't necessarily mean it is in your .aspx file. Good
> luck! Ken.
> --
> Ken Dopierala Jr.
> For great ASP.Net web hosting try:
> http://www.webhost4life.com/default.asp?refid=Spinlight
> If you sign up under me and need help, email me.
> "louise raisbeck" <louiseraisbeck@.discussions.microsoft.com> wrote in
> message news:25C2CE14-0C92-41CC-B349-4F82055E0336@.microsoft.com...
> > hi there,
> > I looked up posts on how to get a javascript confirm on the onclick of a
> > button to determine whether to run server code and i wrote this on the
> page
> > load..
> > string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
> > false) return false";
> > btnEdit.Attributes["onclick"]=jsalert;
> > however I'm getting the error message in my subject heading. what have i
> > done wrong? when i start to write it i get intellisense so i know it
> > recognises btnEdit.
>
yes i did actually try that syntax. same error message:

System.NullReferenceException: Object reference not set to an instance of an
object

"louise raisbeck" wrote:

> hi there,
> I looked up posts on how to get a javascript confirm on the onclick of a
> button to determine whether to run server code and i wrote this on the page
> load..
> string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
> false) return false";
> btnEdit.Attributes["onclick"]=jsalert;
> however I'm getting the error message in my subject heading. what have i
> done wrong? when i start to write it i get intellisense so i know it
> recognises btnEdit.
Hi,

Do you have the runat="server" tag in your btnEdit? Also, where and when
are you getting the error? Are you sure it happens on the
btnEdit.Attributes line? Also I would definitely switch your syntax to what
Kevin showed, that's how I've always done it. Try commenting out the
attributes line and do something else with the button. Like btnEdit.Visible
= True, if this gives you the same error then there is a problem with either
your button tag or your button declaration, if no error then we have
narrowed it down to how you are adding your attribute. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"louise raisbeck" <louiseraisbeck@.discussions.microsoft.com> wrote in
message news:3728851E-41F0-4369-81A1-6181B8FFA60D@.microsoft.com...
> yes i did actually try that syntax. same error message:
> System.NullReferenceException: Object reference not set to an instance of
an
> object
> "louise raisbeck" wrote:
> > hi there,
> > I looked up posts on how to get a javascript confirm on the onclick of a
> > button to determine whether to run server code and i wrote this on the
page
> > load..
> > string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
> > false) return false";
> > btnEdit.Attributes["onclick"]=jsalert;
> > however I'm getting the error message in my subject heading. what have i
> > done wrong? when i start to write it i get intellisense so i know it
> > recognises btnEdit.
Well, Louise, you have 2 possibilities:

btnEdit is Null;
btnEdit.Attributes is null;

What exactly IS "btnEdit?"
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"louise raisbeck" <louiseraisbeck@.discussions.microsoft.com> wrote in
message news:3728851E-41F0-4369-81A1-6181B8FFA60D@.microsoft.com...
> yes i did actually try that syntax. same error message:
> System.NullReferenceException: Object reference not set to an instance of
an
> object
> "louise raisbeck" wrote:
> > hi there,
> > I looked up posts on how to get a javascript confirm on the onclick of a
> > button to determine whether to run server code and i wrote this on the
page
> > load..
> > string jsalert = "javascript:if(confirm ('Is the employee a rehire?') ==
> > false) return false";
> > btnEdit.Attributes["onclick"]=jsalert;
> > however I'm getting the error message in my subject heading. what have i
> > done wrong? when i start to write it i get intellisense so i know it
> > recognises btnEdit.

0 comments:

Post a Comment