samy
samy

Reputation: 1969

ImageButton and runat = server property

im doing some test with the flicker API and i got a wierd problem afther i changed some controls in my code.

when i first tried to requst images i stored them in a Image and every thing work great but i want to do other stuff to the image so i tried using ImageButton class and i get an exception that it got to have run="server" property.

i adding all the controls programmticly.

what is wierd for me is that the class Image didnt gave any excpetion but using a class that inherit from him did.

so my qustion is: it possibly to add and check if the control have the runat="server" property via c# code?

(sorry for my english)

here is some of my code:

 protected void Page_Load(object sender, EventArgs e)
{

    string key = "*****************";
    string secret = "***********";
    MyFlicker myFlicker = new MyFlicker(key, secret);
    int photoCount = myFlicker.coll.Count;
    try
    {
        string[] urls = myFlicker.GetPhotosURLS();
        string[] desc = myFlicker.GetPhotosDescreptions();
        string[] Titels = myFlicker.GetPhotosTitle();

        Panel[] panels = new Panel[photoCount];
        ImageButton[] images = new ImageButton[photoCount];
        Label[] descreptions = new Label[photoCount];
        Label[] titles = new Label[photoCount];

        for (int i = 0; i < photoCount; i++)
        {
            panels[i] = new Panel();
            images[i] = new ImageButton();
            descreptions[i] = new Label();
            titles[i] = new Label();


            titles[i].Text = Titels[i] + ":כותרת התמונה";
            images[i].ImageUrl = urls[i];

            if (descreptions[i] != null)
                descreptions[i].Text ="תיאור התמונה: " + desc[i];
            else
                descreptions[i].Text = "descreption was not found!";

            panels[i].ID = "panel" + i;
            images[i].ID = "image" + i;
            descreptions[i].ID = "des" + i;
            titles[i].ID = "titles" + i;

            panels[i].Controls.Add((Label)titles[i]);
            panels[i].Controls.Add((Label)descreptions[i]);
            panels[i].Controls.Add((Image)images[i]);
            this.Controls.Add((Panel)panels[i]);




        }


    }

    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }



}

here is the apsx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Copy_of_galleryControlTest_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></title>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

Upvotes: 1

Views: 856

Answers (1)

Adrian Iftode
Adrian Iftode

Reputation: 15683

After doing a test similar to this code, I got this error

Control 'ctl03' of type 'ImageButton' must be placed inside a form tag with runat=server.

then I changed from

this.Controls.Add((Panel)panels[i]);

to

form1.Controls.Add((Panel)panels[i]);

And it makes sense now.. ImageButton is a submit control ( a control which would submit the form), so this why this error, the control needd to be a child of the form1 and not outside it

Another workaround is to override this method, but I recommend the first one

public override void VerifyRenderingInServerForm(Control control)
{
    ;
}

Upvotes: 2

Related Questions