Skip to content

Array out of bounds exception in custom web server control.

Kalaivani Kalai

Asked Jan 28, 2018, 6:23 AM EST1 answer
Share
LinkedInX
PowerShell
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SPLITTEXTCONTROL
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:splittext runat=server></{0}:splittext>")]
public class splittext : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
C#
        private TextBox _firstnametext = new TextBox();
        private TextBox _lastnametext = new TextBox();

        public string Text
        {
            get
            {
                String Text = _firstnametext.Text + _lastnametext.Text;
                return ((Text == null) ? "[" + this.ID + "]" : Text);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);
            _firstnametext.MaxLength = 10;
            _lastnametext.MaxLength = 10;
            _firstnametext.Width = new Unit("40px");
            _lastnametext.Width = new Unit("40px");
        }



        protected override void RenderContents(HtmlTextWriter output)
        {
            String str = this.Text;
            char[] delimiters = new char[] { ' ', ',' };
            string[] parts = str.Split(delimiters);
            _firstnametext.Text = parts[0];
            _lastnametext.Text  = parts[1];
            _firstnametext.RenderControl(output);
            _lastnametext.RenderControl(output);

        }

    }
}
 
what is wrong with my code when i try to create a web server control that parses the seperator for firstname and lastname.please give me a solution to solve this

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.