I am using an asp.net formsview in my asp.net page and updatepanel . It has two templates Insertemplate and EditTemplate. Indie both templates there is a dropdownlist with id ddlCountry. I have a dropdownlist with all countries. I am showing states dropdown, if country is US and want to hide the row where states dropdown is shown If country is Non US. I am using following code but it is not working:
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
Control c = (Control)sender;
Control nc = c.NamingContainer;
if (nc.ID == "fvBillTo" && rblShipSelect.SelectedValue == "billing")
{
setShippingAndTaxesDisplay();
DropDownList ddlCountry = c as DropDownList;
if (ddlCountry.SelectedItem != null && ddlCountry.SelectedItem.Value == "001")
{
HtmlGenericControl trState = nc.FindControl("trState") as HtmlGenericControl;
trState.Visible = true;
}
else
{
HtmlGenericControl trState = nc.FindControl("trState") as HtmlGenericControl;
trState.Visible = false; // code stops here
}
}
}
The code is throwing an exception at that point because it doesn’t have a reference to the table row.
Make sure you have the row referenced as a server side control
And cast it as a table row instead of a HtmlGenericControl