加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 使用asp:Button将参数传递给函数

发布时间:2020-12-16 03:38:26 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试将参数传递给一个函数onClick的一个asp:Button asp:Button name='ProductID' onclick="confirm_product_Click" ID="confirmitem" runat="server" Text="accept" 参数是%= product.ProductId%. 我不能使用CommandArguments,因为该值像纯文本一样
我正在尝试将参数传递给一个函数onClick的一个asp:Button

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

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

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

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

我也尝试过使用表单操作:

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

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读