JavaTheScript
JavaTheScript

Reputation: 137

Choose to show a button or not in Page_Load

Using: C# - asp.net

Problem: In Page_Load I want to make it so if the destination URL does not contain an image the button doesn't show on the page.

I've shaved a lot of my code out of the sample so it's not bogged down, but on my actual code it will pop the image in a new window and show it if it exists, all of that is working. If it doesn't exist and I click the button it just does nothing. I would like the button not to show up at all if an image doesn't exist, but I don't even know what to browse the internet for to begin to figure this problem out.

    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += new EventHandler(this.Open_Click);
    }


    protected void Open_Click(object sender, EventArgs e)
    {
         try
        {
            webResponse = webRequest.GetResponse();
        }
        catch 
        {
            ImageExists = false;
        }

        if (ImageExists == true)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + PathToFolder + "');", true);
        }

If that is not possible to do how do you load a "page not found" image in a path like: http://company.com/images/notfound.jpg or anyone have better advice? Honestly I don't know if my catch is even working. I'm still learning C# so my debugging skills are very much lacking.

Upvotes: 0

Views: 101

Answers (1)

Nick
Nick

Reputation: 2907

You need to move the check of the image existance in the page load and then in the page load you would have to do:

  try
  {
      webResponse = webRequest.GetResponse();
  }
  catch 
  {
      ImageExists = false;
      Button1.Visible = false;
  }

Upvotes: 1

Related Questions