locked
Need help with this unruly ASPX RRS feed

  • Question

  • User-1168192436 posted

    I'm ok with ASP not great but I get by, but I'm baffled by this bugger <%# DataBinder.Eval(Container.DataItem, "qty") %>. The qty shows the quantity of the product in my web store that runs on PDshop. What I need to do is be able to evaluate the qty somthing like this:

    <% if qty < 1 then %>

       Do something 

    <% end if %>

    Thank you for any and all help. :) 

    Wednesday, October 24, 2018 12:48 AM

All replies

  • User2103319870 posted

    What I need to do is be able to evaluate the qty somthing like this:

    <% if qty < 1 then %>

       Do something 

    <% end if %>

    You could use Conditional Operator like below

    <asp:Label ID="lblFirstName" runat="server" Text='<%#(DataBinder.Eval(Container, "DataItem.qty").ToString()=="0") ? "Value is Zero":DataBinder.Eval(Container, "DataItem.qty")%>'></asp:Label>

    Wednesday, October 24, 2018 1:23 AM
  • User61956409 posted

    Hi Slee The Sloth,

    If you'd like to use IF ELSE condition in TemplateField (ItemTemplate), you can try:

    In C#:

    <asp:TemplateField HeaderText="QTY">
        <ItemTemplate>
            <asp:Label Text='<%# Convert.ToInt32(Eval("qty")) < 1 ? "qty is less than 1" : Eval("qty") %>'
                runat="server" />
        </ItemTemplate>
    </asp:TemplateField>

    In VB.NET:

    <asp:TemplateField HeaderText="QTY">
        <ItemTemplate>
            <asp:Label Text='<%# If(Eval("qty") < 1, "qty is less than 1", Eval("qty")) %>' runat="server" />
        </ItemTemplate>
    </asp:TemplateField>

    With Regards,

    Fei Han

    Wednesday, October 24, 2018 1:54 AM
  • User-1168192436 posted

    Thank you guys! This is great. I have one last question, is there a way I can use the VB.NET version from Fei Han, can I set a regular ASP variable that can be used in <% here %> instead of just writing " qty is less than 1 " on the screen?  Or can I make it display an image instead of the text?

    Wednesday, October 24, 2018 10:12 AM
  • User61956409 posted

    Hi Slee The Sloth,

    can I set a regular ASP variable that can be used in <% here %> instead of just writing " qty is less than 1 " on the screen?

    You can refer to the following code snippet:

    <asp:TemplateField HeaderText="QTY">
        <ItemTemplate>
            <span style='<%# If(Eval("Order") > 1, "visibility:visible", "visibility:hidden") %>'><%=testval %></span>
        </ItemTemplate>
    </asp:TemplateField>

    In code behind:

    Public testval As String = "test variable"

    Or can I make it display an image instead of the text?

    If you want to deal with complex code logic, I recommend that you can achieve it from code behind in data control's OnRowDataBound event.

    With Regards,

    Fei Han

    Thursday, October 25, 2018 5:40 AM