0

I wanna redirect to another page when i call a function here my button

<button id="test" runat="server" onclick="Button1_Click()"> Button here </button>

here my button action

protected void Button1_click(Object sender, EventArgs e)
 {
    int ID = 0;
   Label5.Visible = false;

   ID = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[1].Text);


  // somthing like 
  // Server.Transfer("~/Producter/Delete?id="+ id)
  // OR
  Response.Redirect("~/Producter/Delete?id="+ ID);

 }
Lina Farid
  • 75
  • 1
  • 3
  • 13

5 Answers5

2

since you are using regular html button, when clicked, it will execute Button1_Click() on the client side, instead of in code behind.

Change the button to :

<asp:Button ID="test" runat="server" OnClick="Button1_Click" Text="Button here" />
ajakblackgoat
  • 2,119
  • 1
  • 15
  • 10
0

It could be a problem with the binding of the function Button1_Click() to the button. Try checking the Button1's OnClick property to make sure it links to the function Button1_Click().

Adam92
  • 436
  • 8
  • 23
0

your Button1_Click function is correct, and I think the way you are calling from object is wrong.

you should either use

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

 protected void Button1_click(Object sender, EventArgs e)
 {
 int ID = 0;
 Label5.Visible = false;

 ID = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[1].Text);
 Response.Redirect("~/Producter/Delete?id="+ ID);

 }

or you should use html button object and call a webmethod like below

 <script>
 $(document).ready(function () {
     $('#test').click(function(){
         $.ajax({
                    type: "POST",
                    url: "Profile.aspx/update_phone",
                    data: "{'ProfessionalID':''}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function () { }
                });
     });
 });
 </script>
 <button id="test" runat="server"> Button here </button>

 [webmethod]
 public static void Button1_click(Object sender, EventArgs e)
 {
  ....
  Response.Redirect("~/Producter/Delete?id="+ ID);

 }
Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
0

Try this

Response.Redirect("~/Producter/Delete?id="+ ID,false);
Ali Umair
  • 1,386
  • 1
  • 21
  • 42
0

try this;

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
     $('#test').on('click', function(e){
        e.preventDefault();
         //using jquery, you must import jquery library for it
        $('#buttonHidden').click();
     }
</script>
// your button here to call javascript
<button id="test" runat="server"> Button here </button>
// the hidden button just to hold the Button1_Click which is fired from fireClick()
<asp:Button ID="buttonHidden" runat="server" Style="display: none" OnClick="Button1_Click" />

also fix your code behind like this;

protected void Button1_Click(Object sender, EventArgs e)
 {
    int ID = 0;
   Label5.Visible = false;

   ID = Convert.ToInt32(GridView1.Rows[row.RowIndex].Cells[1].Text);


  // somthing like 
  // Server.Transfer("~/Producter/Delete?id="+ id)
  // OR
  Response.Redirect("~/Producter/Delete?id="+ ID);

 }
Idrees Khan
  • 7,702
  • 18
  • 63
  • 111