0
I have a gridview that lists the countries and depending on the selected list the cities of that country.
<asp:GridView ID="gvRentalVendor" runat="server" AutoGenerateColumns="false" DataKeyNames="RetalAgency_Id" Width="824px" OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
<Columns> <asp:TemplateField HeaderText="País" ItemStyle-Width="100">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country_Id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlCountry1" Height="39px" Width="190px" AutoPostBack="true"
OnSelectedIndexChanged="ListarCidade1" OnTextChanged="ListarCidade1" OnDataBound="ListarCidade1">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cidade" ItemStyle-Width="100">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("city") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlCity1" Height="39px" Width="190px" AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
But I’m not able to do this, because when I use the Onrowdatabound event, I already get the default value, no action with any other changes that the user makes in the dropdownlist, IE, Selectedvalue is always the same.
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && gvRentalVendor.EditIndex == e.Row.RowIndex)
{
DropDownList listVendor = (DropDownList)e.Row.FindControl("ddlVendor1");
DropDownList listCountry = (DropDownList)e.Row.FindControl("ddlCountry1");
DropDownList listCity = (DropDownList)e.Row.FindControl("ddlCity1");
wsCarSuite csuite = new wsCarSuite();
char[] language = Session["idioma"].ToString().ToCharArray();
// Listar vendor
DataSet dsVendor = LoadVendor(Convert.ToInt32(ddlContratos.SelectedValue));
listVendor.DataSource = dsVendor.Tables[0];
listVendor.DataValueField = "Vendor_Id";
listVendor.DataTextField = "Name";
listVendor.DataBind();
// Listar Países
DataSet dsCountry = ListCountryByLanguageAndVendor(Convert.ToInt32(listVendor.SelectedValue));
listCountry.DataSource = dsCountry.Tables[0];
listCountry.DataValueField = "CountryID";
listCountry.DataTextField = "Nome";
listCountry.DataBind();
DataSet dsCity = listCityByCountry(listCountry.SelectedValue);
listCity.DataSource = dsCity.Tables[0];
listCity.DataValueField = "CityId";
listCity.DataTextField = "city";
listCity.DataBind();
csuite.Dispose();
}
}
I tried to create some events to update the cities, such as: OnSelectedIndexChanged="ListarCidade1" OnTextChanged="ListarCidade1" OnDataBound="ListarCidade1"
Using this code here:
protected void ListarCidade1()
{
ddlCity.DataSource = listCityByCountry(ddlCountry1.SelectedValue);
ddlCity.DataValueField = "CityId";
ddlCity.DataTextField = "city";
ddlCity.DataBind();
}
However, I am getting this error in ddlCountry1. The Name 'ddlCountry1' does not exist in the Current context.
For each row you select the country must be loaded your cities. In this case, 3 lines and you select Brazil, Argentina and Colombia in this order. Show on line 1 the cities of Brazil, Line 2 cities of Argentita ... that’s it?
– Cleidson_eng