Solved

web usercontrol not returning variable to aspx page asp.net

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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