1

我需要一个代码段来调用一个显示警报的javascript函数recordInserted(),从我的以下代码后面的方法中,

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");
    }

请帮我解决这个问题。

我尝试了以下但从未工作过,

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

2 回答 2

1

尝试这个:

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

或者尝试在一秒钟后调用 Javascript 函数:

ClientScript.RegisterClientScriptBlock(typeof(Page), "Call your function", "setTimeout('recordInserted()', 1000)", true);
于 2013-09-29T10:12:54.740 回答
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').

于 2013-09-29T10:28:56.860 回答