pass parameter into a function with asp:Button

大城市里の小女人 提交于 2019-12-10 03:53:57

问题


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

the parameter is <%=product.ProductId %>.

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?


回答1:


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


来源:https://stackoverflow.com/questions/30461086/pass-parameter-into-a-function-with-aspbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!