0

I am a humble student on a work placement, and I have inherited a 'semi-finished' app. It is written in C#(no experience at all) .NET(a little experience), using AJAX(no experience) and Jquery(little experience). I wrote a little method to grab text from a webpage and parse it into a text document. I want it to fire when the user pushes 'publish'.

The button won't fire. I have put a breakpoint in my 'btnPublish_Click' handler, and it doesn't even reach it.

I've registered the button click event (I read that you needed to do this in C#):

namespace MaintainTenderFiles.ajax
{
    public partial class publish : TendersPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                loadPendingTenders();
                preloadTender();
                btnPublish.Click += new EventHandler(this.btnPublish_Click);
            }
            else
            {
               //more code

Then coded the click event:

    public void btnPublish_Click(object sender, EventArgs e)
    {
        String script = "alert('Clicked!!!');";
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
 "alertScript", script, true);
    }

All that happens when I click the button is it takes me to a page called 'published'.

Why can't I get the event to fire?

EDIT**

Apparently forgot to include the code for the button from the markup page:

 <asp:Button ID="btnPublish" OnClick="btnPublish_Click" 
  CssClass="ui-state-default ui-corner-all" runat="server" 
  Text="Publish" CausesValidation="False" UseSubmitBehavior="False" />
7
  • Writing javascript in the code behind is really not the best practice. the way classic webforms works makes me sick... :( Commented Apr 25, 2012 at 15:49
  • I'm just trying to get an alert to pop when I get the button click to finally work.... I will not have jscript in there. What do you mean about classic webforms making you sick? What should I be doing differently? I am just learning, and am more than interested in best practices going forward. Commented Apr 25, 2012 at 15:56
  • There is nothing wrong with how webforms work. Once you understand the process it makes total sense. Also, there is nothing wrong with registering javscript on the codebehind when your doing ajax like this. Commented Apr 25, 2012 at 16:00
  • 1. Do you just want to execute some client-side code at btnPublish click ? 2. Does your markup file .aspx contain btnPublish button or you generate it dynamically in code behind .aspx.cs ? Commented Apr 25, 2012 at 16:09
  • When the button is clicked, the method grabs info from a populated form and then writes it to an html file, then pushes it into a folder waiting to be pushed to our external servers. I do have a button called btnPublish in my markup file, yes. Commented Apr 25, 2012 at 16:12

1 Answer 1

1

Move the button event register so it registers every load.

namespace MaintainTenderFiles.ajax
{
    public partial class publish : TendersPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnPublish.Click += new EventHandler(this.btnPublish_Click);
            if (Page.IsPostBack == false)
            {
                loadPendingTenders();
                preloadTender();

            }
            else
            {
Sign up to request clarification or add additional context in comments.

11 Comments

I still can't get into the click event.... Not only do I have the little alert box not popping up, it doesn't hit the break point I inserted.... Although I had it inside the first part of the if because the else part is a redirect, so I didn't think it mattered. But, as I said before, best practice... +1 for pointing it out...
You are doing a postback to the same page? Your form is runat server? Instead of assigning your onclick in the codebehind. Try setting it in the html like this <asp:Button id="Button1" OnClick="Button1_Click" runat="server"/>
You said when the button is clicked it transfers to a different page. So its never doing the postback, its instead doing a post to the second page, which is where you should put your code. What does the form tag look like?
Exactly, well except the server side click event wont ever happen on the first page when you post to a second page. The current page is never processed a second time because your instead moving to the second page. In order for the click event to occur it has to postback to itself. You cant bind a click event on the second page because the control doesn't exist there.
The span text wont be available in the next page. If you want to use it on the second page just copy it into a textbox or hidden field with javascript before you post to the next page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.