B
B
Boris the Animal2015-10-25 12:20:29
Programming
Boris the Animal, 2015-10-25 12:20:29

Cannot access asp:TextBox from code. How to get the value from there?

What am I doing wrong? mainContent is always null. You need to access the textBox, which is clear from the code below.
Master page:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="WebAppSendMessage.MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta charset="utf-8" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <div>
        <asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
</body>
</html>

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebAppSendMessage.Views.Index" %>

<asp:Content ID="header" ContentPlaceHolderID="head" runat="server">
    <style>
        input[type=text] {
            margin-top: 5px;
            margin-left: 3px;
            margin-right: 3px;
            width: 100%;
        }

        input[type=submit] {
            margin-top: 5px;
            margin-left: 3px;
            margin-right: 3px;
        }
    </style>
</asp:Content>

<asp:Content ID="mainContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <form id="mainForm" runat="server">
        <asp:TextBox ID="textBox" runat="server" />
        <br />
        <asp:Button ID="sendMessageButton" runat="server" Text="Отправить сообщение" OnClick="SendMessageButton_OnClick" />
    </form>
</asp:Content>

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebAppSendMessage.Views
{
    public partial class Index : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["mainMessage"];
            if (cookie != null)
            {
                textBox.Text = cookie["Message"];
            }
        }

        protected void SendMessageButton_OnClick(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["mainMessage"];
            if (cookie == null)
            {
                cookie = new HttpCookie("mainMessage");
                cookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(cookie);
                Response.Charset = "utf-8";
            }

            var mainContent = FindControl("MainContentPlaceHolder") as ContentPlaceHolder;
            if (mainContent != null)
            {
                var messageControl = mainContent.FindControl("textBox") as TextBox;
                if (messageControl != null)
                {
                    cookie["Message"] = messageControl.Text;
                }
            }

            Session["BackAddress"] = Request.RawUrl;

            Response.Redirect("Message.aspx");
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Lavrentiev, 2015-10-26
@YaguarVL

Tell me, what are you looking for first mainContent, in which you then try to find yours textBox?
If your task is to get the value from textBox, then isn't it easier to do so? : cookie["Message"] = textBox.Text;
If it is important to use FindControl, then it is important not to forget that this method searches for an element by ID within the current naming container. That is, the only way to find a textBox is like this:
If you want to search for an element on the page without worries, then the best option is to recursively enumerate the collection Page.Controls.
Also in the method Page_Loadit will not be superfluous to check for nullcookie["Message"]like this:

if (cookie != null && cookie["Message"] != null)
            {
                textBox.Text = cookie["Message"];
            }

Otherwise, you can expect a very exciting debugging.

B
Boris the Animal, 2015-10-26
@Casper-SC

Solved the problem like this:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebAppSendMessage.Views.Index" %>

<asp:Content ID="header" ContentPlaceHolderID="head" runat="server">
    <style>
        input[type=text] {
            margin-top: 5px;
            margin-left: 3px;
            margin-right: 3px;
            width: 100%;
        }

        input[type=submit] {
            margin-top: 5px;
            margin-left: 3px;
            margin-right: 3px;
        }
    </style>
</asp:Content>

<asp:Content ID="mainContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <form id="mainForm" method="post" runat="server">
        <asp:TextBox ID="textBox" runat="server" />
        <br />
        <asp:Button ID="sendMessageButton" runat="server" Text="Отправить сообщение"  OnClick="SendMessageButton_OnClick"/>
    </form>
</asp:Content>

using System;
using System.Web;
using System.Web.UI;

namespace WebAppSendMessage.Views
{
    public partial class Index : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["mainMessage"];
            
            if (cookie != null)
            {
                string message = cookie["Message"];
                if (message != null)
                {
                    textBox.Text = message;
                }
            }
        }

        protected void SendMessageButton_OnClick(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["mainMessage"];
            if (cookie == null)
            {
                cookie = new HttpCookie("mainMessage");
                cookie.Expires = DateTime.Now.AddYears(1);
            }

            string message = Request.Form[textBox.UniqueID];
            cookie["Message"] = message;
            Session["Message"] = message;
            Session["BackAddress"] = Request.RawUrl;

            Response.Charset = "utf-8";
            Response.Cookies.Add(cookie);

            Response.Redirect("Message.aspx");
        }
    }
}

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
    CodeBehind="Message.aspx.cs" Inherits="WebAppSendMessage.Views.Message" %>

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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <form id="mainForm" runat="server">
        <asp:Label runat="server" Text="<%# Text %>"></asp:Label>
        <br />
        <asp:Button ID="backButton" runat="server" Text="Вернуться" OnClick="GoToBackButton_OnClick" />
    </form>
</asp:Content>

using System;
using System.Web;
using System.Web.UI;

namespace WebAppSendMessage.Views
{
    public partial class Message : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }

        protected string Text
        {
            get { return (string)Session["Message"]; }
        }

        protected void GoToBackButton_OnClick(object sender, EventArgs e)
        {
            string returnUrl = Session["BackAddress"] as string ?? "Index.aspx";
            Response.Redirect(returnUrl);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question