0

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>
3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by padtes
1

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;
        }
           }
Votes + Comments
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;
        }
           }
0

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.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.