ASP.NET 未从 LinkBut​​ton onClick (VB) 获取 CommandArgument 值

Posted

技术标签:

【中文标题】ASP.NET 未从 LinkBut​​ton onClick (VB) 获取 CommandArgument 值【英文标题】:ASP.NET Not getting CommandArgument Value from LinkButton onClick(VB) 【发布时间】:2016-08-17 22:57:52 【问题描述】:

我需要从 LinkBut​​ton 中的 commandArgument 获取值,但是当我将鼠标悬停在字母 B 上时,我看到我得到的是这个而不是单独的字母:

这些是它的代码:

'problem here with the commandArgument
    Sub LetterGuessed(sender As Object, e As CommandEventArgs)
        'First, make the letter selected disabled

        Dim clickedButton As LinkButton = e.CommandArgument
        clickedButton.Enabled = False
        clickedButton.ForeColor = Color.Red

        'Now, determine if the letter is in the word
        If Session("hangman_word").ToString().ToLower().IndexOf(e.CommandArgument.ToLower()) >= 0 Then
            'The letter was found
            Dim i As Integer
            Dim current As String = String.Empty
            For i = 0 To Session("hangman_word").ToString().Length - 1
                If Session("hangman_word").ToString().Substring(i, 1).ToLower() = e.CommandArgument.ToLower() Then
                    current &= Session("hangman_word").ToString().Substring(i, 1)
                Else
                    current &= Session("current_word").ToString().Substring(i, 1)
                End If
            Next i

            Session("current_word") = current
            DisplayCurrentWord()

            'See if they have guessed the word correctly!
            If Session("hangman_word").ToString() = Session("current_word").ToString() Then
                EndGame(True)
            End If
        Else
            'The letter was not found, increment the # of wrong guesses
            Session("wrong_guesses") = Convert.ToInt32(Session("wrong_guesses")) + 1

            'Update the hangman image
            hangmanImage.ImageUrl = "Content/hangman/hang_" & Session("wrong_guesses").ToString() & ".gif"

            If Convert.ToInt32(Session("wrong_guesses")) >= 6 Then
                'Eep, the person has lost
                EndGame(False)
            End If
        End If
    End Sub

这是表格:

<form>
  <asp:image id="hangmanImage" runat="server" />
  <p>

  <asp:literal runat="server" id="currentWord" />
  <p>
  <asp:LinkButton runat="server" id="A" CommandArgument="A" Text="A" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="B" CommandArgument="B" Text="B" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="C" CommandArgument="C" Text="C" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="D" CommandArgument="D" Text="D" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="E" CommandArgument="E" Text="E" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="F" CommandArgument="F" Text="F" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="G" CommandArgument="G" Text="G" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="H" CommandArgument="H" Text="H" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="I" CommandArgument="I" Text="I" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="J" CommandArgument="J" Text="J" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="K" CommandArgument="K" Text="K" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="L" CommandArgument="L" Text="L" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="M" CommandArgument="M" Text="M" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="N" CommandArgument="N" Text="N" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="O" CommandArgument="O" Text="O" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="P" CommandArgument="P" Text="P" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="Q" CommandArgument="Q" Text="Q" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="R" CommandArgument="R" Text="R" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="S" CommandArgument="S" Text="S" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="T" CommandArgument="T" Text="T" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="U" CommandArgument="U" Text="U" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="V" CommandArgument="V" Text="V" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="W" CommandArgument="W" Text="W" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="X" CommandArgument="X" Text="X" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="Y" CommandArgument="Y" Text="Y" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="Z" CommandArgument="Z" Text="Z" OnCommand="LetterGuessed" />
  <p>
  <center>
      <asp:label id="lblEndGameMessage" runat="server"
          Font-Size="18pt" Font-Weight="Bold" />
  </center>
</form>

这是页面:http://asptry20160424112507.azurewebsites.net/

有人可以帮帮我吗?

【问题讨论】:

【参考方案1】:

您可以通过这种方式检索 LinkBut​​ton 控件及其CommandArgument

Protected Sub LetterGuessed(object sender, CommandEventArgs e)
    Dim clickedButton As LinkButton = CType(sender, LinkButton)
    Dim cmdArgument As String = e.CommandArgument
    ...
End Sub

【讨论】:

太棒了!非常感谢。游戏工作版:asptry20160424112507.azurewebsites.net 我尝试了您的链接,但收到一条错误消息(与调试模式有关)。我稍后会再试一次,看看它是什么样子的。 是的,我的错我只是尝试添加一些新功能,所以我目前正在调试。谢谢。

以上是关于ASP.NET 未从 LinkBut​​ton onClick (VB) 获取 CommandArgument 值的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET - 在 RenderContent 调用中将事件处理程序添加到中继器内的 LinkBut​​ton

使用确认警报 ASP.Net 单击 Gridview LinkBut​​ton 上的突出显示行

为啥 GridView 中的 LinkBut​​ton 不会引发其 OnClick 事件?

ASP.Net 用户控件悬停导致点击

使用 jquery 切换 LinkBut​​ton 的启用/禁用状态

UpdatePanel 中 ListView 中的 LinkBut​​ton 导致完全回发