1

I tried

Protected Sub btn_add_question_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_add_question.Click
        frm_course.Visible = False
        question_div.Visible = True
        ScriptManager.RegisterClientScriptBlock(btn_add_question, Me.GetType(), "BlockName", "alert('hello world');", True)
    End Sub

and

Protected Sub btn_add_question_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_add_question.Click
        frm_course.Visible = False
        question_div.Visible = True
        Page.ClientScript.RegisterStartupScript(Me.GetType, "Javascript", "alert('hello')")
    End Sub

But, alert message is not showing. Need Help !!

 <asp:Button ID="btn_add_question" runat="server" Text="Next" CssClass="btn_submit" Width="101px" />
5
  • 1
    Use view source on the resultant page and search for alert -- how does it look? Commented Jul 24, 2012 at 18:38
  • Also, please post the asp code for the button, that might have an error too. Commented Jul 24, 2012 at 18:39
  • didn't found any alert Commented Jul 24, 2012 at 18:42
  • please check, i have updated the button code in my post Commented Jul 24, 2012 at 18:43
  • If you don't see alert in the source then this button is not being rendered. Are you sure this control isn't inside another control that is being "hidden" Commented Jul 24, 2012 at 18:59

2 Answers 2

3

You need to add the <script> tags to the function call by passing True as the last parameter to RegisterStartupScript as so:

Page.ClientScript.RegisterStartupScript(Me.GetType, "Javascript", "alert('hello');",True)

UPDATE:

Try this:

ScriptManager.RegisterStartupScript(Me.GetType, "Javascript", "alert('hello');",True)
Sign up to request clarification or add additional context in comments.

5 Comments

Not helpful, same as it was earlier.
i have noticed one thing that, when i commented the frm_course.Visible = False then it showing the alert box, now why i don't know, anybody please explain
@coders That doesn't make sense. There should be something else going on. Are you using any sort of UpdatePanel/ScriptManager in your page? I am confident that any of my 2 options will work on pages with updatepagel/scriptmanager and pages without them.
yes i accept, your solution is working, but only when i put frm_course.Visible = False line inside the comment. That is why, i'm confused, why is that so.
I have the feeling frm_course is your main form and RegisterStartupScript is adding the script inside this form. Since it's invisible is not rendering anything and your script is not firing.
-2

add an OnClientClick handler to the button or, better yet, attach the handler through javascript where it should be.

<asp:button id="myButton" runat="server" text="Postback" onclientclick="alert('hello world');" />

if all you are doing is changing some visibility of some elements, you shouldn't even be doing a post back at all. just use an input type="button" and show/hide through javascript.

<input type="button" value="Next" id="btn_add_question" />

document.getElementById('btn_add_question').onclick = function () {
    document.getElementById('frm_course').style.visibility = 'hidden';
    document.getElementById('question_div').style.visibility = 'visible';
    alert('hello world');
};

5 Comments

this is vb Handles btn_add_question.Click does this on the server side, you are talking about javascript
@Hogan you make a pointless postback to do things you could just do in javascript, it's bad for performance from a code and latency perspective.
What you say may be true. HOWEVER, THIS IS STILL THE POSTERS QUESTION. Thus the downvote. There are any number of times I had to post an alert to a page from code behind which could not be done any other way.
@Hogan the answer i posted accomplishes exactly the same functionality as what the OP is trying to accomplish, without a postback. how is that a bad thing?
Have some imagination there are any number of cases (which have not been simplified to post here) which require the code he is showing.

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.