【问题标题】:Settings the ImageUrl in an ImageButton on a master page with an AppSettings key使用 AppSettings 键在母版页上的 ImageButton 中设置 ImageUrl
【发布时间】:2012-04-12 03:33:39
【问题描述】:

我正在尝试在我的母版页中使用 AppSettings 显示 asp:ImageButton 的 ImageUrl,如下所示:

 View.Master:
 ....
 <asp:ImageButton ID="aspShowHideButton" ImageUrl='<%# System.Configuration.ConfigurationManager.AppSettings("HomeDirectory").ToString()%>images/arrowUpButton.gif' runat="server" />

不幸的是,当我在浏览器中打开它时,这是正在呈现的代码:

 <input type="image" name="ctl00$ctl00$aspContentMain$aspShowHideButton" id="aspContentMain_aspShowHideButton" onmouseover="ShowHideButtonMouseOver()" onmouseout="ShowHideButtonMouseOut()" src="../%3C%25#%20System.Configuration.ConfigurationManager.AppSettings(%22HomeDirectory%22).ToString()%25%3Eimages/arrowUpButton.gif" />

所以它从字面上获取我的 ImageUrl,但我希望它获取键的值,即:

  ...
  <appSettings>
        ....
        <add key="HomeDirectory" value="/" />
        ....

我试过,删除“ToString()”函数,我试过 System.Configuration.... 语句前面的“#”和“$”。我还尝试尝试让它在 Page_Load 函数中工作,使用:

 Protected Sub Page_Load(....)
    If Not IsNothing(Master.FindControl("aspShowHideButton")) Then
       Dim ShowHideButton As ImageButton = Master.FindControl("aspShowHideButton")
       ShowHideButton.ImageUrl = System.Configuration.ConfigurationManager.AppSettings("HomeDirectory") + "images/arrowUpButton.gif"
    End If

但这似乎也不起作用,我假设是因为它找不到我正在寻找的控件(即 aspShowHideButton)。

从根本上说,我想在我的 web.config 文件中有一个键/值对,它允许我更改图像的位置,并且我希望能够在我的 ImageButton:ImageUrl 中使用这个键/值对母版页,这似乎是一件非常受欢迎的事情。任何建议,方向,不胜感激!

谢谢!

【问题讨论】:

    标签: asp.net master-pages imagebutton


    【解决方案1】:

    要在服务器标签中使用 appsettings,请使用以下语法:

    <%$ AppSettings:HomeDirectory %>
    

    但是您将无法连接 ImageUrl 的后缀。请注意,如果您想在 asp.net 服务器控件中引用主目录, ~ 会成功。

     <asp:ImageButton ID="aspShowHideButton" ImageUrl="~/images/arrowUpButton.gif" runat="server"/>
    

    简单的解决方案

    在服务器端代码上初始化 ImageUrl 属性,例如 Page_Load 事件。在那里你将能够使用你想要的任何服务器代码。

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
           this.aspShowHideButton.ImageUrl = System.Configuration.ConfigurationManager.AppSettings("HomeDirectory").ToString() + "images/arrowUpButton.gif";
        }
    }
    

    现在,如果您真的需要直接在 ImageButton 标记中定义这种连接,您将需要使用代码表达式生成器。 Read about that here.

    使用代码表达式生成器,您将能够在 ImageButton 标记中使用这种语法:

    ImageUrl="<%$ Code: System.Configuration.ConfigurationManager.AppSettings("HomeDirectory").ToString() + "images/arrowUpButton.gif" %>"
    

    【讨论】:

    • @daniloquio-更新了另一种可能性。
    【解决方案2】:

    你可以试试这个:

    <asp:ImageButton runat="server" ImageUrl="<%$ AppSettings:FullPath %>images/image001.jpg" ></asp:ImageButton>
    

    (或者)有一个小工作可以解决这个问题:

    Web.Config:

    <appSettings> 
    <add key="testKey" value="images/up.gif" />
    </appSettings>  
    

    添加图片按钮:

    <asp:ImageButton ID="ImageButton1" runat="server" />
    

    您可以从代码隐藏中这样调用:

     protected void Page_Load(object sender, EventArgs e)
        {
            this.ImageButton1.ImageUrl = ConfigurationManager.AppSettings["testKey"];
        }
    

    【讨论】:

    • 您的编辑将不起作用,因为 OP 希望在 appsettings 中拥有 HomeDirectory,而不是完整路径。显然 OP 可以在 Page_Load 上设置 ImageUrl 并使用后缀连接 appsettings["homeDir"]。
    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 2014-03-04
    • 2010-11-23
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多