?
Solved

Javascript from ASP.NET code behind

Posted on 2009-05-06
1
Medium Priority
?
1,099 Views
Last Modified: 2012-05-06
I want to a confirm popup box using javascript in my asp.net page using Page.RegisterClientScriptBlock (or similiar) . Here is how I want it to work.

when the user click "submit" button,  it should check that selectedindex on one of the dropdown is > 0. If so show a confirm pop up. If Ok is pressed continue with the next step and if cancel is pressed return. It would be nice if the javascript checks for the selectedindex of drop down rather then doing it via the code. I want all this done in the code behind and not on the html side of the form.
Thanks in advance.
0
Comment
Question by:pratikshahse
[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
1 Comment
 
LVL 1

Accepted Solution

by:
CJMilner earned 2000 total points
ID: 24321431
I think I understand what you're asking for, how about this.

This example only alerts the textbox but I'm sure you can write the Javascript to check the dropdown box. If not, let me know and I'll help. This example an anonymous function will execute "onreadystatechange" of the page, that way you're giving the page time to load the controls and not get a JS Object Not Found error or null reference.

Without knowing more about how you're storing data between steps and how you're getting to your next step ignoring the drop down logic I can't really help much with the next step forwarding.

Perhaps you could load the contexts of the form into a session array before executing the code behind below? in essense saving the data before checking the drop down, that way on the next page you have the previous page's data. Moving to the next page could be achieved through the javascript: window.location.href='page.aspx'; assuming the confirm is OK and not Cancel. If cancel, just return and page would not move, just essentially reload (or do nothing if the ajax is used). You could easily throw an alert if cancel is checked if you needed to communicate with the user.

First mark up will work without a page refresh as it's using an update panel (ajax), the third code block below is going to do a round trip to the server and reload the entire page. Your call what to use.

Hope all this makes sense!
Cheers
CM
//ASP.NET Markup with AJAX UpdatePanel and Script Manager
    <asp:ScriptManager runat="server" ID="S1"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true">
        <Triggers>
            <asp:PostBackTrigger ControlID="Button1" />
        </Triggers>
        <ContentTemplate>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
            UseSubmitBehavior="False" />
      </ContentTemplate></asp:UpdatePanel>
 
 
//Code behind 
protected void Button1_Click(object sender, EventArgs e)
    {
        Page.RegisterClientScriptBlock
            ("block1","<script type=\"text/javascript\">document.onreadystatechange=function() {alert(document.getElementById('"+TextBox1.ClientID+"').value);}</script>");
    }
 
 
//Third Code block; Not AJAX
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 
            UseSubmitBehavior="False" />

Open in new window

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

On Sep 22nd 2014 Microsoft released Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1 and back in July Update Rollup 3 was released.  So we now have:   Update Rollup 1Update Rollup 2Update Rollup 3Service Pack 1Update Rollup 1 for S…
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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