ASP.Net - Image on Button Click

Associate
Joined
9 Dec 2008
Posts
2,341
Location
Somewhere!
Hi All,

I'm very new to ASP and I'm just having a play around at the moment and I was wondering how to do the following;

If I have a drop down box, and when certain things are selected, I want certain images to display once the submit button has been pressed.

For Example.. what would I put if i had an England.jpg

If DropDownList.Text = "England" Then
????????
End If

Thank you in advance..
 
Well, clicking a submit button does a postback, so you'll need some code to show the image on the postback - either explicitly checking for a postback, or setting a value in the ViewState or Session would probably be the usual way.
Exactly what is required depends on where you want to show the image.

If you can give a bit more information about what you're trying to do we'll probably be able to give more advice.
 
If you had an image component:

Code:
<asp:Image runat="server" ID="DropDownImage" Visible="false" />

Then in your code behind OnClick event for the button something like this (C# as I don't do VB.net)

Code:
DropDownImage.ImageUrl = "images/" + DropDownList.Text;
DropDownImage.Visible = true;
 
So the image is (but isnt) there when you select it in the drop down, but then it actualy becomes visible on the page once Submit is selected?
 
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>Please Select</asp:ListItem>
            <asp:ListItem>England</asp:ListItem>
            <asp:ListItem>Scotland</asp:ListItem>
        </asp:DropDownList>&nbsp;<br />
        <br />
        <asp:Image ID="Image1" runat="server" ImageUrl="blank.jpg" /><br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

code behind

Code:
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case DropDownList1.SelectedValue
            Case "England"
                Image1.ImageUrl = "./England.jpg"
            Case "Scotland"
                Image1.ImageUrl = "./Scotland.jpg"
            Case Else
                Image1.ImageUrl = "./blank.jpg"
        End Select
    End Sub
End Class

instead of the case you could have
Image1.ImageUrl = DropDownList1.SelectedValue & ".jpg"
but that does mean you have to have the value in the ddl the same as your images.
 
Ahh, got it working in the end with If statements.. I'm sure there is a much better way of doing it than the way I have done.. but it works and that's all I'm really worried about for now :) Thanks all!
 
Back
Top Bottom