如何使用参数创建一个linkbutton onclick [英] How to make a linkbutton onclick with parameters

查看:151
本文介绍了如何使用参数创建一个linkbutton onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML:

 < asp:LinkBut​​ton ID =lnk_productImagerunat =serverText =select
OnClick =viewProductImage('<%#DataBinder.Eval(Container.DataItem,Id)%>')
>
< / asp:LinkBut​​ton>

CodeBehind:

 保护无效viewProductImage(对象发件人,EventArgs e,int id)
{
//加载产品映像
}


解决方案

我看到您使用的是中继器,因此您可能会使用此代码:$ b $
$ b

 < asp:Repeater ID =_ postsRepeaterrunat =服务器OnItemCommand =_ postsRepeater_ItemCommand> 
< ItemTemplate>< asp:LinkBut​​ton ID =_ postDeleteLinkBut​​tonrunat =serverCommandName =DeletePostCommandArgument =<%#((Post)Container.DataItem).ID%>> ;删除< / ASP:LinkBut​​ton的>< / ItemTemplate中>
< / asp:中继器>

然后处理中继器的ItemCommand事件:

<$如果(e.CommandName ==DeletePost)//将DeletePost替换为您的命令的名称,则可以使用pre $ protected void _postsRepeater_ItemCommand(object source,RepeaterCommandEventArgs e)
{
//从e.CommandArgument获取传递的参数
// eg如果传递一个int使用:
// int id = Convert.ToInt32(e.CommandArgument);
}
}


HTML :

<asp:LinkButton ID="lnk_productImage" runat="server" Text="select"
   OnClick="viewProductImage('<%#DataBinder.Eval(Container.DataItem,"Id") %>')"
   >
</asp:LinkButton>

CodeBehind:

protected void viewProductImage(object sender, EventArgs e, int id)
{ 
    //Load Product Image
}

解决方案

I see you're using a repeater, so you probably could use this code:

In your repeater template:

<asp:Repeater ID="_postsRepeater" runat="server" OnItemCommand="_postsRepeater_ItemCommand">
  <ItemTemplate><asp:LinkButton ID="_postDeleteLinkButton" runat="server" CommandName="DeletePost" CommandArgument="<%# ((Post)Container.DataItem).ID %>">Delete</asp:LinkButton></ItemTemplate>
</asp:Repeater>

Then handle the repeater's ItemCommand event:

protected void _postsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "DeletePost") // Replace DeletePost with the name of your command
    {
        // Get the passed parameter from e.CommandArgument
        // e.g. if passed an int use:
        // int id = Convert.ToInt32(e.CommandArgument);
    }
}

这篇关于如何使用参数创建一个linkbutton onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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