ASP.NET - How can I change the position of a background image when a button is clicked?
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?
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;
});
});
The problem is that style is defined on server side AFTER the button is clicked. The simplified scenario is like that:
- Server renders the page for the first time.
- User clicks the button.
- Server invokes the button_click event.
- Server renders the page.
- 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.
精彩评论