Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I'm trying to display my status_lbl text label before

C#
Response.Redirect(Request.RawUrl);


I've tried with putting delay time before the redirect page function, but still it doesn't show the status_lbl text label.



C#
using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Services;
    using System.Web.Script;
    using System.Web.Security;

    namespace TagNumberWeb
    {
    public partial class Main : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string CrUserID = Request.QueryString["LogInUser"].ToString();
            string Result = Request.QueryString["Result"].ToString();

            if (!IsPostBack)
            {
                if (string.IsNullOrWhiteSpace(CrUserID) || string.IsNullOrWhiteSpace(Result))
                {
                    Response.Redirect("Login Page.aspx");
                }

                else
                {                    
                    UserID.Text = Request.QueryString["LogInUser"].ToString();
                    status_lbl.Visible = false;
                    GridView1.Visible = false;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sONbr = sONbrTextBox.Text;
            string SOLine = sOLineTextBox.Text;
            string SerialNbr = serialNbrTextBox.Text;
            string StatusCode = statusCodeComboBox.Text;
            string CrUserID = Request.QueryString["LogInUser"].ToString();

            if (string.IsNullOrWhiteSpace(sONbr) || string.IsNullOrWhiteSpace(SOLine) || string.IsNullOrWhiteSpace(StatusCode) || string.IsNullOrEmpty(SerialNbr))
            {
                status_lbl.Text = "Please fill in all the information.";
                status_lbl.Visible = true;
                GridView1.Visible = false;
                return;
            }

            else if (string.IsNullOrWhiteSpace(CrUserID))
            {
                status_lbl.Text = "Please login your account!";
                status_lbl.Visible = true;
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Please login your account!')</script>");
                Response.Redirect("Login Page.aspx");
                GridView1.Visible = false;
                return;
            }

            else if (CheckBox1.Checked == true)
            {

                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr_BCSystem"].ToString());
                conn.Open();

                SqlCommand comm = conn.CreateCommand();
                comm.CommandType = CommandType.StoredProcedure;
                comm.CommandText = "usp_TagNumberUpdate";

                comm.Parameters.AddWithValue("@sONbr", sONbr);
                comm.Parameters.AddWithValue("@SOLine", SOLine);
                comm.Parameters.AddWithValue("@SerialNbr", SerialNbr);
                comm.Parameters.AddWithValue("@StatusCode", StatusCode);
                comm.Parameters.AddWithValue("@CrUserID", CrUserID);

                SqlParameter ReturnVal = comm.Parameters.Add("@return", SqlDbType.NVarChar, 200);
                ReturnVal.Direction = ParameterDirection.Output;

                comm.ExecuteNonQuery();

                string val = (string)ReturnVal.Value;

                conn.Close();
                status_lbl.Text = val;
                status_lbl.Visible = true;
                Response.Redirect(Request.RawUrl);

            }

            else 
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr_BCSystem"].ToString());
                conn.Open();

                SqlCommand comm = conn.CreateCommand();
                comm.CommandType = CommandType.StoredProcedure;
                comm.CommandText = "usp_TagNumberUpdateNoSN";

                comm.Parameters.AddWithValue("@sONbr", sONbr);
                comm.Parameters.AddWithValue("@SOLine", SOLine);
                comm.Parameters.AddWithValue("@StatusCode", StatusCode);
                comm.Parameters.AddWithValue("@CrUserID", CrUserID);

                SqlParameter ReturnVal = comm.Parameters.Add("@return", SqlDbType.NVarChar, 200);
                ReturnVal.Direction = ParameterDirection.Output;

                comm.ExecuteNonQuery();

                string val = (string)ReturnVal.Value;

                conn.Close();
                status_lbl.Text = val;
                status_lbl.Visible = true;
                Response.Redirect(Request.RawUrl);
            }
        }
Posted
Updated 15-Mar-14 19:46pm
v2
Comments
Aravindba 16-Mar-14 2:05am    
hi what purpose u need to show label text ? actually u assign val to label text and chnage visible as true,so where u show label ? u try to check val pass in label text or not ? u need to find what value appear in val string ??
Alvan Khong 16-Mar-14 2:18am    
The label shows the message that return from the SQL server seeing whether the store procedure execution success or not, example if my value update successfully SQL will return me "UPDATE SUCCESSFULLY" else "UPDATE FAILED" or "NO DATA EXIST". That's why the text label is very important to me.
Peter Leow 16-Mar-14 2:41am    
Use settimeout to delay the redirection and location to redirect, see solution 2.

Try this:
C#
Label1.Text = "Redirecting to Page 2";
ScriptManager.RegisterStartupScript(this, typeof(Page), "myscript", "setTimeout(function(){location.href='Default2.aspx';},5000);", true);

I have just thought that the settimeout could be put to good use here.
 
Share this answer
 
v3
Comments
Good one. 5+
Alvan Khong 16-Mar-14 5:09am    
Tried with your code, if i'm not mistaken your code is like after 5 second then redirect to a second page right? (After my text label displayed successfully) But the thing is i wanna stay in my current page, and also must with the Response.Redirect(Request.Rawurl) function so that it will clear out all the things previously edited in the page, and also for every page i redirect i've a query string in my link.
Peter Leow 16-Mar-14 6:06am    
It seems that you are looking for a Ajax solution, then suggest you look at this:
Introduction to the UpdatePanel Control
Hai

what purpose u need to show label text ? actually u assign val to label text and change visible as true,so where u show label ? u try to check val pass in label text or not ? u need to find what value appear in val string ??

Actually u assign value in label text and also set visible as true. it will also visible label,but next line u redirect to another page so u cant see label text.So try to comment redirect line then only u see label.

OK try this,first add this in script

HTML
  function testmsg(msg) {
alert(msg);
}


Add this line before redirect page

C#
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", "<script type='text/javascript'>testmsg('" + val + "');</script>", false);


u cant delay page redirect or cant show in textbox or label text,try show in alert msg.
 
Share this answer
 
v3
Comments
Alvan Khong 16-Mar-14 2:23am    
I use Response.Redirect(Request.RawUrl) to clear up my textbox field after i submitted my data, so what i need is to display my status_lbl text label before the redirect page occurs. The label shows the message that return from the SQL server seeing whether the store procedure execution success or not, example if my value update successfully SQL will return me "UPDATE SUCCESSFULLY" else "UPDATE FAILED" or "NO DATA EXIST". That's why the text label is very important to me.
Aravindba 16-Mar-14 2:29am    
see updation solution
Alvan Khong 16-Mar-14 5:01am    
ScriptManager.RegisterStartupScript(Me.Page, Me.[GetType](), "Alert", "<script type='text/javascript'>testmsg('" + val+ "');</script>", false);

the "Me.Page, Me.[GetType]()" having error "Me" does not exist in current content. what should i change it into?
Aravindba 16-Mar-14 10:33am    
try now solution updated
You can try Peter's solutions.

Another alternative could be showing a Label on the second page, which is the most desired and modern approach.
 
Share this answer
 
v2
Comments
Peter Leow 16-Mar-14 2:39am    
Thanks Tadit. I have improved the solution, now it will wait for 5 seconds before redirecting.
Good one Peter. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900