Sign In  |  Register
 
 DotNetNuke Powered!
DotNetNuke Support Forums

Help needed with module

Rate this topic:

Please Register to post a reply.
Another benefit of registration is the ability to subscribe to and recieve notifications of new posts.

AuthorMessages
jncraig
Posts:79



12/20/2007 12:20 PM  

I'm working on a simple module, but I'm having some trouble with the scope of variables, and the firing of events.

My module has a single view and nothing else.  The form is test.ascx and the codebehind page is test.ascx.cs.

My question is:  Why when clicking buttons is myPhase continually being set to zero?  What would I do to save the value of phase during the life of this page?

 

Test.ascx:

< %@ Control Language="C#" AutoEventWireup="true" CodeFile="Test.ascx.cs"
    Inherits="Test" % >
< asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" UseSubmitBehavior="False" / >
< br / >
< br / >
< br / >
< asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" UseSubmitBehavior="False" / >
< br / >
< br / >
< br / >
< asp:Label ID="Label1" runat="server" Text="Label">

test.ascx.cs:

using System;
using System.Data;

public partial class Test : DotNetNuke.Entities.Modules.PortalModuleBase
{
    public int myPhase;


    private void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myPhase = 1;
        }

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        myPhase += 1;
        UpdateLabel();

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        myPhase += 2;
        UpdateLabel();
    }

    protected void UpdateLabel()
    {
        Label1.Text = myPhase.ToString();
    }

}

jncraig
Posts:79



12/21/2007 9:48 AM  
I solved my problem by making myPhase static. Is this the best way to do it?
John Mitchell
Posts:3276



12/21/2007 11:02 AM  

You probably want MyPhase to be a private variable.  That will insure that it only gets updates from the button clicks.

If you want it to maintain state between post-backs, then you should use viewstate.  A Static variable will also work to hold the value across round-trips, but you may get unwanted results because it becomes a "Global" variable that way.

public partial class Test : DotNetNuke.Entities.Modules.PortalModuleBase
{


    private void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState("myPhase") = 1;
        }

    }


    protected void Button1_Click(object sender, EventArgs e)
    {

       ViewState("myPhase")  = Convert.ToInteger(ViewState("myPhase") )+ 1;
        UpdateLabel();

    }

jncraig
Posts:79



12/21/2007 11:47 AM  
Thanks!
Please Register to post a reply.
Another benefit of registration is the ability to subscribe to and recieve notifications of new posts.

Forums >DotNetNuke Support >General-DNN-Help > Help needed with module



ActiveForums 3.7
Visit our Store for great DotNetNuke Modules and Skins
DNNMasters Sitemap/Google Sitemap 3.0

Item codeSM3-01
Price$29.00
Product Information 
DotNetNuke CSS NavMenu 3.3 (Developers)

Item codeCSSNM33DEV
Base Price$149.00
Product Information 
Snapsis PageBlaster 3.3.2 for DotNetNuke - Professional Edition

AuthorJohn Mitchell
Base Price$79.00
Product Information 
XDAkuna (Web 2.0 CSS XHTML Skin)

Item codeXDAkuna
AuthorNina Meiers
Price$49.00
Product Information