0

I am using alert message using java-script to display some kind of warning when some conditions are not met. My only problem is when the alert message pops up and the users reads the message and clicks Ok, then it goes back to the original page but the page shifts to the left side. Normally my page shows in the center but only when the java-script runs then it shifts to the left side of the page. If i copy the same URL and paste in another page then everything will be aligned and will show in the center. How can i fix this? is there another alerting message like jquery or something else that works nice. Pls. help. thnaks

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web.Routing;

public partial class HomePage : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        string ID = Page.RouteData.Values["ID"] as string;

        if (!IsPostBack)
        {

            BindData_Action();

        }       
    }


protected void btnApproval_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Description, Target_Date FROM MyTable  WHERE ID = '" + Request.QueryString["ID"] + "'", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {

         //if the condition above is true i am sending an email

        }
        else
        {
            Response.Write("<script>alert('Before requesting Approval additional info.  Thanks');</script>");
        }
    }

}

1 Answer 1

1

Using Response.Write can do some funky things to your styling. Instead of doing it in your C# code, break out the javascript separately.

One way to do it is to use RegisterStartUpScript in your C# - this tells the compiler to run this script after the page is loaded:

If (!Page.ClientScript.IsStartupScriptRegistered("showAlert") {
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "showAlert", "showAlert();", True);
}

And then, right after your C# code section on your page ends, add this:

<script type="text/javascript">
    function showAlert() {
        alert('Before requesting Approval, you MUST add at least one Action Item.  Thanks');
    }
</script>

All this is doing is separating out the javascript that you had in your Response.Write.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks David, i am affraid that i am not following you when you say break javascript separately. Can you show me what you mean by that? thanks
Thanks David for the detailed info but i am not sure where exactly i need to put these codes. I have tried everything with my little knowledge in this world but failed miserably. I have updated my code above and i would appreciate if u could guide me again. i feel i am an idiot again. thanks
Ok, it looks like you are using a codebehind page. In that case, replace your response.write with the first block of code I posted. Then put the second block of code in your aspx file in the head section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.