Asp.Net LinkBut​​ton Onclick = method(container.dataitem),需要语法帮助

发布于 2024-07-17 06:53:04 字数 453 浏览 8 评论 0原文

我有一个链接按钮,我想在后面的代码中调用一个方法。 该方法采用一个参数,我需要将其粘贴在container.dataitem 中。 我知道 container.dataitem 语法是正确的,因为我在其他控件中使用它。 我不知道如何将它用作方法的参数。 单击按钮后,应使用 container.dataitem 调用该方法。 该方法称为“AddFriend(string username)”,下面是代码。 谢谢你!

<asp:LinkButton ID="lbAddFriend" runat="server" OnClick='<%# "AddFriend(" +((System.Data.DataRowView)Container.DataItem)["UserName"]+ ")" %>' Text="AddFriend"></asp:LinkButton></td>

I have a linkbutton that I want to call a method in the code behind. The method takes a parameter of which I need to stick in a container.dataitem. I know the container.dataitem syntax is correct because I use it in other controls. What I don't know is how to user it a parameter for a method. Upon clicking on the button, the method should be called with the container.dataitem. The method is called 'AddFriend(string username)' Below is code. Thank you!

<asp:LinkButton ID="lbAddFriend" runat="server" OnClick='<%# "AddFriend(" +((System.Data.DataRowView)Container.DataItem)["UserName"]+ ")" %>' Text="AddFriend"></asp:LinkButton></td>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若沐 2024-07-24 06:53:04

您需要使用 ButtonField 并处理 RowCommand 中的单击。 检查 MSDN 文档

 <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>

并且在后面的代码中...

  void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="Add")
    {
         AddFriend(DataBinder.Eval(Container.DataItem, "Price""UserName"));
    }
  }

You need to use a ButtonField and handle the click in RowCommand. Check the MSDN docs

 <asp:buttonfield buttontype="Link" 
                  commandname="Add" 
                  text="Add"/>

And in the code behind...

  void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    if(e.CommandName=="Add")
    {
         AddFriend(DataBinder.Eval(Container.DataItem, "Price""UserName"));
    }
  }
白衬杉格子梦 2024-07-24 06:53:04

我认为同样的事情也适用于数据列表,但我一直在后面的代码中使用它作为中继器。 可以使用 DataListItemEventArgs 和 DataListCommandEventArgs 代替 Repeater。

protected void rptUserInfo_Data(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        UserInfo oUserInfo = e.Item.DataItem as UserInfo;

        LinkButton hlUser = e.Item.FindControl("hlUser") as LinkButton;
        hlUser.Text = oUserInfo.Name;
        hlUser.CommandArgument = oUserInfo.UserID + ";" + oUserInfo.uName;
        hlUser.CommandName = "User";
    }
}
public void UserArtItem_Command(Object sende, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "User")
    {
        string command = e.CommandArgument.ToString();
        string[] split = command.Split(new Char[] { ';' });

        Session["ArtUserId"] = split[0];
        Session["ArtUserName"] = split[1];
        Response.Redirect("~/Author/" + split[1]);
    }
}

I think the same thing applies to a data list, but I've been using this for a repeater in my code behind. Mayby use DataListItemEventArgs and DataListCommandEventArgs in place of the Repeater.

protected void rptUserInfo_Data(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        UserInfo oUserInfo = e.Item.DataItem as UserInfo;

        LinkButton hlUser = e.Item.FindControl("hlUser") as LinkButton;
        hlUser.Text = oUserInfo.Name;
        hlUser.CommandArgument = oUserInfo.UserID + ";" + oUserInfo.uName;
        hlUser.CommandName = "User";
    }
}
public void UserArtItem_Command(Object sende, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "User")
    {
        string command = e.CommandArgument.ToString();
        string[] split = command.Split(new Char[] { ';' });

        Session["ArtUserId"] = split[0];
        Session["ArtUserName"] = split[1];
        Response.Redirect("~/Author/" + split[1]);
    }
}
兮子 2024-07-24 06:53:04

也许是这个?

<asp:LinkButton ID="lbAddFriend" runat="server"
 Text="Add Friend" OnCommand="AddFriend"
 CommandArgument='<%# Eval("UserName").ToString() %>' />

然后在代码中:

Protected Sub AddFriend(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    Dim UserName As String = e.CommandArgument
    'Rest of code
End Sub

Maybe this?

<asp:LinkButton ID="lbAddFriend" runat="server"
 Text="Add Friend" OnCommand="AddFriend"
 CommandArgument='<%# Eval("UserName").ToString() %>' />

Then in the code:

Protected Sub AddFriend(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    Dim UserName As String = e.CommandArgument
    'Rest of code
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文