views:

58

answers:

5

I have a page that do some validations in Page_Load method

According this validation i need to block the access to this page.

Example:

protected void Page_Load(object sender, EventArgs e){
if (!IsPostBack)
{
    if (MyValidation)
    {
    // The page is loaded an de user get access
    }
    else
    {
    // Here, i need to block the access to this page
    // Redirect, close browser, etc ...
    }
} }

actually, a have this code...

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // The page is loaded an de user get access
            Services.ChatService ws = new ChatService();
            KeyValuePair<int, string> kvp = ws.AtenderClienteChat();
            if (kvp.Key > 0)
            {
                this.CodigoChamadoChat = CriaChamado(kvp.Key, kvp.Value);
                odsChamado.SelectParameters["cd_chamado"].DefaultValue = this.CodigoChamadoChat.ToString();
                fvChamadoChat.DataBind();

                // recupero da aplication a lista que contém os chat em andamento
                Application.Lock();
                List<chat_andamento> lstChatAndamento = (List<chat_andamento>)Application["ListChatAndamento"];

                // Instancio e inicializo uma nova chat_andamento
                chat_andamento ca = new chat_andamento(this.CodigoChamadoChat, kvp.Key, kvp.Value, WebUtil.GetUserId());
                lstChatAndamento.Add(ca);

                // Devolvo para a Application a lista preenchida
                Application["ListChatAndamento"] = lstChatAndamento;
                Application.UnLock();

                // Altero o titulo da pagina para facilitar localização do tecnico quando estiver com mais de um chat em andamento
                chamado c = (chamado)fvChamadoChat.DataItem;
                Page.Title = kvp.Value + " (" + c.cliente.nomecomercial_cliente + ")";


                // Envia uma mensagem de boas vindas
                Services.ChatService sw = new ChatService();
                sw.SendMessage(this.CodigoChamadoChat, "Olá " + kvp.Value + ", em que posso ajudar?", 2);

                //RetrieveMessages();  
                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "RetMessTec", "timer_onTick();", true);
            }
            else
            {
    // Here, i need to block the access to this page
    // Redirect, close browser, etc ...
                // aqui é preciso abortar o carregamento da pagina
                // talvez mostrar um DIV sobre toda a pagina inpedindo interação do usuário
                  ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "CliAtendido", "self.close();", true);
                 //this.divGeral.Visible = false;
                 //this.divErro.Visible = true;    
            }
        }
    }

and this code give me this error

Server Error in '/' Application.

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 173: function EncerrarChamado() { Line 174:
// Primeiramente mostra o combo para seleção do motivo Line 175: var divMotivo = $('#<%= fvChamadoChat.FindControl("divMotivo").ClientID %>'); Line 176: if (divMotivo.hasClass('Hidden')) { Line 177:
divMotivo.removeClass('Hidden');

Source File: c:\Projetos\Avalon\Avalon\View\Forms\frmChatTecnico.aspx Line: 175

Line 175 have some JQuery statements that retrieve a div inside a FormView

THE QUESTION 1 IS: Hoe to block access (Redirect, close browser, etc...) if the validation goes to "Else" part of my validation

THE QUESTION 2 IS: If my validation goes to "Else" the FormView is not created and if it is not created the javascript cant access it.

Any ideas ?

A: 

Have you added a ScriptManager in your page?

kgiannakakis
Yes... on MasterPage
Ewerton
A: 
  1. I would personally rediect using FormsAuthentication.RedirectToLogin() [or whatever it is called]

  2. I think if you issue a return; command after the redirect the rest wont matter as the server will send the redirect which will redirect the browser.

RemotecUk
A: 

Response.Redirect("error.aspx");

Won't that work?

Ravenheart
Probaly not.If i dont enter here if (kvp.Key > 0) a lot of things dont happens. Eg: this.CodigoChamadoChat = CriaChamado(kvp.Key, kvp.Value);odsChamado.SelectParameters["cd_chamado"].DefaultValue = this.CodigoChamadoChat.ToString();fvChamadoChat.DataBind()then the FormView (fvChamado) is not created and the Javascript will try to access evaluate this.
Ewerton
Probably not ? have u given it a try?
HotTester
A: 

When the goes to "Else" simply show the user a message for the same and a hyperlink for redirection to another page.

HotTester
Probaly not. If i dont enter here if (kvp.Key > 0) a lot of things dont happens. Eg: this.CodigoChamadoChat = CriaChamado(kvp.Key, kvp.Value); odsChamado.SelectParameters["cd_chamado"].DefaultValue = this.CodigoChamadoChat.ToString(); fvChamadoChat.DataBind() then the FormView (fvChamado) is not created and the Javascript will try to access evaluate this.
Ewerton
A: 

The problem is that i'am trying to access a control thats not rendered yet.

To solve the problem i do the following modifications in my code:

In JS a declare the variables that i need, simply like so:

    $(document).ready(function() {
    var divMotivo;
    var ddlMotivo;
    var btnFinalizaChat;
});

Inside My FormView i put some script, to initialize these variables, so, the variables are initialized only if the FormView is rendered

                    <asp:FormView ID="fvChamadoChat" runat="server" DataSourceID="odsChamado" DataKeyNames="cd_chamado"
                    Width="100%">
                    <ItemTemplate> // A lot of things ... divs, tables, etc

                        <script type="text/javascript">
                            divMotivo = $('#<%= fvChamadoChat.FindControl("divMotivo").ClientID %>');
                            ddlMotivo = $('#<%= fvChamadoChat.FindControl("ddlMotivo").ClientID %>');
                            btnFinalizaChat = $('#<%= fvChamadoChat.FindControl("btnFinalizaChat").ClientID %>');
                        </script>

                    </ItemTemplate>
                </asp:FormView>

That's it

Ewerton