ASP.NET - 单击按钮时如何更改背景图像的位置?

发布于 2024-09-15 21:48:16 字数 239 浏览 0 评论 0原文

我有一个标准的 asp 按钮,单击它会触发:

protected void btnDealItem_Click(object sender, EventArgs e)
{
    divMyDiv.Style.Add("background-position", "70px 0");
}

问题是,单击按钮时背景不会向右移动 70 像素。

这是解决这个问题的正确方法还是语法问题?

I have a standard asp button and on click it triggers:

protected void btnDealItem_Click(object sender, EventArgs e)
{
    divMyDiv.Style.Add("background-position", "70px 0");
}

Problem is, when the button is clicked the background doesn't shift 70 pixels to the right.

Is this the correct way of going about this or is it a question of syntax?

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

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

发布评论

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

评论(2

别靠近我心 2024-09-22 21:48:16

您可以使用 JQuery 在客户端执行此操作: http://jquery.com/

我从您的代码中假设您不想对按钮的单击事件执行任何其他操作。 return false; 行阻止按钮将页面发回。

 $(document).ready(function () {
     $("#" + <%= btnDealItem.ClientID %>).click(function() {
          $(this).attr("style", "background-position:70px 0;" );
          return false;
     });
 });

或者

<style>
     .backgroundshift {
        background-position: 70px 0;
     }
</style>


 $(document).ready(function () {
     $("#" + <%= btnDealItem.ClientID %>).click(function() {
          $(this).addClass("backgroundshift");
          return false;
     });
 });

You could do it on the clientside with JQuery: http://jquery.com/

I am assuming from your code that you don't want to do anything else with the click event of the button. The line return false; stops the button from posting the page back.

 $(document).ready(function () {
     $("#" + <%= btnDealItem.ClientID %>).click(function() {
          $(this).attr("style", "background-position:70px 0;" );
          return false;
     });
 });

or

<style>
     .backgroundshift {
        background-position: 70px 0;
     }
</style>


 $(document).ready(function () {
     $("#" + <%= btnDealItem.ClientID %>).click(function() {
          $(this).addClass("backgroundshift");
          return false;
     });
 });
冰雪之触 2024-09-22 21:48:16

问题是样式是在单击按钮后在服务器端定义的。
简化的场景是这样的:

  1. 服务器第一次渲染页面。
  2. 用户单击按钮。
  3. 服务器调用button_click事件。
  4. 服务器呈现页面。
  5. 用户可以看到背景图像位置发生变化的按钮。

如果您想在单击按钮后立即实现更改,请通过 java 脚本使用客户端脚本。

The problem is that style is defined on server side AFTER the button is clicked.
The simplified scenario is like that:

  1. Server renders the page for the first time.
  2. User clicks the button.
  3. Server invokes the button_click event.
  4. Server renders the page.
  5. User can see the button with changed background image position.

If you want to achieve the change immediately after clicking the button use client-side scripting via java script.

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