locked
Call javascript from C# RRS feed

  • Question

  • I can not see mto figure this out. I have a usercontrol using scriptmanager for some update panels. all I am trying to do is add a validator that calls a javascript alert if things are missing. see the code below. it does not throw an error but I can nto see m to get the alert to work. any ideas?
    protected void BtnCalculate_Click(object sender, EventArgs e)
        {
            if ((DdlLoanCalcState.SelectedIndex > 0) && ((DdlLoanCalcCounty.SelectedIndex > 0) || (DdlLoanCalcCity.SelectedIndex > 0)))
            {
                string cityCounty = "";
                int countyID;
                if (DdlLoanCalcCounty.SelectedIndex > 0)
                {
    
                    cityCounty = DdlLoanCalcCounty.SelectedItem.Text + " County, ";
                    countyID = int.Parse(DdlLoanCalcCounty.SelectedValue);
    
                }
                else
                {
    
                    cityCounty = DdlLoanCalcCity.SelectedItem.Text + ", ";
                    DataSet GetCounty = new DataSet();
                    GetCounty = LoanLimits.GetCountyByCity(int.Parse(DdlLoanCalcCity.SelectedValue));
                    countyID = int.Parse(GetCounty.Tables[0].Rows[0]["CountyID"].ToString());
    
                }
    
    
    
                DataSet GetLoanLimit = new DataSet();
    
                GetLoanLimit = LoanLimits.GetLoanLimitByCounty(2010, countyID);
                int loanLimit = int.Parse(GetLoanLimit.Tables[0].Rows[0]["LoanLimit"].ToString());
    
                string totalDown = Regex.Replace(TxtDownPayment.Text, @"[^\d\.]*", "");
    
                TxtDownPayment.Text = String.Format("{0:0.##}", totalDown);
    
                string downPayementText = " with a down payment of " + decimal.Parse(TxtDownPayment.Text).ToString("C0");
    
                if (TxtDownPayment.Text == "0")
                {
                    downPayementText = " with no down payment";
                }
    
    
                int totalLoanLimit = ((int.Parse(TxtDownPayment.Text) * 4) + loanLimit);
                if (totalLoanLimit < 0) { totalLoanLimit = 0; }
    
                LblTotalLoan.Text = "The maximum VA loan amount for a property in  " + cityCounty + DdlLoanCalcState.SelectedItem.Text + downPayementText + " is " + decimal.Parse(totalLoanLimit.ToString()).ToString("C0");
    
            }
            else
            {
    
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Your Message');", true); 
    
               
                
            }
    
    
            
        }
    Thursday, March 4, 2010 6:03 PM

Answers

  • This code will show JavaScript alert on PostBack:

     

            private void MessageBox(string message)
            {
                string javaScript = "<script>alert('" + message + "');</script>";
    
                if(!ClientScript.IsClientScriptBlockRegistered( "show_messagebox" ))
                    ClientScript.RegisterClientScriptBlock( GetType( ), "show_messagebox", javaScript );
            }

     

    • Proposed as answer by Brad Lane Friday, March 5, 2010 5:03 AM
    • Marked as answer by liurong luo Tuesday, March 9, 2010 9:27 AM
    Friday, March 5, 2010 1:01 AM

All replies

  • I can not see mto figure this out. I have a usercontrol using scriptmanager for some update panels. all I am trying to do is add a validator that calls a javascript alert if things are missing. see the code below. it does not throw an error but I can nto see m to get the alert to work. any ideas?
    protected void BtnCalculate_Click(object sender, EventArgs e)
        {
            if ((DdlLoanCalcState.SelectedIndex > 0) && ((DdlLoanCalcCounty.SelectedIndex > 0) || (DdlLoanCalcCity.SelectedIndex > 0)))
            {
                string cityCounty = "";
                int countyID;
                if (DdlLoanCalcCounty.SelectedIndex > 0)
                {
    
                    cityCounty = DdlLoanCalcCounty.SelectedItem.Text + " County, ";
                    countyID = int.Parse(DdlLoanCalcCounty.SelectedValue);
    
                }
                else
                {
    
                    cityCounty = DdlLoanCalcCity.SelectedItem.Text + ", ";
                    DataSet GetCounty = new DataSet();
                    GetCounty = LoanLimits.GetCountyByCity(int.Parse(DdlLoanCalcCity.SelectedValue));
                    countyID = int.Parse(GetCounty.Tables[0].Rows[0]["CountyID"].ToString());
    
                }
    
    
    
                DataSet GetLoanLimit = new DataSet();
    
                GetLoanLimit = LoanLimits.GetLoanLimitByCounty(2010, countyID);
                int loanLimit = int.Parse(GetLoanLimit.Tables[0].Rows[0]["LoanLimit"].ToString());
    
                string totalDown = Regex.Replace(TxtDownPayment.Text, @"[^\d\.]*", "");
    
                TxtDownPayment.Text = String.Format("{0:0.##}", totalDown);
    
                string downPayementText = " with a down payment of " + decimal.Parse(TxtDownPayment.Text).ToString("C0");
    
                if (TxtDownPayment.Text == "0")
                {
                    downPayementText = " with no down payment";
                }
    
    
                int totalLoanLimit = ((int.Parse(TxtDownPayment.Text) * 4) + loanLimit);
                if (totalLoanLimit < 0) { totalLoanLimit = 0; }
    
                LblTotalLoan.Text = "The maximum VA loan amount for a property in  " + cityCounty + DdlLoanCalcState.SelectedItem.Text + downPayementText + " is " + decimal.Parse(totalLoanLimit.ToString()).ToString("C0");
    
            }
            else
            {
    
                System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "AlertBox", "alert('Your Message');", true); 
    
               
                
            }
    
    
            
        }
    
    http://social.msdn.microsoft.com/Search/en-US/?query=Call%20javascript%20from%20C%23&rq=meta:Search.MSForums.ForumID(24becc9b-b984-47b2-a748-a62e38c0066f)+site:microsoft.com&rn=Visual+C%23+General+Forum
    Just Be Humble Malange!
    Thursday, March 4, 2010 7:05 PM
  • You can't call client side scripts from server side. Visit forums.asp.net if you need help in AJAX.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    Thursday, March 4, 2010 7:41 PM
  • This code will show JavaScript alert on PostBack:

     

            private void MessageBox(string message)
            {
                string javaScript = "<script>alert('" + message + "');</script>";
    
                if(!ClientScript.IsClientScriptBlockRegistered( "show_messagebox" ))
                    ClientScript.RegisterClientScriptBlock( GetType( ), "show_messagebox", javaScript );
            }

     

    • Proposed as answer by Brad Lane Friday, March 5, 2010 5:03 AM
    • Marked as answer by liurong luo Tuesday, March 9, 2010 9:27 AM
    Friday, March 5, 2010 1:01 AM
  • Hello everybody,

    my question is that how can i pass a parameter value to javascript function using

     <script type="text/javascript" language="javascript">    
        function newinput(wording) { 
          document.getElementById('DivExample').innerHTML = "<p>" + wording + "</p>";      
        } 
      </script>
    
     <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
              onselectedindexchanged="DropDownList1_SelectedIndexChanged">
              <asp:ListItem Text="item 0"></asp:ListItem>
              <asp:ListItem Text="item 1"></asp:ListItem>
              <asp:ListItem Text="item 2"></asp:ListItem>
            </asp:DropDownList>
            <asp:Label ID="RenderLabel" runat="server" Text="Rerender" Visible="false" ></asp:Label>
          </ContentTemplate>
        </asp:UpdatePanel>
        <div id="DivExample" style="clear:both;"></div>
    
     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
          ScriptManager scriptManager = ScriptManager.GetCurrent(Page);
          ScriptManager.RegisterClientScriptBlock(RenderLabel, RenderLabel.GetType(), "runtime", "newinput(You have selected " + DropDownList1.SelectedValue + ");", true);
        }

    i have tried the above code ,but no idea how to pass the value to js function using ScriptManager.RegisterClientScriptBlock.

    please share your work experience with respect to this issue.

    Thanks in advance

    Thursday, February 17, 2011 11:49 AM