views:

39

answers:

2

Hi guys, I have this label that i want to fadeOut after button event clicked. I am using a MasterPage. And the Script Manager is declared on MasterPage. In Defaulst.aspx i have:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="Server">
    <script type="text/javascript" src="scripts/jquery-1.4.1.min.js">
        $(function () {
            $("input[id$='btnShowDate']").click(function () {
                $("span[id$='lblStatus']").fadeOut("slow");
            });
        });
    </script>
    <asp:UpdatePanel runat="server" ID="uP">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnShowDate" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" ID="lblStatus" />
            <div>
                <asp:Button runat="server" ID="btnShowDate" Text="Show Today`s Date" OnClick="btnShowDate_Click" /></div>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

And on the CodeBehind i have:

protected void btnShowDate_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
lblStatus.Text = DateTime.Now.Date;
}

The problem is that the label is not fading Out after the button clicked. Does someone has any idea on how to handle this problem? Thank you.

+3  A: 

The ID isn't what you think it is in the rendered page, ASP.Net mangles it a bit, like this:

<span id="container_container2_lblStatus">Stuff</span>

So you need an attribute-ends-with selector, like this:

$(document).ready(function() {
  $("span[id$='lblStatus']").fadeOut("slow");
});

The to make it happen on click, add it as a .click() handler, like this:

$(function() {
  $("input[id$='btnShowDate']").live('click', function() {
    $("span[id$='lblStatus']").fadeOut("slow");
  });
});

A cleaner alternative is to add a class to each control, for example:

<asp:Label runat="server" id="lblStatus" CssClass="status" />
//and...
<asp:Button runat="server" id="btnShowDate" CssClass="showDate" ... />

The use those classes as your selector, for example:

$(function() {
  $(".showDate").live('click', function() {
    $(".status").fadeOut("slow");
  });
});

Since the button's getting replace in an update panel, you want .live() here, so it works after postback as well.

Nick Craver
I like to use CSS classes on my ASP.NET controls to select them in jQuery, but Nick's solution works just as well.
dave thieben
@dave - As do I, was already adding the option when you commented...trying to demonstrate the actual ID problem as well in this case though, since the OP seems unaware of it.
Nick Craver
even better. ;)
dave thieben
Thanks for the answer but still it doesn`t works. The label appears after the button clicked and does not fade out.
sacrament
@sacrament - Which of the above solutions are you using? Can you update with your current code?
Nick Craver
I am using this code:$(function() { $("input[id$='btnShowDate']").click(function() { $("span[id$='lblStatus']").fadeOut("slow"); });});
sacrament
@sacrament - Can you post what your rendered HTML looks like?
Nick Craver
Hi Nick, i just edited the code above so take a look at html
sacrament
@sacrament - What is `btnAddComment`? In your code all I see is `btnShowDate`.
Nick Craver
its this code: protected void btnShowDate_Click(object sender, EventArgs e) { System.Threading.Thread.Sleep(3000); lblStatus.Text = DateTime.Now.ToShortDateString(); }
sacrament
@sacrament - I still don't see a `btnAddComment`, where is **addComment**? That's a different ID altogether from `btnShowDate`.
Nick Craver
i am sorry i just forgot to change the id of that button so instead of btnAddComment should be btnShowDate
sacrament
@sacrament - Can you post your rendered markup, what the page looks like in the browser in maybe a fiddle or something? You're posting all sorts of code that doesn't match, so I'm not sure what your page looks like, try putting your HTML here and hitting update, then comment the link: http://jsfiddle.net/ZPBFm/
Nick Craver
<div id="ctl00_MainContentPlaceHolder_uP"> <span id="ctl00_MainContentPlaceHolder_lblStatus"></span> <div> <input type="submit" name="ctl00$MainContentPlaceHolder$btnShowDate" value="Show Today`s Date" id="ctl00_MainContentPlaceHolder_btnShowDate" /></div> </div> <div id="ctl00_MainContentPlaceHolder_upProgress" style="display:none;"> <div> <img id="ctl00_MainContentPlaceHolder_loader" src="App_Themes/default/images/6.gif" style="border-width:0px;" /> </div> </div></div>
sacrament
@sacrament - It's working for me here: http://jsfiddle.net/nick_craver/6v5zc/ Are you getting a JavaScript error, perhaps not including jQuery correctly?
Nick Craver
No i dont get a javascript error but the label does not fadeOut after clicking. I dont know where and what the error is.
sacrament
@sacrament - Do you have a page I can see? Something outside the question isn't working quite right, but given all your info it still works, so I'd need to see the actual page to see what's not working.
Nick Craver
Is there any way i can post the page here?
sacrament
@sacrament - Try posting the entire *rendered* page (like the output you commented) here: http://pastebin.com/
Nick Craver
http://pastebin.com/pJKY32bX
sacrament
@sacrament - You can't have any inside a `<script>` tag with a `src`, it needs to be a separate tag, like this: http://jsfiddle.net/nick_craver/tydYy/
Nick Craver
Nick, Thanks for your patience but still doesn`t work. I tried everything, i read that in a page with script manager i should register script block but i have no idea why this doesn`t work on my page.
sacrament
@sacrament - You should be getting *some* error, are you running Firefox with firebug or chrome to get a decent error console?
Nick Craver
Yes i am running Firefox with firebug. Right now i tried it and it says that " Failed to load source for: .../jquery-1.4.1.min.js
sacrament
@sacrament - then that's your error, jQuery isn't even loading properly, have you considered including it from the google cdn?, for example: `<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>`
Nick Craver
I just did that, but still it doesn`t work. Forget it Nick i dont wanna waste your time. I believe the problem is with script manager. I have to declare some code in Default.aspx.cs.
sacrament
By the way thanks a lot for your time Nick , I appreciate it.
sacrament
@sacrament - You shouldn't have to, here's your page working: http://jsbin.com/oribu4/3 All I did was strip some script...you just have to fix your JavaScript errors.
Nick Craver
@sacrament - anytime, I would recommend accepting answers, but doesn't seem we solved your issue, I think it's just a matter of references to your script being correct, but whatever the error you're seeing, that's the reason it doesn't work currently.
Nick Craver
I will accept the answer Nick. Thank you again.
sacrament
A: 

Ok i figured out how to handle jQuery in update panel. here is the code:

<script type="text/javascript" src="scripts/jquery-1.4.1.min.js">
    </script>
    <script type="text/javascript">
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function () {
            $(document).ready(function () {
                $("span[id$='lblStatus']").delay(3000).fadeOut(4000, function () {
                    $(this).innerHTML("");
                });
            });
        });

</script>

Maybe others who have issues could use it and handle it. Thank you

sacrament