1

I have a problem that I want to call the javascript code, when selection of gridview is changing. But, I can not start my javascript. How can I do that?

//Html side

<input ID="addressinput" type="text"  runat="server" style="display:none;"/>

<asp:Button ID="Button1" runat="server" Text="Button" style="display:none;" OnClientClick="return myfunction();" onclick="Button1_Click"  />

//Javascript

function myfunction() {
    FindLocaiton();
}

//C#

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    addressinput.Value = GridView1.SelectedRow.Cells[1].Text;
     Button1.Click += new EventHandler(Button1_Click);
}

protected void Button1_Click(object sender, EventArgs e)
{

}
2
  • When you change the selected index in your GridView a postback occurs, right? so you want the JavaScript code to run after the postback? Commented Nov 3, 2013 at 10:19
  • Yeah exactly. I tried it but I could not write. Commented Nov 3, 2013 at 10:23

2 Answers 2

3

As per following code java script function myfunction() will execute first then the server side button click event.... so u need to add following code in button click event to execute javascript code after server side code..

ClientScript.RegisterStartupScript(this.GetType(), "msg", "myfunction();")

Following is the code that i have tested and its working.

ASPX Code.

<script type="text/javascript">
    function myfunction() {

        alert('In JavaScript Code after server side code execution');
    }
</script>

   <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

C# Code :-

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("In Button Click event");
        ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script language=javascript>myfunction();</script>");
    }
Sign up to request clarification or add additional context in comments.

3 Comments

and do n't specify OnClientClick="return myfunction();" otherwise onclientclick will execute prior to sever side click event...
It did not work. I can click my button but I think it did not affect the javascript code.
I have modified answer and following code is tested and its working...please let me know if u need any other help...
1

Place a literal with runat server and fill this literal with the javascript serverside within a stringbuilder

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.