当前位置:编程学习 > C#/ASP.NET >>

.net母版页有内容页这个属性吗?请问怎么表示?

if (((TextBox)this.Page.PreviousPage.conFindControl("txt_username")).Text != string.Empty && ((TextBox)this.Page.PreviousPage.FindControl("txt_password")).Text != string.Empty)
有这样一个判断语句,当值从普通页面传入这个页面时正常执行,
但把传值页面放进母版页传值时,此接受页面的这段话则报错,
(此接受页面也在同样的母版页中)请问怎么改。。。。 --------------------编程问答-------------------- ddddd。。...........各位大虾请回答啊。。。谢了。。 --------------------编程问答-------------------- 报的什么错 --------------------编程问答-------------------- --------------------编程问答--------------------
引用楼主 o0tt0o 的回复:
if (((TextBox)this.Page.PreviousPage.conFindControl("txt_username")).Text != string.Empty && ((TextBox)this.Page.PreviousPage.FindControl("txt_password")).Text != string.Empty)
有这样一个判断语句,当值从普……

加了个模板页  找的路径当然也就不一样了   --------------------编程问答-------------------- 放入母版页  Contorl 的id 会变化 
你看看页面源文件里对应的id 就知道了  --------------------编程问答-------------------- 那个。。所以请教应该怎么找……
引用 4 楼 yanbuodiao 的回复:
引用楼主 o0tt0o 的回复:
if (((TextBox)this.Page.PreviousPage.conFindControl("txt_username")).Text != string.Empty && ((TextBox)this.Page.PreviousPage.FindControl("txt_password")).Text != str……
--------------------编程问答-------------------- 用户代码未处理NullReferenceExpection 未将对象引用设置到对象实例
引用 2 楼 chuanzhang5687 的回复:
报的什么错
--------------------编程问答-------------------- .conFindControl  这个是失误。。就是FindControl………………
引用楼主 o0tt0o 的回复:
if (((TextBox)this.Page.PreviousPage.conFindControl("txt_username")).Text != string.Empty && ((TextBox)this.Page.PreviousPage.FindControl("txt_password")).Text != string.Empty)
有这样一个判断语句,当值从普……
--------------------编程问答-------------------- --------------------编程问答-------------------- 不清楚哦  --------------------编程问答-------------------- 1.先找到链接的页的母版页this.PreviousPage.Master
2.从母版页中findControl的ContentPlaceHolder对象
3.从这个ContentPlaceHolder对象中找到相应的控件对象
TextBox tb = (TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1");

Respone.ReDirect(url)后,从url页面中不会找previouspage
只能使用Server.transfer(url),与button ,imageButton的postbackurl,才能使用this.previouspage

--------------------编程问答-------------------- 很有道理。但还是调不通。。用户代码未处理NullReferenceExpection 未将对象引用设置到对象实例 我把 全部代码发上来吧。
引用 11 楼 yanbuodiao 的回复:
1.先找到链接的页的母版页this.PreviousPage.Master
2.从母版页中findControl的ContentPlaceHolder对象
3.从这个ContentPlaceHolder对象中找到相应的控件对象
TextBox tb = (TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindCo……
--------------------编程问答--------------------
接受页后台
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class chap_3_2 : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.PreviousPage != null)
        {
            if (PreviousPage.IsCrossPagePostBack == true)
            {
              if (((TextBox)this.PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("txt_username")).Text != string.Empty &&(((TextBox) this.PreviousPage.Master.FindControl("ContentPlaceHolder").FindControl("txt_password")).Text != string.Empty))
                //if (((TextBox)this.PreviousPage.Master.FindControl("testusername")).Text != string.Empty && ((TextBox)this.Master.Page.PreviousPage.FindControl("testpassword")).Text != string.Empty)

                {
                    this.Label3.Text = this.Label3.Text + "<br>用户名:" + ((TextBox)Page.PreviousPage.FindControl("txt_username")).Text
                        + "<br>密码:" + ((TextBox)Page.PreviousPage.FindControl("txt_password")).Text
                        + "<br>问题:" + ((DropDownList)Page.PreviousPage.FindControl("DropDownList1")).Text
                        + "<br>答案:" + ((TextBox)Page.PreviousPage.FindControl("txt_answer")).Text;
                    this.Label1.Text = "</br>来自" + Request.ServerVariables["remote_addr"].ToString() + "的朋友,你好";
                    this.Label2.Text = "</br>你当前运行的文件是:" + Request.ServerVariables["script_name"].ToString();
                    this.Label3.Text = "<p>以下是你提交的信息,请确认<br />" + this.Label3.Text;
                }
                else
                {
                   Label4.Text="必须输入用户名密码,请返回";
                }
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Write("<script>alert(\"确认成功。。请返回\")</script>");
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("chap_3-1.aspx");
        Response.Write("<script>alert(\"取消成功。。\")</script>");
    }
}

接受页前台
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="chap_3-2.aspx.cs"  MasterPageFile="~/news.master"  Inherits="chap_3_2" %>

<asp:Content ID="Context1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <body>
    <div>
        
        <table>
            <tr>
                <td colspan="2">
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="Label2" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="Label3" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="确认" />
                </td>
                <td>
                    <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="返回" />
                </td>
            </tr>
        </table>
        <br />
        <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
</body>
</asp:Content>

发送页前台
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/news.master"  CodeFile="chap_3-test.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Context1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <title>出错跨页传递</title>

    <div>
   
        <table>
            <tr>
                <td class="style2">
                    用户名  
                </td>
                <td class="style3">
                    <asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    密码</td>
                <td style="text-align: left">
                    <asp:TextBox ID="txt_password" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    提问</td>
                <td style="text-align: left">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>你妈妈叫什么</asp:ListItem>
                        <asp:ListItem>你爸爸叫什么</asp:ListItem>
                        <asp:ListItem>你叫什么</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    回答</td>
                <td style="text-align: left">
                    <asp:TextBox ID="txt_answer" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="text-align: right">
                          </td>
                <td style="text-align: left">
                    <asp:Button ID="Button1" runat="server" Text="当前页面确认" />
                    <asp:Button ID="Button3" runat="server"  
                        PostBackUrl="~/chap 3/chap_3-2.aspx"  Text="跨页面提交"/>
                    <asp:Button ID="Button2" runat="server" Text="取消"  />
                </td>
            </tr>
        </table>
        
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label><br />
        <asp:Label ID="Label2" runat="server"></asp:Label><br />
        <asp:Label ID="Label3" runat="server"></asp:Label><br />
        
    </div>
    </asp:Content>

<asp:Content ID="Content1" runat="server" contentplaceholderid="head">

    <style type="text/css">
        .style3
        {
            text-align: left;
        }
    </style>

</asp:Content>

发送页后台
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ContentPlaceHolder testCPH;
        TextBox testusername;
        TextBox testpassword;
        testCPH = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        testusername = (TextBox)testCPH.FindControl("txt_username");
        testpassword = (TextBox)testCPH.FindControl("txt_password");
        Button bt_input = (Button)testCPH.FindControl("Button3");

    }


    
}


引用楼主 o0tt0o 的回复:
if (((TextBox)this.Page.PreviousPage.conFindControl("txt_username")).Text != string.Empty && ((TextBox)this.Page.PreviousPage.FindControl("txt_password")).Text != string.Empty)
有这样一个判断语句,当值从普……
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,