Solved

web usercontrol not returning variable to aspx page asp.net

Posted on 2014-07-25
1
593 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What!!!??? 5 58
Example code 13 36
Why am I losing my session variable value 11 33
Creating a Windows C++ program in visual studio 2015 5 22
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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