Solved

Trouble adding JavaScript alert to asp:LinkButton

Posted on 2008-10-01
5
1,848 Views
Last Modified: 2012-05-05
Experts,

I'm making a web form that allows a user to schedule an email to be sent at a later date.  If the user fails to specify a date for the email to be sent, I want to have a JavaScript alert popup when the user clicks the "schedule" (submit) button.  Attached is what I have so far, and it isn't working.  Please offer suggestions on how to fix/alter attached code OR I'm also open to a totally different solution as well.

Thanks,

Pete
protected void Email_ScheduleDelivery(object sender, EventArgs e)
{
    if (tbEmailDate.Text == "")
    {
        LinkButton scheduleButton = (LinkButton)FindControl(linkScheduleEmail);
        scheduleButton.Attributes.Add("onclick", "javascript:return " +
            "alert('You have not specified a Scheduled Date!')");
    }
    else
    {
        //Schedule email processing code...
    }
}

Open in new window

0
Comment
Question by:P1ST0LPETE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 0 total points
ID: 22617091
Ok, I figured it out.  I was making it too complicated:
linkScheduleEmail.Attributes.Add("onclick", "javascript:alert('You have not specified a Scheduled Date!')");

Open in new window

0
 
LVL 5

Expert Comment

by:Rikus_Trent
ID: 22617144
The issue is that your method is happening after the page_load event so the attribute never actually getting written to the page.

You can do a few different things to solve this...

1) move this validation to clientside. Call a client-side javascript method onClientClick to validate and trigger the LinkButton alert. Be sure to return false on failure to keep the postback from happening each time regardless.

2) use asp.net validation controls to validate the value. you can use validation summary control to display the error message in a javscript alert display format if you wish.

3) keep the validation client side as you have now, but use a stringbuilder and RegisterClientScriptBlock to trigger the javascript.

        StringBuilder sb = new StringBuilder();
        sb.Append("<script language='javascript'>");
        sb.Append("alert('You have not specified a Scheduled Date!');");
        sb.Append("</script>");

        Type t = this.GetType();
        if (!ClientScript.IsClientScriptBlockRegistered(t, "DateAlert"))
            ClientScript.RegisterClientScriptBlock(t, "DateAlert", sb.ToString());
0
 
LVL 5

Expert Comment

by:Rikus_Trent
ID: 22617194
Wow you're right... it does work.... Well I guess you do learn something everyday.
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 22617425
Actually it sort of works...
You are right that my original code would not apply the JavaScript to the LinkButton until the linkbutton was actually pushed once and the eventhandler was fired.  So in effect I would have to press the button twice to see the popup.  So I added my solution to the Page_Load event and it is working fine.  New problem though now.....how to remove the JavaScript if the textbox holding the date is not blank?
I know I can use :

linkScheduleEmail.Attributes.Remove("onclick");

But where would I put that in the code to fire BEFORE the schedule button was pressed?

I think your solution of adding client side JavaScript called from an OnClientClick event would be the way to go, but not sure exactly how to do that.  Not very experienced in JavaScript just yet....
0
 
LVL 5

Expert Comment

by:Rikus_Trent
ID: 22618774
Here's a quick example of using javascript with the onclientclick...


//HTML file...
 
<head>
    <script language="javascript" type="text/javascript">
        function validateDate()
        {
            var txtDate = document.getElementById('textbox id');
            if(txtDate.value == '')
            {
                alert('your message here');
                return false; //this will stop the postback and email processing
            }
        }
    </script>
</head>
 
 
 
//linkbutton tag
 
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="validateDate();">LinkButton</asp:LinkButton>

Open in new window

0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question