Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You will have to add some custom Javascript to your page for the client-side alert to show. Here's a method that I've written that works with a GridView called 'GridView1' (this should be the default name if you've just dragged the control onto your ASPX page):</p> <pre><code>&lt;script type="text/javascript"&gt; function ClientCheck() { var valid = false; var gv = document.getElementById("GridView1"); for (var i = 0; i &lt; gv.all.length; i++) { var node = gv.all[i]; if (node != null &amp;&amp; node.type == "checkbox" &amp;&amp; node.checked) { valid = true; break; } } if (!valid) { alert("Invalid. Please select a checkbox to continue."); } return valid; } &lt;/script&gt; </code></pre> <p>You can see that it sets a variable to the <code>GridView</code> control to start with, then iterates through all the children in a <code>for</code> loop. If the child is a <code>checkbox</code> and it is <code>checked</code>, then we set the <code>valid</code> variable to true. If we get to the end of the iteration and no checked checkboxes are found, then <code>valid</code> remains false and we execute the alert.</p> <p>To link this into your <code>GridView</code> on your ASPX page, first make the button column a <code>TemplateField</code> and surround the <code>LinkButton</code> with your client-side code. If you've used the designer to set up your columns, you can use the "Convert this field into a TemplateField" link in the column editor). Here's an example of the source you'll end up with:</p> <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText="Button Field" ShowHeader="False"&gt; &lt;ItemTemplate&gt; &lt;span onclick="return ClientCheck();"&gt; &lt;asp:LinkButton ID="LinkButton1" runat="server" CommandName="IDClick" Text='&lt;%# Eval("YourDataSourceItem") %&gt;'&gt;&lt;/asp:LinkButton&gt; &lt;/span&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; // ...your remaining columns... </code></pre> <p>Using the <code>TemplateField</code> lets us add any client-side code we like. Here we're adding a <code>span</code> and using <code>onclick</code> to call our <code>ClientCheck</code> method.</p> <p>If you aren't bothered about the alert, you could achieve your aims by using a <code>CustomValidator</code> control, which executes on the server-side.</p> <p>I hope this helps.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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