在Page_Load方法中阻止对页面的访问

时间:2010-02-08 11:05:07

标签: c# .net javascript client-side server-side

我有一个页面在Page_Load方法

中做了一些验证

根据此验证,我需要阻止访问此页面。

示例:

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 ...
    }
} }

实际上,有这个代码......

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;    
            }
        }
    }

这段代码给我这个错误

  '/'应用程序中的服务器错误。      

对象引用未设置为   对象的实例。描述:   在此期间发生了未处理的异常   当前网络的执行   请求。请查看堆栈跟踪   有关错误的更多信息   它起源于代码。

     

异常详细信息:   System.NullReferenceException:Object   引用未设置为的实例   对象

     

来源错误:

     

第173行:功能   EncerrarChamado(){Line 174:
  // Primeiramente mostra o combo para   seleçãodomotivo第175行:var   divMotivo = $('#&lt;%=   fvChamadoChat.FindControl( “divMotivo”)。ClientID的   %GT;');第176行:如果   (divMotivo.hasClass('隐藏')){Line   177:
  divMotivo.removeClass( '隐藏');

     

源文件:   C:\ Projetos \阿瓦隆\阿瓦隆\查看\表格\ frmChatTecnico.aspx   行:175

     

第175行有一些JQuery语句   检索FormView中的div

问题1:如果验证进入验证的“Else”部分,则阻止访问(重定向,关闭浏览器等等)

问题2是:如果我的验证转到“Else”,则不会创建FormView,如果没有创建,则javascript无法访问它。

有什么想法吗?

5 个答案:

答案 0 :(得分:0)

您是否在页面中添加了ScriptManager?

答案 1 :(得分:0)

  1. 我个人会使用FormsAuthentication.RedirectToLogin()[或其他任何名称]进行重做

  2. 我想如果你发出回报;重定向后的命令,其余的不重要,因为服务器将发送重定向,这将重定向浏览器。

答案 2 :(得分:0)

的Response.Redirect( “error.aspx”);

那不行吗?

答案 3 :(得分:0)

当转到“Else”时,只需向用户显示相同的消息和用于重定向到另一个页面的超链接。

答案 4 :(得分:0)

问题在于我正在尝试访问尚未呈现的控件。

要解决此问题,我在代码中进行了以下修改:

在JS中声明我需要的变量,就像这样:

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

在我的FormView中我添加了一些脚本来初始化这些变量,因此,只有在呈现FormView时才初始化变量

                    <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>

就是这样

相关问题