如何重定向到btnclick调用的函数中的另一个页面。 ASP.NET

时间:2013-04-27 07:09:44

标签: c# asp.net gridview

当我调用一个函数时,我想重定向到另一个页面  这是我的按钮

<button id="test" runat="server" onclick="Button1_Click()"> Button here </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);


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

 }

5 个答案:

答案 0 :(得分:2)

由于您使用常规html按钮,因此在单击时,它将在客户端执行Button1_Click(),而不是在后面的代码中执行。

将按钮更改为:

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

答案 1 :(得分:0)

将函数Button1_Click()绑定到按钮可能会出现问题。尝试检查Button1的OnClick属性,确保它链接到函数Button1_Click()。

答案 2 :(得分:0)

你的Button1_Click函数是正确的,我认为你从对象调用的方式是错误的。

你应该使用

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

 }

或者你应该使用html按钮对象并调用下面的webmethod

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

 }

答案 3 :(得分:0)

试试这个

Response.Redirect("~/Producter/Delete?id="+ ID,false);

答案 4 :(得分:0)

试试这个;

<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" />

还会像这样修复你的代码;

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

 }