简体   繁体   English

隐藏母版页控件

[英]Hide Master page controls

I want to hide master page table from the content page. 我想从内容页面隐藏母版页表。

I have tried to hide the table control in different ways but I get following error message on line: 我试图以不同的方式隐藏表控件,但在行上收到以下错误消息:

tbl = (HtmlTable)Page.Master.FindControl("tbl_login");

ERROR: 错误:

Object reference not set to an instance of an object. 你调用的对象是空的。

Here is the table control on the master page and the code for hidding master page control from the login page. 这是母版页上的表控件,以及用于从登录页面隐藏母版页控件的代码。

码

There is a page called POView and it has got one link button to Approve PO and it shows POP up window which has got few controls defined one master page(text box and two button in html table) when you hit the button it sends the request to another page called Login which authenticate user password and do some updates in DB and then show the message to this POP up window (response.write). 有一个称为POView的页面,它具有一个批准PO的链接按钮,它显示POP向上窗口,当您单击该按钮时,很少有控件定义一个母版页(文本框和html表中的两个按钮)。转到另一个名为Login的页面,该页面对用户密码进行身份验证并在数据库中进行一些更新,然后将消息显示到此POP up窗口(response.write)。

All i want is to hide the controls (textbox and button defined on master page) from this login page. 我只想从此登录页面隐藏控件(在母版页上定义的文本框和按钮)。

NOTE: That Login page doesn't have any reference tag for the master page. 注意:该登录页面没有母版页的任何参考标记。

@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %> @页面语言=“ C#” AutoEventWireup =“ true” CodeFile =“ login.aspx.cs” Inherits =“ login”%>

ADDED: Here is the function which is called onClick of Master page button. 添加:这是称为母版页按钮的onClick的功能。 It is sending request to login page. 正在向登录页面发送请求。

function showHint(str, str2, str3, reason)
{

gDiv = 'appFeedback';

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
  //replace "tricky" chars in QS
  var pwd=  str.replace('£','!!pound!!');
  pwd=  pwd.replace('£','!!pound!!');
  pwd=  pwd.replace('£','!!pound!!');
  pwd=  pwd.replace('£','!!pound!!');
  pwd=  pwd.replace('&','!!and!!');
  pwd=  pwd.replace('&','!!and!!');
  pwd=  pwd.replace('&','!!and!!');
  pwd=  pwd.replace('&','!!and!!');

var url="login.aspx";
url=url+"?q="+pwd;
url=url+"&q2="+str2;
url=url+"&q3="+str3;
url=url+"&reason="+reason;
url=url+"&entity=<%= Request["entity"] %>";
url=url+"&sid="+Math.random();

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

Try To call this javascript After doing the login window.opener.document.getElementById("tbl_login").style.visibility="hidden"; 尝试在登录window.opener.document.getElementById("tbl_login").style.visibility="hidden";之后调用此javascript window.opener.document.getElementById("tbl_login").style.visibility="hidden";

In you code it is given as 在您的代码中,它给出为

xmlHttp.onreadystatechange=stateChanged;

Here the "stateChanged" must be a javascript function which will be fired after doing the login. 这里的“ stateChanged”必须是一个JavaScript函数,该函数将在登录后被触发。 Inside that function you can write the code to hide. 在该函数内,您可以编写要隐藏的代码。

for more information about the readystatechangedevent please look into this :http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp 有关readystatechangedevent更多信息,请查看以下内容:http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

Return success from Loginpage 从登录页面返回成功

//After doing the Logic return the status (foreg:- i return here as "success")
Response.Clear();
Response.ContentType = "text/plain";
Response.Write("success");
Response.End();

When content page have directive 当内容页面有指令时

<%@ MasterType VirtualPath="~/Main.Master" %>

you have access to content of master page. 您有权访问母版页的内容。 Now, if you make method on master page that just hidding your table - something like this 现在,如果您在母版页上制作仅隐藏表格的方法-像这样

public void SetTableUnvisible()
{
    tbl_login.Visible = false;
}

you can call this method on Page_Load event of content page 您可以在内容页面的Page_Load事件上调用此方法

Master.SetTableUnvisible();

and your table is no more visible. 并且您的表格不再可见。

It sounds like you're trying to always remove the table, even though one or more pages don't even contain the table since they don't reference the master page anyway. 听起来您正在尝试始终删除该表,即使一个或多个页面甚至不包含该表,因为它们仍然没有引用母版页。 Is that correct? 那是对的吗?

If so, your code will be falling over because you're dereferencing Page.Master anyway, even though it won't always exist. 如果是这样,您的代码将崩溃,因为无论如何它都将取消引用Page.Master,即使它并不总是存在。 Try this, which checks to see whether there is a master page before trying to use it: 尝试以下操作,它会在尝试使用母版页之前检查它是否存在:

MasterPage master = Page.Master;
if (master != null)
{
    tbl = master.FindControl("tbl_login") as HtmlTable;
    if (tbl != null)
    {
        tbl.IsVisible = false;
    }
}

If your login page doesn't use the master page then you can't do it on server but with client side code only. 如果您的登录页面不使用母版页,那么您不能在服务器上执行此操作,而只能使用客户端代码。 Make a script method that hides the table. 创建一个隐藏表的脚本方法。 You can insert it in the master page: 您可以将其插入母版页:

<script type="text/javascript">
function hideTable()
{
   $('#<%=tbl_login.ClientID%>').hide();
}
</script>

In the login page: 在登录页面中:

ClientScript.RegisterStartupScript(this.GetType(), "hide", "hideTable();", true);

BTW, if you want to do anything in the master page, don't try to find controls on it from your page. 顺便说一句,如果您想在母版页中做任何事情,请不要尝试在您的页面上找到控件。 Write a method in the master page that do it and call it from your page. 在执行该操作的母版页中编写一个方法,然后从您的页面中调用它。 You'll need to cast Master to the master type, or put a master type in the master directive that do it for you. 您需要将Master转换为master类型,或者将master类型放入为您执行的master指令中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM