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"
}