如何在GridView Clickable中创建单元格或行

时间:2013-03-25 10:52:18

标签: asp.net vb.net

如果有人可以修改以下代码以便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

1 个答案:

答案 0 :(得分:2)

您可以在LinkButton

ItemTemplate上定义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

样本

              <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")
 {
      ...
 }
}