使用asp:Button将参数传递给函数 [英] pass parameter into a function with asp:Button

查看:468
本文介绍了使用asp:Button将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将参数传递到asp:Button

I'm trying to pass a parameter into a function onClick of an asp:Button

    <asp:Button name='ProductID' onclick="confirm_product_Click" ID="confirmitem" runat="server" Text="accept">

参数是<%=product.ProductId %>.

我不能使用CommandArguments,因为该值像纯文本一样传递.

I cant use CommandArguments because the value is passed like plain text.

我尝试了隐藏输入,但失败了.

I tried with hidden input but it failed.

我还尝试了使用表单操作:

I also tried using form action:

<form method="post" action="?ProductID=<%=product.ProductId %>"> 
<asp:Button name='ProductID' onclick="confirm_product_Click" ID="confirmitem" runat="server" Text="accept">
</form>

,但不会将值发送给函数. 有人可以帮我解决这个问题吗?

but it doesn't send the value to the function. Can someone help me solve this problem?

推荐答案

在您的aspx中

in your aspx

<asp:Button ID="test" runat="server" CommandArgument='<%#Eval("product.ProductId")%>' CommandName="ThisBtnClick" OnClick="MyBtnHandler" />

后面的代码中

in code behind

void MyBtnHandler(Object sender, EventArgs e)
{
   Button btn = (Button)sender;
   switch (btn.CommandName)
   {
      case "ThisBtnClick":
         DoWhatever(btn.CommandArgument.ToString());
         break;
      case "ThatBtnClick":
         DoSomethingElse(btn.CommandArgument.ToString());
         break;
   }
}

这篇关于使用asp:Button将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆