Solved

web usercontrol not returning variable to aspx page asp.net

Posted on 2014-07-25
1
601 Views
Last Modified: 2014-07-28
Hi Experts,

I'm currently writing an asp.net website and looking at user controls. I have managed to input variables into the usercontrol and display them but when I click the button on the usercontrol to return to the aspx page it does not feedback the output variable that i'm setting "MyResult" within the control. What am I doing wrong please. The code is: -

DynamicUsercontrol.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Testing
{
    public partial class DynamicUsercontrol : System.Web.UI.Page
    {
        QuestionType1 ucSimpleControl = new QuestionType1();

        protected void Page_Load(object sender, EventArgs e)
        {
            //ucSimpleControl.MyResult = "test";
            lblReturn.Text = ucSimpleControl.MyResult;
        }


        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            ucSimpleControl = LoadControl("~/QuestionType1.ascx") as QuestionType1;
            ucSimpleControl.QuestionText = "This is my Question";
            PlaceHolder1.Controls.Add(ucSimpleControl);
        }
    }
}

Open in new window


DynamicUserControl.aspx : -
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicUsercontrol.aspx.cs" Inherits="Testing.DynamicUsercontrol" %>
<%@ Register TagPrefix="uc" TagName="QuestionT1" Src="QuestionType1.ascx" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="cmdAdd" runat="server" Text="Add user control" OnClick="cmdAdd_Click" />
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <br />
        My return:
        <asp:Label ID="lblReturn" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Open in new window


QuestionType1.ascx.cs : -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Testing
{
    public partial class QuestionType1 : System.Web.UI.UserControl
    {

        string PassResult;

        public string MyResult
        {
            get { return PassResult; }
            //set { PassResult = value; }
        }

        public string QuestionText { get; set; }
       // public String MyResult { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            lblText.Text = QuestionText.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            PassResult = "Ive passed";
            
        }
    }
}

Open in new window


QuestionType1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="QuestionType1.ascx.cs" Inherits="Testing.QuestionType1" %>
<p>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Pass" />
    Question type 1
    <asp:Label ID="lblText" runat="server" Text="Label"></asp:Label>
</p>

Open in new window


Many thanks
Lee
0
Comment
Question by:codingitupagain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 40220060
You are adding your Usercontrol dynamically in to the page. You need to take care of re-adding it on every postback: Check this tutorial.

And you are assigning the lblText in the load of the Page but setting property value in the buttonclick of the UC. Click is called later in the cycle and your label text won't be sent to new value.
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question