简体   繁体   中英

Htmlselect selected index changed event

I have 2 HtmlSelect controls and i need to load second one according to first HtmlSelect's selected index. But it doesn't work, because HtmlSelect doesnt have selectedindexchanged event. What do i need to do? Any idea?

Example;

<!--These are my Html Select controls-->

 select runat="server" id="drpCity"></select> 
 <select runat="server" id="drpState">/select>



  protected void Page_Load(object sender, EventArgs e)
  {
     FillCities(drpCity);
     FillStates(drpState, drpCity.SelectedValue);
  }

  public void FillCitiesHtmlSelect(HtmlSelect drpCity)
        {
           BusCity busCity = new BusCity();
           List<EntCity> lentCity = busCity.SearchAll();
           drpCity.DataValueField = EntCity.Columns.CITYCODE;
           drpCity.DataTextField = EntCity.Columns.CITYNAME;
           drpCity.DataSource = lentCity;
           drpCity.DataBind();

            drpCity.Items.Insert(0, new ListItem("City", string.Empty));
        }

public void FillStatesHtmlSelect(HtmlSelect drpState, string cityCode)
{
    if (!string.IsNullOrEmpty(cityCode))
       {
          BusState busState = new BusState();
          List<EntState> lentEmsState = busState.Search(cityCode);
          drpState.DataValueField = EntState.Columns.STATECODE;
          drpState.DataTextField = EntState.Columns.STATENAME;
          drpState.DataSource = lentState;
          drpState.DataBind();
        }
        else
        {
          drpState.Items.Clear();
        }
           drpState.Items.Insert(0, "State", string.Empty));
   }

I can suggest an approach, though i have not implemented it. If you can call a javascript function from your one select control to check for a non-default value and if the condition meets, load the second select html.

HTML select控件没有'selectedindexchanged'事件,而是客户端事件'onchange'。同样,您不能从html select控件调用服务器端。要调用在C#代码页(服务器端)中编写的函数,您需要使用webservice将您的C#函数定义为webmethord,并在html select控件的“ onchange”事件中使用网络服务调用函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM