Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can add asp.net required field validators to each text box in your gridview. </p> <pre><code>&lt;asp:TextBox id="txtQty" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:RequiredFieldValidator id="reqQty" ControlToValidate="txtQty" ErrorMessage="*"&gt;&lt;/asp:RequiredFieldValidator&gt; </code></pre> <p><strong>Elaboration</strong></p> <p>T-SQL</p> <pre><code>create table products ( id int identity(1,1), name varchar(500), price decimal(18,2) ) insert into products values ('Soap', 15.0) insert into products values ('Provitas', 25.0) insert into products values ('Paper', 10.0) insert into products values ('Foam Bath', 150.0) </code></pre> <p>ASPX</p> <pre><code> &lt;asp:GridView ID="gvTest" runat="server" DataSourceID="SqlTest" AutoGenerateColumns="false"&gt; &lt;Columns&gt; &lt;asp:BoundField HeaderText="ID" DataField="ID" /&gt; &lt;asp:BoundField HeaderText="Name" DataField="Name" /&gt; &lt;asp:BoundField HeaderText="Price" DataField="Price" /&gt; &lt;asp:TemplateField HeaderText="Qty"&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID="txtQty" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:RequiredFieldValidator ID="reqQty" runat="server" ControlToValidate="txtQty" ErrorMessage="*"&gt;&lt;/asp:RequiredFieldValidator&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID="SqlTest" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=play;Persist Security Info=True;User ID=user;Password=userpassword" SelectCommand="SELECT id, name, price FROM Products"&gt;&lt;/asp:SqlDataSource&gt; &lt;asp:Button ID="cmdTest" runat="server" Text="Submit" /&gt; </code></pre> <p>C# button click handler to get the qty entered by the user</p> <pre><code>protected void cmdTest_Click(object sender, EventArgs e) { for (int i = 0; i &lt; gvTest.Rows.Count; i++) { int qty = Convert.ToInt32(((TextBox)gvTest.Rows[i].FindControl("txtQty")).Text); // code here } } </code></pre> <p>Validation Result</p> <p><img src="https://i.stack.imgur.com/7qrsV.jpg" alt="enter image description here"></p>
 

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