Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking entire row in Gridview clickable
    primarykey
    data
    text
    <p>I've been trying to follow this answer: <a href="https://stackoverflow.com/questions/6250545/how-to-implement-full-row-selecting-in-gridview-without-select-button">How to implement full row selecting in GridView without select button?</a></p> <p>But I'm still slightly confused. After following that question, my rows are now clickable. But how do I implement it to do something after being clicked? Currently, I use a button to do what I need to the database per row:</p> <p>Here is the .aspx code:</p> <pre><code>&lt;Columns&gt; &lt;asp:ButtonField Text = "Click Me" CommandName = "Clicked" ButtonType = "Button" /&gt; ...other columns stuff &lt;/Columns&gt; </code></pre> <p>C# code behind:</p> <pre><code> protected void RowCommand(object sender, GridViewCommandEventArgs e) { //if button is clicked if (e.CommandName == "Clicked") { //go find the index on the gridview int selectedIndex = MsgInbox.SelectedIndex; if (int.TryParse(e.CommandArgument.ToString(), out selectedIndex)) { //do something with database } </code></pre> <p>Now that works beautifully. However, I don't want a button to be clickable, I want a entire row to be clickable. I know this is currently wrong, but this is what I have so far for the code:</p> <p>.aspx code</p> <pre><code> &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID="SelectRow" runat="server" ForeColor="red" CommandName="Clicked"&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; </code></pre> <p>C# code:</p> <pre><code> protected void Gridview_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; var selectButton = e.Row.FindControl("SelectRow") as Button; e.Row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(selectButton, ""); </code></pre> <p>I get a simple null pointer exception when I do this, but I'm not really familiar with e.Row.Attributes so I really have no idea where this is failing and what I need to do to add the database logic.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload