将 CSS 应用到 .NET ImageButton

发布于 2024-11-01 03:39:19 字数 1064 浏览 3 评论 0原文

我有以下 CSS,可以将其应用于 HTML 输入标记。

#headerSearch
{
    width: 265px;
}

#headerSearch .text
{
    width: 215px;
}

#headerSearch #searchButton
{
    background: url(../images/searchButton.png) no-repeat 0 0;
    width: 36px;
    border: 1px solid #ccc;
    margin: 0;
}

#headerSearch #searchButton:hover
{
    background: url(../images/searchButton.png) no-repeat 0 -28px;
}

我应用它的 HTML...

<div id="headerSearch" class="float">
  <input id="txtSearch" class="text left" type="text" />
  <input id="searchButton" class="submit right" type="submit" value="" />
</div>

效果非常好。

但是,我想使用 ImageButton 控件而不是输入标记,因为我想要 ImageButton 的页面提交行为(当您单击它并引发单击事件等时发生),但我不确定如何进行关于将 CSS 与 ImageButton 混合使用。我尝试了类似的简单操作

<asp:ImageButton ID="ibtnSrch" runat="server" CssClass="searchBtn" onclick="ibtnSrch_Click" AlternateText="Search" />

,但发生的情况是图像在其上方的白色框中显示有一个红色 X(默认图像缺少图标)。

那么,更简洁地说,如何将优雅的 CSS 与 .NET ImageButton 混合在一起?

I have the following CSS that I can apply to an HTML input tag.

#headerSearch
{
    width: 265px;
}

#headerSearch .text
{
    width: 215px;
}

#headerSearch #searchButton
{
    background: url(../images/searchButton.png) no-repeat 0 0;
    width: 36px;
    border: 1px solid #ccc;
    margin: 0;
}

#headerSearch #searchButton:hover
{
    background: url(../images/searchButton.png) no-repeat 0 -28px;
}

And the HTML to which I apply it...

<div id="headerSearch" class="float">
  <input id="txtSearch" class="text left" type="text" />
  <input id="searchButton" class="submit right" type="submit" value="" />
</div>

It works wonderfully.

However, I want to use an ImageButton control instead of the input tag because I want the page submit behavior of the ImageButton (which occurs when you click on it and click event is raised, etc.), but I am not sure how to go about mixing CSS with the ImageButton. I tried something simple like

<asp:ImageButton ID="ibtnSrch" runat="server" CssClass="searchBtn" onclick="ibtnSrch_Click" AlternateText="Search" />

but what occurs is the image displays with a red X in a white box (the default image is missing icon) over top of it.

So, more succinctly, how do I mix elegant CSS with the .NET ImageButton?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末が日狂欢 2024-11-08 03:39:19

根据示例代码,您已将 CssClass 设置为 "searchBtn" 但没有 .searchBtn 的 CSS >

也许将其添加到您的 css 中

.searchBtn {
    background: url(../images/searchButton.png) no-repeat 0 0;
    border: 1px solid #ccc;
    margin: 0;
}

.searchBtn:hover {
    background: url(../images/searchButton.png) no-repeat 0 -28px;
}

渲染为

因为该控件是一个没有图像源的图像输入,这就是为什么你会得到一个红色的 x

based on the sample code you have set the <asp:ImageButton /> CssClass to "searchBtn" but there is no CSS for .searchBtn

perhaps add this to your css

.searchBtn {
    background: url(../images/searchButton.png) no-repeat 0 0;
    border: 1px solid #ccc;
    margin: 0;
}

.searchBtn:hover {
    background: url(../images/searchButton.png) no-repeat 0 -28px;
}

an <asp:ImageButton /> renders down to <input type="image" name="ibtnSrch" id="ibtnSrch" class="searchBtn" src="" alt="Search" style="border-width:0px;" />

since the control is an image input with no image source that is why you get a red x

悲凉≈ 2024-11-08 03:39:19

如果您将 searchButton 样式更改为一个类,那么您只需使用

<asp:Button ID="ibtnSrch" runat="server" 
            CssClass="submit right searchButton" OnClick="ibtnSrch_Click" />

您就可以将该按钮放在单独的 ValidationGroup 或设置 CausesValidation="false"

如果您想将其全部保留在客户端并在 JavaScript 中重定向到搜索页面,但也想利用您在控件上设置的 ASP.NET 验证,则可以使用 客户端 ASP.NET 验证

If you change your searchButton style to be a class, then you can just use an <asp:Button>

<asp:Button ID="ibtnSrch" runat="server" 
            CssClass="submit right searchButton" OnClick="ibtnSrch_Click" />

You can then put that button in a separate ValidationGroup or set CausesValidation="false".

If you want to keep it all client-side and do the redirect to the search page in JavaScript but also want to take advantage of the ASP.NET validation you've set up on your controls, you can use the client-side ASP.NET validation.

脱离于你 2024-11-08 03:39:19

简而言之,我不会使用 asp 图像按钮。

我将使用您当前的 html 控件,然后添加一些 javascript 以在单击提交输入时单击隐藏的 asp:Button 控件。

<div id="headerSearch" class="float">
  <input id="txtSearch" class="text left" type="text" />
  <input id="searchButton" class="submit right" type="submit" value="" onclick="<% hiddenSearch.ClientID %>.click();" />
  <asp:Button ID="hiddenSearch" runat="server" style="display:none;" />
</div>

我不太记得这是否是获取客户端 ID 的正确语法......

In short I would not use the asp image button.

I would use your current html controls and then add some javascript to click a hidden asp:Button control when your submit input is clicked.

<div id="headerSearch" class="float">
  <input id="txtSearch" class="text left" type="text" />
  <input id="searchButton" class="submit right" type="submit" value="" onclick="<% hiddenSearch.ClientID %>.click();" />
  <asp:Button ID="hiddenSearch" runat="server" style="display:none;" />
</div>

I don't quite recall if that is the correct syntax to get the client id...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文