ASP.NET Conditional Statements

Discussion in 'ASP .Net' started by Brian, Jul 20, 2007.

  1. Brian

    Brian Guest

    I have an ASP.NET page that uses a FormView and SqlDataSource. Within my
    page I want to change a string if a column within my database record is a
    certain value. Here is some sample code:

    <asp:Content ID="Content2" ContentPlaceHolderID="Main" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" Width="123px"
    DataKeyNames="property_key" DataSourceID="SqlDataSource1">
    <ItemTemplate>
    <strong style="font-size: 12px">For Sale</strong>
    </ItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
    ConnectionStrings:MyDatabase %>"
    SelectCommand="SELECT * from property_info WHERE property_key =
    @property_key">
    <SelectParameters>
    <asp:QueryStringParameter DefaultValue="0" Name="property_key"
    QueryStringField="id"
    Type="String" />
    </SelectParameters>
    </asp:SqlDataSource>
    </asp:Content>

    I know how to use "Eval" to display values from the current database record
    but I want to do is change the "For Sale" string to "For Rent" when a
    database column is a certain value. I wasn't sure if this would be better
    handled with C# code behind - and just use a placeholder or if I should use
    code within the page. I'd almost prefer to use a place holder but I'm not
    sure how to access to current database record from the C# code behind. Or
    if this should be handled within the page, I don't know the syntax to do a
    conditional statement here. Ever tried searching MSDN for "conditional
    statement ASP.NET"? :) Please excuse my ignorance but I haven't done much
    ASP.NET work.

    Any suggestions would be greatly appreciated.

    TIA

    Brian
    Brian, Jul 20, 2007
    #1
    1. Advertising

  2. Brian

    JC Guest

    You could use a SQL case statement and return the correct value from the
    database.

    "Brian" <> wrote in message
    news:D...
    >I have an ASP.NET page that uses a FormView and SqlDataSource. Within my
    >page I want to change a string if a column within my database record is a
    >certain value. Here is some sample code:
    >
    > <asp:Content ID="Content2" ContentPlaceHolderID="Main" Runat="Server">
    > <asp:FormView ID="FormView1" runat="server" Width="123px"
    > DataKeyNames="property_key" DataSourceID="SqlDataSource1">
    > <ItemTemplate>
    > <strong style="font-size: 12px">For Sale</strong>
    > </ItemTemplate>
    > </asp:FormView>
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:MyDatabase %>"
    > SelectCommand="SELECT * from property_info WHERE property_key =
    > @property_key">
    > <SelectParameters>
    > <asp:QueryStringParameter DefaultValue="0" Name="property_key"
    > QueryStringField="id"
    > Type="String" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    > </asp:Content>
    >
    > I know how to use "Eval" to display values from the current database
    > record but I want to do is change the "For Sale" string to "For Rent" when
    > a database column is a certain value. I wasn't sure if this would be
    > better handled with C# code behind - and just use a placeholder or if I
    > should use code within the page. I'd almost prefer to use a place holder
    > but I'm not sure how to access to current database record from the C# code
    > behind. Or if this should be handled within the page, I don't know the
    > syntax to do a conditional statement here. Ever tried searching MSDN for
    > "conditional statement ASP.NET"? :) Please excuse my ignorance but I
    > haven't done much ASP.NET work.
    >
    > Any suggestions would be greatly appreciated.
    >
    > TIA
    >
    > Brian
    JC, Jul 20, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,158
    mfmehdi
    Oct 20, 2006
  2. Cowboy Bob
    Replies:
    1
    Views:
    3,378
    Matt Parker
    Jan 29, 2004
  3. Harry George
    Replies:
    6
    Views:
    358
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    731
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    248
    John Crichton
    Jul 12, 2010
Loading...

Share This Page