locked
Setting max and min for each TextBox in GridView from database RRS feed

  • Question

  • User678153907 posted

    Hello, I've been trying to set the min and max for the textboxes in my GridView from my database, for instance if number of products in stock is 10, the maximum value for the textbox for that product would be 10, and so far it's either the same min and max are applied for all products, or they don't work at all.

    The latest thing I tried is using a RangeValidator, but apparently it doesn't check until data is sent to the server, but since I'm using JQuery to get Data, it isn't helpful to me.

    Here is my GridView :

    <asp:GridView runat="server" AutoGenerateColumns="false" ID="liste" CssClass="table table-bordered text-center">
                                <Columns>
                                    <asp:BoundField DataField="Libellé" ItemStyle-CssClass="libelle align-middle" HeaderText="Libellé" />
                                    <asp:TemplateField HeaderText="Quantité">
                                        <ItemTemplate>
                                            <asp:TextBox ID="quantity" runat="server" CssClass="form-control d-inline" Width="100px" type="number">1</asp:TextBox>
                                            <asp:RangeValidator Type="Integer" ControlToValidate="quantity" id="valQte" runat="server"></asp:RangeValidator>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date de début">
                                        <ItemTemplate>
                                            <asp:TextBox type="date" CssClass="form-control" runat="server" ID="dDebutCom"></asp:TextBox>
                                            <asp:RangeValidator ControlToValidate="dDebutCom" ID="valDDebut" runat="server"></asp:RangeValidator>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date de fin">
                                        <ItemTemplate>
                                            <asp:TextBox type="date" CssClass="form-control" runat="server" ID="dFinCom"></asp:TextBox>
                                            <asp:RangeValidator ControlToValidate="dFinCom" ID="valDFin" runat="server"></asp:RangeValidator>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <input type="button" id="addToCart" value="Ajouter au panier" class="btn btn-primary d-inline addcart" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

    Thanks for helping.

    EDIT: I actually found a workaround, I just showed the columns from my database, and made them hidden, that what I could target them with jquery and apply the max on the TextBoxes.

    Wednesday, March 7, 2018 12:27 PM

All replies

  • User-1838255255 posted

    Hi Doca,

    According to your description, as far as i know, if the product number is over 10, you want to show 10 in the textbox. But the data is from the code behind, then bind them to gridview in asp.net. So you can not handle the data? 

    if my understanding is right, you could handle it in the Jquery, foreach all textbox value, then handle it use if condition. 

    if my understanding is wrong, please correct me! 

    Best Regards,

    Eric Du 

    Thursday, March 8, 2018 8:56 AM
  • User678153907 posted

    Thanks for you help, but what I needed was a text box (spinner), where the max value came from the database.

    Right now what I've done is putting the data in the table, then hiding it with jquery and using its value to set the max value for each row.

    Thursday, March 8, 2018 9:43 AM
  • User-1838255255 posted

    Hi Doca,

    According to your needs, i know maybe you want to set the spinner textbox max value, this max value is from the database. I think you could use this jquery plugin to set the max value. 

    jQuery Spinner:

    http://api.jqueryui.com/spinner/ 

    you could get the max value from database then set it through JS.

    http://api.jqueryui.com/spinner/#option-max  

    Best Regards,

    Eric Du 

    Monday, March 19, 2018 1:46 AM