Dropdownlist The Name '' does not exist in the Current context

Asked

Viewed 140 times

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?

1 answer

0

Doing some tests using a similar code, first he was not able to obtain and load the Dropdownlist inside the 'Edititemtemplate'. So I used with Dropdownlist within the 'Itemtemplate'.

Using the Event in Country 'Onselectedindexchanged' with 'Autopostback="true"', I also added an attribute to identify which line’s input 'Indice='<%# Container.Dataitemindex %>'.

  `<asp:TemplateField HeaderText="País" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="ddlCountry1" Height="39px" Width="190px"  AutoPostBack="true" OnSelectedIndexChanged="ListarCidade1"
                     DataTextField="Name" DataValueField="CountryID"  indice='<%# Container.DataItemIndex %>'>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    <asp:TemplateField HeaderText="Cidade" ItemStyle-Width="100">
            <ItemTemplate>
            <asp:DropDownList runat="server" ID="ddlCity1" Height="39px" Width="190px" AutoPostBack="true">
                </asp:DropDownList>
        </ItemTemplate>
        </asp:TemplateField>`

The event fired was done as follows, it obtains the index of the line that holds the Country Dropdownlist, in order to obtain its respective Dropdownlist of cities, informing in which column City is.

protected void ListarCidade1(object sender, EventArgs e)
    {
        DropDownList ddlCountry = (DropDownList)sender;
        int indice = Convert.ToInt32(ddlCountry.Attributes["indice"]);
        DropDownList ddlCity = (DropDownList)gdvCidade.Rows[indice].Cells[1].FindControl("ddlCity1");
        ddlCity.DataSource = listCityByCountry(ddlCountry.SelectedValue);
        ddlCity.DataValueField = "CityId";
        ddlCity.DataTextField = "city";
        ddlCity.DataBind();
    }

If Gridview has any action at the event Page_load, must insert the !Ispostback

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvRentalVendor.DataSource = //Lista;
            gvRentalVendor.DataBind();
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.