使用时的System.NullReferenceException(asp.net中的标签)

时间:2015-03-17 18:50:17

标签: c# asp.net .net

你好吗

我正在使用asp.net C#和

我有问题,我在一个aspx页面中有文本框和两个按钮(是,否)以及我想要的是如果用户单击是按钮,另一个aspx页面上的标签将成功写入并且当用户单击没有相同的标签时将写失败

这些是我的页面

这是3.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="3.aspx.cs" Inherits="lubna._3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />

        enter id<br />
        <asp:TextBox ID="id_no" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="yes" OnClick="Button1_Click" />

        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="no" />

        <br />

        <br />
        <br />

    </div>
    </form>
</body>
</html>

和3.aspx.cs

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace lubna
{
    public partial class _3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {


            Label label1 = (Label)this.Master.FindControl("Label1");

           label1.Visible = true;
           label1.Text = "successful";
           id_no.Text = "";

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Label label1 = (Label)this.Master.FindControl("Label1");

            label1.Visible = true;
            label1.Text = "failed";
            id_no.Text = "";
        }
    }
}

label.aspx

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>



    </div>
    </form>
</body>
</html>

和label.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lubna
{
    public partial class label : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

我希望得到答案 非常感谢你

1 个答案:

答案 0 :(得分:3)

Please refer to the following link about passing values between ASP.NET web form pages

Page无法直接相互修改。但是您可以在某些事件上重定向到另一个页面,并在URL中传递一些数据(查询字符串)。接收页面将检查URL,如果找到了它正在查找的数据,可以更新它的UI以显示数据。

3.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lubna
{
    public partial class _3: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender,EventArgs e)
        { 
              Response.Redirect("label.aspx?Event=successful");
        }
        protected void Button2_Click(object sender,EventArgs e)
        { 
              Response.Redirect("label.aspx?Event=failure");
        }
    }
}

label.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lubna
{
    public partial class label : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                 Label1.Text = Request.QueryString["Event"] == null ? "" : Request.QueryString["Event"]; 
            }
        }
    }
}