Link to home
Start Free TrialLog in
Avatar of codingitupagain
codingitupagain

asked on

web usercontrol not returning variable to aspx page asp.net

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
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial