Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ViewState Issue for User Control

Posted on 2009-07-10
7
Medium Priority
?
242 Views
Last Modified: 2013-12-17
Hi.  I currently have a user control that is sitting on the UI/Presentation Layer like so:

1. Default.aspx has the User Control in the design area.
2. The User Control is setting an event for a delegate.
3. The page reloads when the "Update" button is pressed within the User Control.
4. (After pressing Update from the User Control), On the Page_Load in Default.aspx.cs, if I look at the event value for the user control (CtnlEditTask.CurrentTaskClick), it shows up null.  That event should be set up (see step 2).

How can I get the page_load to properly remember what the event's prior setting is?
Default.aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
    {
        CtnlEditTask.EnableViewState = true;
        if (!IsPostBack)
        {
            this.ReloadTasks(null, EventArgs.Empty);
        }
        else
        {
            CtnlEditTask.CurrentTaskClick += new TaskUpdateHandler(ReloadTasks);
        }
    }
 
Control_EditTask.ascx.cs:
public delegate void TaskUpdateHandler(object sender, EventArgs e);
 
public partial class Control_EditTask : System.Web.UI.UserControl
{
    private int _taskID = 0;
    private TaskUsers _taskUsers = new TaskUsers();
    private Task _task = null;
    private TaskTopics _topics = null;
 
    public event TaskUpdateHandler CurrentTaskClick;
 
    protected void Page_Load(object sender, EventArgs e)
    {
            CurrentTaskClick += new TaskUpdateHandler(this.cmdUpdate_Click);
        
    }
...
    protected void cmdUpdate_Click(object sender, EventArgs e)
    {...}
 
}

Open in new window

0
Comment
Question by:Unionblitz
[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
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24830111
Hello ,

Please explain what you want to achive ?
0
 

Author Comment

by:Unionblitz
ID: 24830173
I want to have a aspx page call a method within that aspx page when an event (like a button being pressed) is fired in a user control.

I have a default.aspx page that has a user control.  The user control has some text fields and a button, " btnUpdate".  When the btnUpdate is pressed, I want that user control to update itself, and then I want default.aspx to call a method from within default.aspx (so default.aspx gets updated as well).

Ideas?
0
 
LVL 15

Expert Comment

by:jinal
ID: 24830879
I am not understanding that you want to transfer call event from UserControl to page.

1. ASPX
           -  Has User Control.
                     - On Button Update click in UserControl you want to indicate this update in DEfault.aspx too.

Am i right ?
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:Unionblitz
ID: 24831146
That's correct- On Button Update click in UserControl should trigger an event in Default.aspx, which will be mapped to a method in Default.aspx...

A user control contains content that needs to refresh other elements on the Default.aspx page.

Ideas?
0
 
LVL 15

Expert Comment

by:jinal
ID: 24831179
Hello ,

Please try attached sample.



/* WebUserControl.ascx */
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
 
/* WebUserControl.ascx.cs */
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class WebUserControl : System.Web.UI.UserControl
{
    public delegate void TaskUpdateHandler(object sender, EventArgs e);
    public event TaskUpdateHandler CurrentTaskClick;
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // Do stuff for button and than fire event.
        Response.Write("This is control event");
        if (CurrentTaskClick != null)
        {
            CurrentTaskClick(sender, e);
        }
    }
}
 
 
/* ASPX PAge */
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
    
    </div>
    </form>
</body>
</html>
 
/* default.aspx.cs */
 
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebUserControl1.CurrentTaskClick += new WebUserControl.TaskUpdateHandler(WebUserControl1_CurrentTaskClick);
        if (!Page.IsPostBack)
        {
            this.WebUserControl1_CurrentTaskClick(this.WebUserControl1, EventArgs.Empty);
        }
    }
 
    void WebUserControl1_CurrentTaskClick(object sender, EventArgs e)
    {
        Response.Write("This is in page");
    }
}

Open in new window

0
 
LVL 15

Accepted Solution

by:
jinal earned 1000 total points
ID: 24831186
Try with previous sample .

And i think you need to change one thing in your code and try .

You default.aspx.cs should look like this. As  you are puuting CtnlEditTask.CurrentTaskClick += new TaskUpdateHandler(ReloadTasks); in else it create problem. b'coz viewstate persistes control state not events.

Default.aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ReloadTasks(null, EventArgs.Empty);
        }
        CtnlEditTask.CurrentTaskClick += new TaskUpdateHandler(ReloadTasks);     
    }
 

Open in new window

0
 

Author Closing Comment

by:Unionblitz
ID: 31602368
Jinal, thank you for your help!  It took a little tweeking on my end to get the delegate to work, but the code you provided was exactly what I was looking for.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

618 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