查找使用母版页的页面控件时出现问题
我有一组页面,每个页面用于提示用户输入数据并在数据库中为此数据创建一条记录。
我对所有页面使用相同的技术.. 2 个面板,第一个面板包含所有输入控件,默认情况下可见,第二个面板显示成功消息,默认情况下隐藏。
现在,当用户单击“提交”按钮时..如果数据有效,我会编写几行来隐藏第一个面板并显示第一个面板..我有大约 20 个页面使用相同的技术,因此作为一个好的实践 我在我的 Web 项目中添加了一个名为 Helper 的类,它搜索 2 个面板,然后隐藏并显示我需要的内容。
我对所有页面使用了相同的面板 ID,这样我就可以轻松使用它!
注意:我首先直接直接搜索面板,但它不起作用,所以我尝试搜索包含面板和内容的内容控件(因为此页面使用母版页),然后找到面板。两次尝试都不会成功!
public class Helpers
{
public static void SuccessfulSubmission(Page p)
{
Content content = p.FindControl("Content2") as Content;
Panel inputPanel = content.FindControl("inputPanel") as Panel;
inputPanel.Visible = false;
}
}
I have a set of pages, each page is used to prompt the user for data and create a record for this data in the database.
I use the same technique for all the pages .. 2 panels, the first has all the input controls and is visible by default and a second panel that shows a success message and is hidden by default.
Now when the user clicks the "Submit" button .. if the data is valid, I write a couple of lines to hide the first panel and show the first one .. I have like 20 pages that uses the same technique so as a good practice I added a class to my web project that's called Helper that searches for the 2 panels, then hide and show what I need.
I used the same panel Id for all the pages so I could use it easily!
N.B: I first just went ahead and searched for the panel directly but it won't work so I tried to search for the content control that holds the panels and stuff (as this page is using a master page) and then find the Panel. both attempts won't work out!
public class Helpers
{
public static void SuccessfulSubmission(Page p)
{
Content content = p.FindControl("Content2") as Content;
Panel inputPanel = content.FindControl("inputPanel") as Panel;
inputPanel.Visible = false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
搜索内容控件永远不会起作用,因为每当使用母版页时,其控件都会插入到页面中,并且页面的控件(在内容控件内)会添加到相应的内容占位符中。也许,您应该首先尝试找到内容占位符,然后尝试查找控件。
然而,更好的设计是创建一个基页面类以及特定的母版页来控制这种交互。例如,
您将有一个母版页(假设名为 TwoPanelMaster),它将使用您的网站母版页并使用两个面板布局以及提交按钮。想法是您拥有通过母版页控制的标准布局。
在后面的主代码中公开方法来访问提交按钮、显示/隐藏面板等。
现在创建一个基页面类来编码基本交互 -
这是一种通用方法,可以为您提供更多控制并消除依赖关系,例如您的页面必须包含具有特定 ID 等的面板。
Searching content control would never work because whenever master page is used, its controls get inserted into the page and page's controls (within content control) gets added to the corresponding content placeholders. Perhaps, you should try to locate the content placeholders first and then try finding the control.
However, a better design would be creating a base page class along with specific master page to control this interaction. For example,
You will have a master page (say named as TwoPanelMaster)that would use your site master page and use two panel layout along with submit button. Idea is you have standard layout that is controlled via master page.
Expose methods in the master code behind to get access to submit button, to show/hide panels etc.
Now create a base page class that would code the basic interaction -
This is a general approach that would give you a more control and remove dependency such that your pages must have panels having particular ID etc.