【问题标题】:ImageButton and runat = server propertyImageButton 和 runat = 服务器属性
【发布时间】:2012-02-26 21:37:05
【问题描述】:

我正在使用闪烁 API 进行一些测试,但我在代码中更改了一些控件后遇到了一个奇怪的问题。

当我第一次尝试请求图像时,我将它们存储在图像中,一切都很好,但我想对图像做其他事情,所以我尝试使用 ImageButton 类,但我得到一个异常,它必须运行 =服务器”属性。

我以编程方式添加所有控件。

对我来说奇怪的是 Image 类没有给出任何异常,而是使用从他继承的类。

所以我的问题是:是否可以通过 c# 代码添加并检查控件是否具有 runat="server" 属性?

(对不起我的英语)

这是我的一些代码:

 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);
    }



}

这里是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>

【问题讨论】:

  • 哪行代码准确生成了您描述的错误?
  • 你为什么不用中继器什么的?
  • @ShadowWizard 它没有告诉我一些 reson 以及它自己在 try and catch 中的代码。
  • 99% 你得到的错误不是直接因为你发布的代码。请同时发布您的.aspx 标记,我们将拭目以待。 (添加后通知,我不会定期查看我访问的每个帖子)
  • @ShadowWizard 我添加了 aspx 代码(感谢您尝试帮助我)。

标签: c# asp.net


【解决方案1】:

在做了类似这段代码的测试后,我得到了这个错误

“ImageButton”类型的控件“ctl03”必须放在表单标签内 使用 runat=server。

然后我变了

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

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

现在这很有意义.. ImageButton 是一个提交控件(一个将提交表单的控件),所以这就是为什么这个错误,控件需要是 form1 的子级而不是它之外

另一种解决方法是覆盖此方法,但我推荐第一个

public override void VerifyRenderingInServerForm(Control control)
{
    ;
}

【讨论】:

  • 我用你提供给我的解决方案更改了编码,但它仍然产生相同的异常并且它很奇怪,因为代码在 try and catch 中并且仍然没有显示哪行代码抛出它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 2010-11-11
  • 2011-06-03
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多