TextBox object does not get updated while selectedIndexChanged in Dropdownlist in updatepanel

Last post 09-09-2008 9:35 AM by BizMiz. 2 replies.

Sort Posts:

  • TextBox object does not get updated while selectedIndexChanged in Dropdownlist in updatepanel

    09-08-2008, 1:26 PM
    • Member
      2 point Member
    • BizMiz
    • Member since 09-08-2008, 12:50 PM
    • Posts 3

    I saw similar posting but it did not work for me. Thank you all for postings. I am a new comer in asp.net with ajax and I am doing a similar thing except I have a textbox in SelectedIndexChanged event hadler. Here is what I am trying to do.

    I have a dropdownlist which is holding all company names. When user will select company name, the address of that company will automatically be populated in the TextBox control. It is very simple but when I am selecting a company name I am getting the address from a function but when that address is being set to TextBox control getting error like "object reference not set to an instance of an object" meaning Textbox object not instantiated.

    Please help me guys. You have lot of understanding in this field I can see. Here is my sample code: Please be sure that GetCompanyAddress(..) function is fine.

    protected override void OnInit(EventArgs e)

    {

    base.OnInit(e);

     

    if (Session["ddListCompany"] != null)

    {

    DropDownList ddListCompany = (DropDownList)Session["ddListCompany"];ddListCompany.SelectedIndexChanged += new EventHandler(ddListCompany_SelectedIndexChanged);

    UpdatePanel1.ContentTemplateContainer.Controls.Add(ddListCompany);

    }

    if (Session["tbCompanyAddress"] != null)

    {

    TextBox tbConfigSpec = (TextBox)Session["tbCompanyAddress"];

    UpdatePanel1.ContentTemplateContainer.Controls.Add(tbCompanyAddress);

    }

     

    }

    ////////////////// page load function

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!Page.IsPostBack)

    {

    //get all the sections first

    DropDownList ddListCompany = new DropDownList();

    ddListCompany.ID = "ddListCompany";ddListCompany.AutoPostBack = true;ddListCompany.SelectedIndexChanged += new EventHandler(ddListCompany_SelectedIndexChanged);

    UpdatePanel1.ContentTemplateContainer.Controls.Add(ddListCompany);

     

    ddListCompany.Items.Clear();

    ddListCompany.Items.Add("Microsoft");

    ddListCompany.Items.Add("GE");

    string companyName= ddListCompany.SelectedItem.ToString();

    TextBox tbCompanyAddress = new TextBox();

    tbCompanyAddress.ID = "tbCompanyAddress";

    tbCompanyAddress.TextMode = TextBoxMode.MultiLine;tbCompanyAddress.AutoPostBack = true;

    tbCompanyAddress.Text = GetCompanyAddress(companyName);

    UpdatePanel1.ContentTemplateContainer.Controls.Add(tbCompanyAddress);

    Session["ddListCompany"] = ddListCompany;

    Session["tbCompanyAddress"] = tbCompanyAddress;

     

    }

    }

    //////////////// Event handler ////////////

    protected void ddListCompany_SelectedIndexChanged(object sender, EventArgs e)

    {

    string companyName = ((
    DropDownList)sender).SelectedItem.Text;TextBox tbCompanyAddress = (TextBox) Session["tbCompanyAddress"];

    tbCompanyAddress.Text = GetCompanyAddress(companyName);

    //I am getting error here just line above. "Object reference not set to an instance of an object"

    }

  • Re: TextBox object does not get updated while selectedIndexChanged in Dropdownlist in updatepanel

    09-08-2008, 2:10 PM
    • Star
      8,978 point Star
    • dwhite
    • Member since 02-08-2007, 2:25 PM
    • Bristol, CT
    • Posts 1,422

    It would be a better design to use a DynamicPopulate instead of the UpdatePanel for this, or you could do it yourself with some JavaScript and a Page Method.  Just pass the company to your server method, and then populate the textbox using JavaScript. 

    -Damien

     

    Visoft, Inc - Home | Blogs

    Latest Blog Post: Silverlight DataBinding Bug
  • Re: TextBox object does not get updated while selectedIndexChanged in Dropdownlist in updatepanel

    09-09-2008, 9:35 AM
    Answer
    • Member
      2 point Member
    • BizMiz
    • Member since 09-08-2008, 12:50 PM
    • Posts 3

    Thank you dwhite for responding to my add. Actually I found the problem in GetCompanyAddress(). Within function I have a file object through which I am reading the address information for a company. That object was going out of scope(per page post). Once I saved the object in session variable, It worked fine. By working in it I got some better idea. The whole thing canbe done without creating dropdownlist and txtbox dynamicallly. I will post that solution very soon.

    Thanks and cheers 

     

Page 1 of 1 (3 items)