Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Custom MOSS 2007 webpart with validation

Options
  • 10-04-2008 2:10pm
    #1
    Registered Users Posts: 2,791 ✭✭✭


    Hi,

    Am developing my first webpart for sharepoint 2007 and all works fine except for the input validation which is needed for the textbox. Would appreciate some help on this, thanks!
    public class CreateMemberRegKey: WebPart
        {
            //Declare the button control here
            Button cmdGenerate;
            TextBox txtRCOP = new TextBox();
            Label txtMessage = new Label();
    
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
    
                //instantiate the button
                cmdGenerate = new Button();
                //Add begining table elements
                Controls.Add(new LiteralControl("<table width=\"100%\">"));
                Controls.Add(new LiteralControl("<tr>"));
                Controls.Add(new LiteralControl("<td style=\"text-align:center;\">"));
                
                //Add the textbox properties
                txtRCOP.ID = "RCOPID_txt";
                txtRCOP.Width = 100;
                Controls.Add(new LiteralControl("Enter RCOP ID: "));
                this.Controls.Add(txtRCOP);
                Controls.Add(new LiteralControl("</td>"));
                Controls.Add(new LiteralControl("</tr>"));
                //Next Row for Button
                Controls.Add(new LiteralControl("<tr>"));
                Controls.Add(new LiteralControl("<td style=\"text-align:center;\">"));
                
                //Add the button properties
                cmdGenerate.ID = "cmdGenerate";
                cmdGenerate.Text = "Create Registration Key";
                cmdGenerate.ToolTip = "Click here to generate a Registration Key";
                cmdGenerate.CausesValidation = true;
                //Add the event handler and then the button
                cmdGenerate.Click += new EventHandler(cmdGenerateKey_click);
                this.Controls.Add(cmdGenerate);
                Controls.Add(new LiteralControl("</td>"));
                Controls.Add(new LiteralControl("</tr>"));
                Controls.Add(new LiteralControl("<tr>"));
                Controls.Add(new LiteralControl("<td style=\"width:95%;text-align:center;\">"));
                
                //Add the message label properties
                txtMessage.ID = "txtMessage";
                txtMessage.Text = "";
                txtMessage.Font.Size = 11;
                txtMessage.Visible = true;
                txtMessage.ForeColor = System.Drawing.Color.Red;
                this.Controls.Add(txtMessage);
                
                // Required field validator
                RequiredFieldValidator rcopRequiredValidator = new RequiredFieldValidator();
                rcopRequiredValidator.ControlToValidate = txtRCOP.ClientID;
                rcopRequiredValidator.ErrorMessage = "You must provide an RCOP ID";
                rcopRequiredValidator.Display = ValidatorDisplay.Dynamic;
               
                //Close the table tags
                Controls.Add(new LiteralControl("</td>"));
                Controls.Add(new LiteralControl("</tr>"));
                Controls.Add(new LiteralControl("</table>"));
    
            }
    


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Change to the contol to validate rcopRequiredValidator.ControlToValidate = txtRCOP;


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Ginger wrote: »
    Change to the contol to validate rcopRequiredValidator.ControlToValidate = txtRCOP;

    Hi Ginger,

    Thanks for the reply. Yeah, that was one of the first things I tried but no luck :(


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Its actually the name of the ID field so RCOPID_txt will be the value of ControlToValidate

    Sorry didnt read the code the whole way through


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Haven't used Moss but this looks like a regular ASP.NET webform.

    The control to validate should simply be the id of the textbox as previously stated - no need for the client id.

    I also see you have the onClick function on the button named differently to the button. Is this function working? Is the form submitting? Is validation turned on in that page?


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Yes it is the name of the control, but in this case the OP has specified the ID value of the control as RCOPID_txt

    So there are 2 options

    1. ControlToValidate = "RCOPID_txt"
    2. ControlToValidate = txtRCOP.ID

    Should do it


  • Advertisement
  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Nice one lads, will try this out tomorrow and report back.


Advertisement