Solved

web usercontrol not returning variable to aspx page asp.net

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now