简体   繁体   English

LinkBut​​ton CommandName和CommandArgument

[英]LinkButton CommandName and CommandArgument

I'm having trouble understanding CommandName and CommandArgument associated with an ASP.NET LinkButton. 我无法理解与ASP.NET LinkBut​​ton关联的CommandNameCommandArgument I have read this article - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx and other questions on this site. 我已经阅读了这篇文章 - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx以及本网站上的其他问题。

I guess my questions are - what exactly is a "Command"? 我想我的问题是 - 究竟什么是“命令”? Can a CommandName basically be any text? CommandName基本上可以是任何文本吗? I see "Sort" and "Submit" as examples. 我将“排序”和“提交”视为示例。 And as for CommandArgument , this is just used to further specify the type of "Command". 至于CommandArgument ,这仅用于进一步指定“Command”的类型。 Correct? 正确? Thanks very much for your help. 非常感谢您的帮助。

A Command can be anything you want it to be. Command可以是你想要的任何东西。

The basic idea is that if you say have a repeater, and in that repeater you have multiple options, you can give each option a different CommandName . 基本的想法是,如果你说有一个转发器,并且在那个转发器中你有多个选项,你可以给每个选项一个不同的CommandName The CommandArgument would then be based on the unique identifier of the line. 然后, CommandArgument将基于该行的唯一标识符。

Then on the post-back you can check which CommandName was fired and based on that use the value in the CommandArgument 然后在回发后,您可以检查触发了哪个CommandName ,并根据该命令使用CommandArgument的值

For Example, the mark-up could look something like... 例如,加价可能看起来像......

<asp:Repeater runat="server" id="myRepeater">
  <ItemTemplate>
    <div>
      <asp:LinkButton runat="server" id="lnkEdit" CommandName="edit" 
        CommandArgument="<%#Container.DataItem.Id%>" Text="Edit" 
        OnClick="OnClickHandler" />
      <asp:LinkButton runat="server" id="lnkDelete" CommandName="delete" 
        CommandArgument="<%#Container.DataItem.Id%>" Text="Delete"
        OnClick="OnClickHandler" />
    </div>
  </ItemTemplate>
</asp:Repeater>

Then your post-pack handler could check to see which one was clicked... 然后你的post-pack处理程序可以查看哪个被点击了...

Protected Sub OnClickHandler(ByVal sender As Object, ByVal e As EventArgs)
  Dim lnk as LinkButton = CType(sender,LinkButton)
  Select Case lnk.CommandName
    Case "edit"
      EditItem(lnk.CommandArgument)
    Case "delete"
      DeleteItem(lnk.CommandArgument)
  End Select
End Sub

CommandName can be any string yes. CommandName可以是任何字符串yes。 But beware! 但要小心! ASP.NET will treat certain strings in a special way. ASP.NET将以特殊方式处理某些字符串。 For example if you have a Button control in a GridView column with a CommandName of "delete" it will raise the OnDeleting event and the CommandArgument will have been set to the row index of the GridViewRow that the button is in. Otherwise as others have posted you can use the CommandName and CommandArgument however best suits your circumstances. 例如,如果GridView列中的Button控件具有CommandName为“delete”,则它将引发OnDeleting事件,并且CommandArgument将被设置为该按钮所在的GridViewRow的行索引。否则,正如其他人发布的那样您可以使用CommandNameCommandArgument但最适合您的情况。

typically you will set the CommandArgument to be the row index of the control's parent container during binding, and the CommandName to be something meaningful to your application domain, such as "UpdateFoo." 通常,您将CommandArgument设置为绑定期间控件的父容器的行索引,并将CommandName设置为对应用程序域有意义的内容,例如“UpdateFoo”。 You then use this in the OnRowCommand event handler to determine which button has been clicked and therefore what business logic to execute. 然后,您可以在OnRowCommand事件处理程序中使用它来确定单击了哪个按钮,从而确定要执行的业务逻辑。

CommandName is what you actually do when the event is triggered and the CommandArgument and of course is the argument related to process. CommandName是您在触发事件时实际执行的操作, CommandArgument当然是与进程相关的参数。 It makes more sense if you use link buttons in repeater or similar list items. 如果在转发器或类似列表项中使用链接按钮,则更有意义。 In that case your CommandName can be "Delete", "Edit", "Publish" and for this processes you need to know which record you are dealing with and 'CommandArgument' is your man in this case you can assign it the ID or comma separated data to process. 在这种情况下,你的CommandName可以是“删除”,“编辑”,“发布”,对于这个过程,你需要知道你正在处理哪条记录,'CommandArgument'是你的男人,在这种情况下,你可以为它分配ID或逗号将数据分开处理。

Of course you can also use this to merge similar events "Sort" is a great example for this as you can give 'CommandArgument' as "Price asc", "Date asc", "Date desc" all your link buttons triggers the event. 当然你也可以使用它来合并类似的事件“Sort”是一个很好的例子,因为你可以将'CommandArgument'作为“Price asc”,“Date asc”,“Date desc”所有链接按钮触发事件。

As freefaller says, both the CommandName and CommandArgument are just string values. 正如freefaller所说, CommandNameCommandArgument都只是字符串值。

The reason why is many fold however a click event will just fire the click of a button and you handle that button(s) specifically. 之所以有很多折叠,但点击事件只会点击一个按钮,你就会专门处理这个按钮。

A good example of the use of CommandButtons is imagine you have rows of data, each row has a ability to View, Edit or Delete. 使用CommandButtons的一个很好的例子是想象你有数据行,每行都有查看,编辑或删除的能力。

Rather than code for each one seperately, you can use the Command parts and have each row have the CommandArgument of the record ID, and the CommandArgument to be the action you wish to perform; 而不是每一个seperately代码,你可以使用Command的部分,具有每行有CommandArgument记录ID,而CommandArgument是要执行的动作; ViewRec, EditRec and DelRec` for example: ViewRec, EditRec and DelRec`:

protect void cmd_Command(object sender, CommandEventArgs e)
{
    // Example, Redirect to page with action
    response.redirect(string.format("~/record.aspx?id={0}&action={1}", e.commandArgument, e.CommandName);
}

Using this example as a concept, shows that you then only have one section of code to handle multiple options and therefore only one place to maintain etc... 使用这个例子作为一个概念,表明你只有一段代码来处理多个选项,因此只有一个地方可以维护等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM