0

I'd be glad if someone could kindly modify the following code so that GridView becomes click-able. The problem is that the code in question works perfectly in my Windows application under CellMouseClick Event of DataGridView but it doesn't work in the Web application since there's no such event therein. So, under what event can it be made click-able?

    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
Akaglo
  • 129
  • 4
  • 17
  • [Why are empty catch blocks a bad idea?](http://stackoverflow.com/questions/1234343/why-are-empty-catch-blocks-a-bad-idea) – Tim Schmelter Mar 25 '13 at 10:54
  • I agree it's a bad idea,Sir, but it's so in this web application because I simply don't know how to go about it. In the Windows Application, there's something like: `MessageBox.Show(ex.Message)` – Akaglo Mar 25 '13 at 11:08
  • Either handle it in a meaninful way(at least write it to a log-file) or don't catch it. An exception that gets soneone's attention immediately is always better than one nobody notices. – Tim Schmelter Mar 25 '13 at 11:10
  • Thank you, Sir. Could you please attend to my main problem? – Akaglo Mar 25 '13 at 11:15
  • You haven't mentioned the problem so it's difficult to help. – Tim Schmelter Mar 25 '13 at 11:17
  • The problem is that the code in question works perfectly in my Windows application under CellMouseClick Event of DataGridView but it doesn't work in the Web application since there's no such event therein. So, under what event can it be made click-able? – Akaglo Mar 25 '13 at 11:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26860/discussion-between-akaglo-and-tim-schmelter) – Akaglo Mar 25 '13 at 11:26

1 Answers1

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")
 {
      ...
 }
}
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • Please I don't know specifically how I should implement your suggestion. Could please elaborate a little further? – Akaglo Mar 25 '13 at 14:53
  • i posted sample of code – Aghilas Yakoub Mar 25 '13 at 14:57
  • I've checked the links. Even though the samples look fine they don't seem to give me exactly what I want. The issue is that I want a code that will enable data on a row to appear in the text boxes when the user clicks on the row in the GridView, to perform update. My code above does exactly this in my Windows application and I want same in the Web application. – Akaglo Mar 26 '13 at 09:44