Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C# switch statement to change image state

Back to Forum: Tech Off
  • User profile image
    Xenon101

    I'm writing a webpage in ASP.net 2.0 with C#...

    I have 8 images, they are:
    djs_nav_jarrad.gif
    djs_nav_dima.gif
    djs_nav_shorty.gif
    djs_nav_franky.gif
    &
    djs_nav_jarrad_selected.gif
    djs_nav_dima_selected.gif
    djs_nav_shorty_selected.gif
    djs_nav_franky.gif

    When Default.aspx?id=1 I want to have djs_nav_jarrad_selected.gif to appear in place of djs_nav_jarrad.gif. I've been told the best way to do it is by C# switch statement but I can't get it working.

    override protected void OnInit(EventArgs e) { this.Load += new System.EventHandler(this.Page_Load); base.OnInit(e); }

    public void Page_Load(EventArgs e, Object sender)
    {
    switch ( Request.QueryString("id") ) {
        case "1": imgNavbar.Src = "images/djs_nav_jarrad_selected.gif";
        default :
            imgNavBar.Src = "images/djs_nav_jarrad.gif";
    }
    }

    that's in Default.aspx.cs

    and in Default.aspx is

    <img runat="server" id="imgNavbar" alt="Jarrad" />

    however this isn't working... any ideas?

  • User profile image
    JChung2006

    Don't forget your break; statements.

  • User profile image
    Xenon101

    I've never written C# before, these are my first lines of code lol. I don't even know what a break; statement is Embarassed

  • User profile image
    wacko

    you need to add a break in your switch statement

    switch(string)
    {
        case 1:
           //do code
           break;

        defualt:
    break;
    }


    that is a complete switch statement, you need to have the breaks after your code in each case

  • User profile image
    Xenon101

    Ok that's all good. Still getting a few errors though.
    Not sure what these are doing:

    protected System.Web.UI.HtmlControls.HtmlImageControl imgNavbar;

    override protected void OnInit(EventArgs e) { this.Load += new System.EventHandler(this.Page_Load); base.OnInit(e); }

    public void Page_Load(EventArgs e, Object sender)


  • User profile image
    JChung2006

    The first line is declaring a variable named imgNavbar so you can reference your HTML Image control in code.

    The second line is a function that executes when the page initializes.  It tells your code to run the Page_Load function when the page handles its Load event.

    The third line is the declaration of a Page_Load function which is called when the page is loaded (see above).

    I recommend reading a C# primer before trying to do much more.

  • User profile image
    Minh

    protected System.Web.UI.HtmlControls.HtmlImageControl imgNavbar;

    When you drag & drop a control onto a page, VS will add this line "behind the scene" for you.



    public void Page_Load(EventArgs e, Object sender)

    This function is the "Load" event. It lets you run any initialization code -- like loading a table from a database, check security of a page, etc...



    override protected void OnInit(EventArgs e) { this.Load += new System.EventHandler(this.Page_Load); base.OnInit(e); }

    This line tells the page event system to run the Page_Load function as part of a lower-level system initialization routine.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.