在Asp.net中通过Button的CommandArgument传递多个参数
我有一个包含多行的 gridview,每行都有一个更新按钮,当有人单击更新按钮时我需要传递 2 个值。 除了将参数打包在 CommandArgument 中并用逗号分隔(过时且不优雅)之外,我如何传递多个参数?
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="UpdateRow" CommandArgument="arg_value" Text="Update and Insert" OnCommand="CommandButton_Click" ></asp:LinkButton>
请注意,无法从页面上的任何控件检索这些值,因此我目前不寻求任何设计解决方案。
I have a gridview with multiple rows, each has a Update button and I need to pass 2 values when someone clicks on Update button.
Aside from packing the arguments inside CommandArgument separated by commas (archaic and not elegant), how would I pass more than one argument?
<asp:LinkButton ID="UpdateButton" runat="server" CommandName="UpdateRow" CommandArgument="arg_value" Text="Update and Insert" OnCommand="CommandButton_Click" ></asp:LinkButton>
As a note, the values can't be retrieved from any controls on the page, so I am presently not seeking any design solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以传递分号分隔的值作为命令参数,然后拆分字符串并使用它。
在服务器端
You can pass semicolon separated values as command argument and then split the string and use it.
at server side
@Patrick 的回答是个好主意,值得更多的赞扬!您可以拥有任意数量的数据项,它们都是分开的,并且可以在必要时在客户端使用。
它们也可以以声明方式添加,而不是在代码中添加。我只是对 GridView 这样做了,如下所示:
在 RowCommand 中,您执行以下操作:
比将所有值与分隔符放在一起并在最后再次解压要干净得多。
不要忘记测试/清理从页面返回的任何数据,以防它被篡改!
@Patrick's answer is a good idea, and deserves more credit!. You can have as many data items as you want, they are all separated, and can be used client side if necessary.
They can also be added declaratively rather than in code. I just did this for a GridView like this:
In the RowCommand, you do this:
So much cleaner than hacking all the values together with delimiters and unpacking again at the end.
Don't forget to test/clean any data you get back from the page, in case it's been tampered with!
经过一番探索后,凯尔西似乎是正确的。
当你想使用它时,只需使用逗号或其他东西并将其分开即可。
After poking around it looks like Kelsey is correct.
Just use a comma or something and split it when you want to consume it.
我的方法是使用属性集合从代码隐藏添加 HTML 数据属性。这与 jquery 和客户端脚本更加一致。
然后通过属性集合检索值
My approach is using the attributes collection to add HTML data- attributes from code behind. This is more inline with jquery and client side scripting.
Then retrieve the values through the attribute collection
在上面的评论中添加一个更优雅的方法,
在服务器端:
A little more elegant way of doing the same adding on to the above comment ..
And on the server side:
将其存储在 gridview datakeys 集合中,或者将其存储在同一单元格内的隐藏字段中,或者将这些值连接在一起。这是唯一的办法。您不能在一个链接中存储两个值。
Either store it in the gridview datakeys collection, or store it in a hidden field inside the same cell, or join the values together. That is the only way. You can't store two values in one link.
如果你想传递两个值,你可以使用这种方法
基本上我将 {CommmandName,CommandArgument} 视为键值。从数据库字段设置两者。在这种情况下,您必须使用 OnClick 事件并使用
OnCommand
事件,我认为这是更干净的代码。If you want to pass two values, you can use this approach
Basically I am treating {CommmandName,CommandArgument} as key value. Set both from database field. You will have to use OnClick event and use
OnCommand
event in this case, which I think is more clean code.您可以使用分隔符分割参数(分隔符不得位于参数值内 - 例如“|”)。
参数名称必须是数据源的有效名称。
单引号用于命令参数,双引号用于参数名称。
服务器代码
You can split the parameters using a separator (that must not be inside the parameter values - ex. "|").
Names of parameters must be valid names of the data source.
Single quotes for the command arguments, and double quotes for the names of the parameters.
server code