为什么我的 CommandArgument 为空?
我有一个 ASP.Net 页面,它向用户显示选项列表。当他们从列表中进行选择时,它会回发并查询 SQL Server。结果显示在更新面板选项下方的列表视图中。以下是 ItemTemplate 的片段:
<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />
DataItemIndex 未出现,因此我的命令参数为空。然而,对象发送者是显示项目的按钮。
为什么索引项没有出现在CommandArgument中?
可能是回发的原因吗?如果是这样,为什么会被退回?有办法解决吗?
编辑: 抱歉,从我之前尝试解决该问题的角度来看,我发布了错误的代码,但它仍然没有出现。
解决: 我发现了另一种解决方法,OnCommand 的发送者是链接按钮,其中包含 CommandArgument。我将把这个问题归结为多个回发和 JavaScript 的问题。
I have an ASP.Net page, which displays a list of options to the user. When they select from the list, it does a post back and queries a sql server. The results are displayed in a listview below the options in an update panel. Below is a snippet of the ItemTemplate:
<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />
The DataItemIndex does not appear, so my commandargument is empty. However, the object sender is the button, which shows the item.
Why is the index item not appearing in the CommandArgument?
Could it be the post back? If so, why would it be the post back? Is there a way around it?
Edit:
Sorry, from my attempts to solve it before, I posted bad code, but it still isn't appearing.
Resolution:
I found another work around in that the sender of the OnCommand is the link button, which has the CommandArgument. I will chalk this issue up to be an issue with multiple postbacks and javascript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能在具有
runat="server"
属性的标记的属性内使用<%= %>
语法。我很惊讶代码竟然还能运行。 :)更新:
您可能想在转发器上使用 ItemDataBound 事件,找到链接按钮并设置 CommandArgument 属性。
不是很优雅,但这里有一个 VB.NET 示例。
You can't use the
<%= %>
syntax inside properties on a tag with arunat="server"
attribute. I'm surprised the code will even run. :)UPDATE:
You probably want to use the ItemDataBound event on the repeater, find the linkbutton and set the CommandArgument property.
Not very elegant, but here's a VB.NET sample.
你没有设置
你可能想要的
或者
:)
You're not setting it
You possibly want
or
:)
试试
你漏掉了“#”号。
Try
You were missing the "#" sign.
这个网站确实帮助我解决了这个问题:http://forums.asp.net/t/1671316。 aspx
我遇到的问题是,当我第二次单击该按钮时,在命令参数中传递了空参数。正如上面的帖子所解释的,这是因为命令参数仅在数据绑定事件中设置。因此,要解决此问题,请在 page_load 子示例中包含一个数据绑定事件
。 (VB)
This site really helped me with this problem: http://forums.asp.net/t/1671316.aspx
The issue I ran into was that I was being passed null arguments in the commandargument when I clicked on the button a second time. As the post above explains, this is because commandargument is only set in the databind event. So, to fix this, include a databind event in the page_load sub
Ex. (VB)
确保视图状态已启用
e.Row.EnableViewState = true;
Make sure View State is enabled
e.Row.EnableViewState = true;