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

时间:2016-04-24 16:00:39

标签: javascript asp.net vb.net

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

这些是它的代码:

'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 个答案:

答案 0 :(得分:2)

您可以通过这种方式检索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