title: How to implement onclick row selection on gridview
body: <p>Can someone help me, I am new at using asp.net. I have a table that is filled with data and if the user clicks on a row, the data inside the selected row will populate the input fields. I Have created a c# code using onclick row selection in grid view, it is working but it can only select one row and if you select another row it doesn't select it and it stays on whatever you clicked first. how can I select the other rows using onclick?</p>
  
  <p>this is the html gridview code</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      DataKeyNames="CASE_KEY" DataSourceID="SqlDataSource1" Height="250px" 
      Width="1109px" BackColor="White" BorderColor="#999999" BorderStyle="None" 
      BorderWidth="1px" CellPadding="3" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
      OnRowDataBound="GridView1_RowDataBound"&gt;
      &lt;AlternatingRowStyle BackColor="Gainsboro" /&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="CASE_KEY" HeaderText="CASE_KEY" ReadOnly="True" 
              SortExpression="CASE_KEY" Visible="False" /&gt;
          &lt;asp:BoundField DataField="DEPARTMENT_CASE_NUMBER" 
              HeaderText="Department Case #" SortExpression="DEPARTMENT_CASE_NUMBER" /&gt;
          &lt;asp:BoundField DataField="DEPARTMENT_NAME" HeaderText="Department" 
              SortExpression="DEPARTMENT_NAME" /&gt;
          &lt;asp:BoundField DataField="CHARGE" HeaderText="Charge" 
              SortExpression="CHARGE" /&gt;
          &lt;asp:BoundField DataField="LAB_CASE" HeaderText="Lab Case #" 
              SortExpression="LAB_CASE" /&gt;
          &lt;asp:BoundField DataField="OFFENSE_DATE" HeaderText="Incident Report Date" 
              SortExpression="OFFENSE_DATE" /&gt;
      &lt;/Columns&gt;
  </code></pre>
  
  <p>This is the html input fields</p>
  
  <pre><code>&lt;table class="style2"&gt;
      &lt;tr&gt;
          &lt;td class="style3"&gt;
              Department Case #&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="style3"&gt;
              Department&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:TextBox ID="TextBox2" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="style3"&gt;
              Charge&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:TextBox ID="TextBox3" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="style3"&gt;
              Lab Case #&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:TextBox ID="TextBox4" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="style3"&gt;
              Incident Report Date&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:TextBox ID="TextBox5" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>This is my c# code</p>
  
  <pre><code> protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
      {
          //Get the selected row
          GridViewRow row = GridView1.SelectedRow;
          if (row != null)
          {
              //Change the cell index(1) of column as per your design
              //Get the Selected row cell values here
              GridViewRow gr = GridView1.SelectedRow;
              TextBox1.Text = gr.Cells[1].Text;
              TextBox2.Text = gr.Cells[2].Text;
              TextBox3.Text = gr.Cells[3].Text;
              TextBox4.Text = gr.Cells[4].Text;
              TextBox5.Text = gr.Cells[5].Text;
  
          }
      }
      protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
          {
              //Change the mouse cursor to Hand symbol to show the user the cell is selectable
              e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';this.style.cursor='Pointer'";
              e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
  
              //Attach the click event to each cells
              e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
          }
      }
  </code></pre>