Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-10-14
4
Medium Priority
?
471 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
[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
  • 2
  • 2
4 Comments
 
LVL 34

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 34

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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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