0

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

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

 }
4

5 回答 5

2

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

将按钮更改为:

<asp:Button ID="test" runat="server" OnClick="Button1_Click" Text="Button here" />
于 2013-04-27T07:47:50.963 回答
0

函数 Button1_Click() 与按钮的绑定可能存在问题。尝试检查 Button1 的 OnClick 属性以确保它链接到函数 Button1_Click()。

于 2013-04-27T08:17:13.653 回答
0

尝试这个

Response.Redirect("~/Producter/Delete?id="+ ID,false);
于 2013-04-29T05:29:54.030 回答
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);

 }
于 2013-04-29T05:57:25.207 回答
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);

 }
于 2013-04-27T08:21:13.983 回答