为什么我的 CommandArgument 为空?

发布于 2024-08-03 11:15:08 字数 562 浏览 4 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(5

七色彩虹 2024-08-10 11:15:08

您不能在具有 runat="server" 属性的标记的属性内使用 <%= %> 语法。我很惊讶代码竟然还能运行。 :)

更新:

您可能想在转发器上使用 ItemDataBound 事件,找到链接按钮并设置 CommandArgument 属性。

不是很优雅,但这里有一个 VB.NET 示例。

Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Select Case e.Item.ItemType
      Case ListItemType.Item, ListItemType.AlternatingItem
        Dim b As LinkButton = e.Item.FindControl("btn")
        b.CommandArgument = e.Item.ItemIndex
        b.DataBind()
    End Select
  End Sub

You can't use the <%= %> syntax inside properties on a tag with a runat="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.

Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Select Case e.Item.ItemType
      Case ListItemType.Item, ListItemType.AlternatingItem
        Dim b As LinkButton = e.Item.FindControl("btn")
        b.CommandArgument = e.Item.ItemIndex
        b.DataBind()
    End Select
  End Sub
仙气飘飘 2024-08-10 11:15:08

你没有设置

你可能想要的

<%# Container.DataItemIndex %>

或者

<%= Container.DataItemIndex %>

:)

You're not setting it

You possibly want

<%# Container.DataItemIndex %>

or

<%= Container.DataItemIndex %>

:)

愛放△進行李 2024-08-10 11:15:08

试试

<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />

你漏掉了“#”号。

Try

<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />

You were missing the "#" sign.

黯然#的苍凉 2024-08-10 11:15:08

这个网站确实帮助我解决了这个问题:http://forums.asp.net/t/1671316。 aspx

我遇到的问题是,当我第二次单击该按钮时,在命令参数中传递了空参数。正如上面的帖子所解释的,这是因为命令参数仅在数据绑定事件中设置。因此,要解决此问题,请在 page_load 子示例中包含一个数据绑定事件

。 (VB)

Private Sub BindSelectButtons()       

    'Purpose: bind the data to the select buttons for commandargument to be used
    Dim i As Integer
    For i = 0 To gridview1.Rows.Count - 1
        gridview1.Rows(i).Cells(8).FindControl("btnID").DataBind()
    Next
End Sub

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    'Rebind select buttons so that the commandargument refreshes
    BindSelectButtons()
End Sub

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)

Private Sub BindSelectButtons()       

    'Purpose: bind the data to the select buttons for commandargument to be used
    Dim i As Integer
    For i = 0 To gridview1.Rows.Count - 1
        gridview1.Rows(i).Cells(8).FindControl("btnID").DataBind()
    Next
End Sub

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    'Rebind select buttons so that the commandargument refreshes
    BindSelectButtons()
End Sub
澉约 2024-08-10 11:15:08

确保视图状态已启用
e.Row.EnableViewState = true;

Make sure View State is enabled
e.Row.EnableViewState = true;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文