0

如果有人可以修改以下代码以使 GridView 变得可点击,我会很高兴。问题是有问题的代码在我的 Windows 应用程序中的 DataGridView 的 CellMouseClick 事件下完美运行,但它在 Web 应用程序中不起作用,因为其中没有这样的事件。那么,在什么情况下可以使它成为可点击的呢?

    Try
        dr = Nothing
        Dim str As String
        str = GridView1.SelectedValue(0).Value
        cmd = New SqlCommand("Select* from ProgramDetails.Subjects where SubjectCode='" & str & "'", cn)
        dr = cmd.ExecuteReader
        While (dr.Read)
            txtIdNumber.Text = dr(0)
            txtSubjectCode.Text = dr(1)
            txtSubjectName.Text = dr(2)
            If dr(3) = "Core" Then
                rbnCore.Checked = True
            Else
                rbnElective.Checked = True
            End If
            txtUserId.Text = dr(4)
            txtPassword.Text = dr(5)
        End While
        dr.Close()
        btnSave.Enabled = False
    Catch ex As Exception
    End Try
4

1 回答 1

2

You can define LinkButton on ItemTemplate of your Gridview

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lkb" CommandName="YourCommand" runat="server" Text="Sample"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

sample

              <asp:gridview id="ContactsGridView" 
              autogeneratecolumns="false"
              onrowcommand="GridView_RowCommand"
              runat="server">
              <columns>
                 <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lkb" CommandName="YourCommand" runat="server" Text="Sample"></asp:LinkButton>
                    </ItemTemplate>
                  </asp:TemplateField>
              </columns>
            </asp:gridview>

void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
 // If multiple buttons are used in a GridView control, use the
 // CommandName property to determine which button was clicked.
 if(e.CommandName=="YourCommand")
 {
      ...
 }
}
于 2013-03-25T13:50:37.103 回答