• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

I can't find an HTML Button with the Controls.FindControl method!

I have an HTML button on my page opposed to an ASP.Net server control as I don't want the form to be posted back when I click on it.  In the form load event I want to add a bit of javascript using the attributes collection e.g.

myButton.attributes.add("OnClick",myScript)

I'm trying to get a reference to the HTML button control using the FindControl method but this can't seem to find the button on my page e.g.

myButton = ctype(me.controls.findcontrol("btnPrint"),HtmlButton))

I'v tried doing...

myButton = ctype(me.controls("Form1").controls.findcontrol("btnPrint"),HtmlButton))

but that doesn't work either, they both return Nothing

So how do I get a reference to an HTML control?
0
useless_eater
Asked:
useless_eater
  • 2
  • 2
2 Solutions
 
tusharashahCommented:
you can make your button as Server control and return false from your JavaScript to prevent Postback.

Try this:

Button1.Attributes.Add("OnClick", "return false;")


-tushar
0
 
useless_eaterAuthor Commented:
I'll try that and report back.  But I'd still like to know if I can find an html control.
0
 
b1xml2Commented:
<button id="btnPrint" runat="server">Print</button>

How are you adding the button? Statically or Dynamically.

If it is statically, then is there any Control that contains the button.


Dim myButton As HtmlButton = CType(FindControl("btnPrint"),HtmlButton)
0
 
useless_eaterAuthor Commented:
I'm adding the control at design time.

Having looked at your example b1xml2 I realise my mistake, I forgot to put the runat=server property in my tag.  It's finding it now.

0
 
b1xml2Commented:
so all is well then =)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now