-3

how are you

I am using asp.net C# and

I have problem that I have textbox and two buttons(yes,no) in one aspx page and what I want if the user click on yes button the label on another aspx page will write successful and when user click no the same label will write failed

and these are my pages

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

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

and 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)
        {

        }
    }
}

I hope to get an answer thank you so much

programmer
  • 7
  • 1
  • 1
  • 6
  • 1
    Please debug your application. You will find the issue yourself. – Quality Catalyst Mar 17 '15 at 18:53
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BJ Myers Mar 17 '15 at 18:54
  • 1
    The code makes no sense to me, where is the code for the button clicks ? Where is Button1_Click and Button2_Click – Philip Stuyck Mar 17 '15 at 18:55
  • 1
    [This](http://i.lvme.me/v8ccqht.jpg) – Banana Mar 17 '15 at 18:56
  • Your pages can't update each other directly, not even if they share a master page. That's not how ASP.NET works. – mason Mar 17 '15 at 18:58
  • @mason can you please explain in details because I am not professional in asp.net ,,,, thank you – programmer Mar 17 '15 at 19:03
  • @lubna asp.net is a request-response based which means that you send a request to the server, and get the page as a response. when you open the "label" page in your browser, that page is already in your computer and the server has no access to it unless you post it back from the "label" page itself. your question gives the impression that you are trying to manipulate elements on the "label" page from the "3" page, which is not possible. you need to send a request to the server to alter the value on the "label" page, and then you need to request the "label" page again. – Banana Mar 17 '15 at 19:08
  • @lubna Pages can't update each other. That's not how HTTP works. HTTP is based on requests and responses from the server, the server can't push updates to a webpage via the HTTP protocol. You need to do some research and figure the basics out before you start making sites. – mason Mar 17 '15 at 19:09
  • @lubna you could however, make it look like the pages update eachother but the implementation will depend on the reason why you need them to do it in the first place. – Banana Mar 17 '15 at 19:17

1 Answers1

3

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

Page's cant directly modify each other. But you can redirect to another page upon some event, and pass some data in the URL (query string). The receiving page will check the URL, and if it finds the data it's looking for, can update it's UI to show the data.

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"]; 
            }
        }
    }
}
mason
  • 31,774
  • 10
  • 77
  • 121
nobody
  • 10,892
  • 8
  • 45
  • 63