?
Solved

Dynamic controls

Posted on 2005-03-18
9
Medium Priority
?
336 Views
Last Modified: 2012-08-14
I am developing a dynamic web control.  Right now, I'm just trying to get a (I thought) simple control to request a password, fire an event if it's correct, and write a failure message if it's wrong.  It works fine if I enter the right password, but if I enter the wrong password, my failure message never shows up.  

Basically, I set the viewstate to indicate whether or not to show the failure message, create all controls in an overrided CreateChildControls, and call EnsureChildControls whenever I want them to show up.  What am I doing wrong?

Here is the code:

using System;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      
      
      /// <summary>
      /// Handles admin password request and authentication.
      /// </summary>
      public class AdminPasswordControl : WebControl {
            readonly string pwdTB_ID = "AdminPwdTB";
            readonly string pwdButton_ID = "AdminPwd";
            readonly string thisState = "AdminPasswordRequestControlState";
            enum States {
                  Clear, Failed
            }
            
            public event EventHandler PasswordAccepted;
            
            public AdminPasswordControl() {
                  this.EnableViewState = true;
                  ViewState.Add(thisState, States.Clear);
                  
                  Config.Instance.Sha1jsUrl = "sha1.js";
                  
                  System.Security.Cryptography.SHA1 sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
                  byte[] hash = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes("pwd"));
                  Config.Instance.AdminPassword = Convert.ToBase64String(hash).Replace("=", String.Empty);
                  
                  EnsureChildControls();
            }
            
            public void OnSubmit(object sender, EventArgs e) {
                  Button btn = (Button)sender;
                  
                  string sha1EnteredPwd = ((TextBox)this.FindControl(pwdTB_ID)).Text;
                  
                  if ( Config.Instance.AdminPassword == sha1EnteredPwd ) {
                        if ( PasswordAccepted != null ) {
                              PasswordAccepted(this, new EventArgs());
                        }
                  }
                  else {
                        ViewState[thisState] = States.Failed;

                        EnsureChildControls();
                  }
            }
            
            protected override void CreateChildControls() {
                  this.Controls.Clear();
                  
                  this.Controls.Add(new JavascriptIncludeControl(Config.Instance.Sha1jsUrl));
                  
                  this.Controls.Add(ControlProvider.GetRegularLabel("Enter administrator password: "));
                  
                  TextBox pwdTb = new TextBox();
                  pwdTb.ID = pwdTB_ID;
                  pwdTb.TextMode = TextBoxMode.Password;
                  pwdTb.Width = ControlProvider.InputControlSize;
                  
                  this.Controls.Add(pwdTb);
                  this.Controls.Add(new LineBreakControl());
                  
                  Button submitBtn = ControlProvider.GetUpdateButton(pwdButton_ID,
                                                                     new EventHandler(OnSubmit));
                  submitBtn.Text = "Submit";
                  // Hash the password before sending
                  submitBtn.Attributes.Add("onClick", pwdTb.ClientID + ".value = " +
                                           "b64_sha1(" + pwdTb.ClientID + ".value);");
                  
                  this.Controls.Add(submitBtn);

                  if ( (States)ViewState[thisState] == States.Failed ) {
                        Label failLabel = ControlProvider.GetRegularLabel("Invalid password!");
                        failLabel.ForeColor = System.Drawing.Color.Red;
      
                        this.Controls.Add(failLabel);
                  }
            }
      }
0
Comment
Question by:thedude112286
[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
  • 2
9 Comments
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13579472
maybe OnSubmit execute after CreateChildControls, not before.
Have you checked that?
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 13580262
what is the value of ViewState[thisState], looks like it doesn't get equal

or what's ControlProvider.GetRegularLabel, what if you try like this..?

 if ((States)ViewState[thisState] == States.Failed) {
                    Label failLabel = new Label();
                    failLabel.Text="Invalid Password!";
                    failLabel.ForeColor = System.Drawing.Color.Red;
     
                    this.Controls.Add(failLabel);
 }
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 13580267
where is this come from...? I mean value of thisState variable, have you checked if it's right...?

ViewState[thisState]
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:thedude112286
ID: 13582250
ControlProvider.GetRegularLabel just returns a label whose appearance (width, height, etc) is already set.  This cut down on a lot of redundant code.  I'm pretty sure that the ViewState does get set to States.Failed, but I'll double check this tonight.  Is there anything wrong with the pattern used?  That is, when I want to update the interface, should I just set the viewstate and then call EnsureChildControls or is there a better way to do this?
0
 
LVL 4

Author Comment

by:thedude112286
ID: 13583285
laotzi, I call EnsureChildControls from OnSubmit so I just assumed that CreateChildControls would be called then.  If it's being called before, is there any way that I can have it called after OnSubmit?  Should I just add on the Login Failed label in the OnSubmit routine?
0
 
LVL 4

Author Comment

by:thedude112286
ID: 13583287
laotzi, I have also tried calling Controls.Clear before EnsureChildControls from OnSubmit and when I do that, I end up with a blank page.  This makes me think you are right about the order of execution.  How can I fix this???
0
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 2000 total points
ID: 13583297
Yeah, I think you can add the label in OnSubmit instead of CreateChildControls
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 13583374
also remember that EnsureChildControls should be called after all controls are initialized on the form
0
 
LVL 4

Author Comment

by:thedude112286
ID: 13585919
Thanks laotzi.  I'll try that out.

davidlars99: If I initialize all of the controls before I call EnsureChildControls, what should CreatChildControls do?  Is it even needed?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 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