I am having trouble getting a button to work properly, it works the first time it is pressed but then it doesn't work again when pressed.

<script runat="server" language="C#" type="">
		public int LogoWidth = 150;		
 		public void btn1_Click(Object s, EventArgs e) {
   		LogoButton.AlternateText = "Hello World";
   		LogoWidth += 10;
   		}
	</script> 

<asp:ImageButton runat="server" id="LogoButton" ImageUrl="/SiteCollectionImages/EQCFullLogo.jpg" OnClick="btn1_Click" Height="100%" Width="100%" AlternateText="Equation Consulting"></asp:ImageButton>

Recommended Answers

Everytime you click a button upon postback your LogoWidth is reset to 150 so you won't see the effect after first click.
Two options:
1: make LogoWith static
i.e. public static void LogoWidth=150;
2: Modify your code like below:

protected void Page_Load(object sender, EventArgs e)
    {
        if …
Jump to Post

All 4 Replies

Everytime you click a button upon postback your LogoWidth is reset to 150 so you won't see the effect after first click.
Two options:
1: make LogoWith static
i.e. public static void LogoWidth=150;
2: Modify your code like below:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.LogoWidth = 150;
        }
    }
    
    public void btn1_Click(Object s, EventArgs e)
    {
        LogoButton.AlternateText = "Hello World";
        LogoWidth += 10;
    }
   
    public int LogoWidth { 
        get {
            if (ViewState["logoWidth"] == null)
            {
                ViewState["logoWidth"] = 150;
            }
            return (int)ViewState["logoWidth"];
        }
        set{
            ViewState["logoWidth"] = value;
        }
           }
commented: Very Helpful +0

Thank you for your help, but when I modify my code as you stated, I get an error "Server Error in '/' Application. Cannot complete this action. Please try again"

Any ideas on what I am doing wrong would be appreciated.

Everytime you click a button upon postback your LogoWidth is reset to 150 so you won't see the effect after first click.
Two options:
1: make LogoWith static
i.e. public static void LogoWidth=150;
2: Modify your code like below:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.LogoWidth = 150;
        }
    }
    
    public void btn1_Click(Object s, EventArgs e)
    {
        LogoButton.AlternateText = "Hello World";
        LogoWidth += 10;
    }
   
    public int LogoWidth { 
        get {
            if (ViewState["logoWidth"] == null)
            {
                ViewState["logoWidth"] = 150;
            }
            return (int)ViewState["logoWidth"];
        }
        set{
            ViewState["logoWidth"] = value;
        }
           }

hmm not sure...can you post the entire error message.
First try the option -1 i.e. just adding static to the LogoWidth variable.
Check your EventLog to see if you can find any specific error message.

if the purpose of the button click is to increase the logo by 10 each time, then you are also missing LogoButton.Width = LogoWidth ; after you increase the value of the variable on buttonClick.

As for the error, cannot find what is causing it. Can you step through in debugger at all? put a breakpoint in button click. See what line causes it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.