1

I need a code segment to call a javascript function recordInserted() which shows up an alert, from my following code behind method,

protected void add_Click(object sender, EventArgs e)
    {
        String gradename = txt_gradename.Text;
        int allocatedclasses = Int32.Parse(txt_allocatedclasses.Text);
        String headid = txt_head_id.Text;
        int numberofstudents = Int32.Parse(txt_numberofstudents.Text);

        db = new DBConnection();
        db.getConnection();
        db.executeUpdateQuery("INSERT INTO Grade (GradeName,AllocatedClasses,GradeHeadID,NumberOfStudents) VALUES ('"+gradename+"','"+allocatedclasses+"','"+headid+"','"+numberofstudents+"')");

//I Need to call it from here before redirecting
        Response.Redirect("AdminReferenceGradeAdd.aspx");
    }

Please helpp me with this.

I have tried the following but never worked,

Page.ClientScript.RegisterStartupScript(this.GetType(),"Call my function","recordInserted()",true);

2 Answers 2

1

This will never work .. beacuse you are saying to redirect.
when you say Response.Redirect every thing which you have prepared to send is not sent,instead response is redirect to a new page.So your client script never reaches to browser. you can use it like this :-

Page.ClientScript.RegisterStartupScript(this.GetType(),"Call my function","recordInserted();window.location.href='wwW.google.com'",true);

use window.location.href to redirect to your page("yourpage.aspx').

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

Comments

1

Try this:

ClientScript.RegisterClientScriptBlock(typeof(Page), "Call your function", "recordInserted()", true);

Or try calling Javascript function after a second:

ClientScript.RegisterClientScriptBlock(typeof(Page), "Call your function", "setTimeout('recordInserted()', 1000)", true);

3 Comments

It didn't work, simply redirected to the aspx page without showing up the alert! :(
@DilukshanMahendra I've edited my answer. Can you try the second method, if have time?
Yeah! Dfntly, Thanks Aycan

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.