Solved

ASP.NET Public Property - Can't get value from User Control

Posted on 2014-10-14
4
459 Views
Last Modified: 2014-10-19
I am working with a user control that is used as a selection criteria.  I need to have a checkbox depending on if a certain criteria is selected.  I have this working.  On the main form, I need to validate so I thought I would use public properties.  When checking the properties they values are false even though I set them to true.

//USER CONTROL CODE
 private short GetSelection()
        {
            short iSelection = 0;
            int iPlanc = 0;

            if (ViewState["Planc"] != null)
            {
                iPlanc = Convert.ToInt32(ViewState["Planc"]);
            }

            RowSelectorColumn myColumn = RowSelectorColumn.FindColumn(dgPlan);
            if (myColumn.SelectedIndexes.Length > 0) 
                iSelection = Convert.ToInt16(dgBenefit.DataKeys[myColumn.SelectedIndexes[0]]);

            if (iSelection == iPlanc)
            {
                this.pnlValidate.Visible = true;
                bValidate = true;  //Have been declare private above
                bValidChecked = false;  //Have been declare private above
            }
            else
            {
                this.pnlValidate.Visible = false;
                bValidate = false;  //Have been declare private above
                bValidChecked = false;  //Have been declare private above
            }

            return iSelection;
        }

        protected void chkValid_Checked(object sender, EventArgs e)
        {
            if (this.chkValid.Checked)
                bValidChecked = true;
            else
                bValidChecked = false;
        }

        public bool ValidChecked
        {
            get
            {
                return bValidChecked;
            }
        }

        public bool Validate
        {
            get
            {
                return bValidate;
            }
        }

Open in new window


//CODE IN MAIN FORM THAT INHERITS THE USER CONTROL
    protected void lnkNext_Click(object sender, System.EventArgs e)
    {
        try
        {
            lblMessage.Text = string.Empty;
            
             //User Control - Always returns False
            if (SelectionGrid1.Validate == true)
            {
                if (SelectionGrid1.ValidChecked == false)
                {
                    lblMessage.Text = "You must blah blah blah to continue.";
                    return;
                }
            }
        }
        catch (ApplicationException ex)
        {
            lblMessage.Text = ex.Message;
            return;
        }
    }

Open in new window

0
Comment
Question by:CipherIS
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40380876
It is just as easy to just have your ValidChecked property return the state of the checkbox:
public bool ValidChecked
{
	get { return this.chkValid.Checked };
}

Open in new window


-saige-
0
 
LVL 1

Accepted Solution

by:
CipherIS earned 0 total points
ID: 40380895
The problem is that the checkbox MUST be checked only if ONE out of "n" is checked.  That value of one is stored in the web.config file.  When it matches then I need to check the value of the check box.

if (iSelection == iPlanc) is comparing the Selection to the value stored in the web.config (iPlanc - which I stored in ViewState)

I solved it by storing the variables in Viewstate this way it retains its value.  

The properties work as designed now.
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40380911
Gotcha.  Glad you got it resolved.

-saige-
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 40389818
Stored values in Viewstate and it resolved the issue.
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

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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