簡體   English   中英

如何使用asp.net獲取jquery select2標簽列表的值?

[英]how to get values of jquery select2 tags list using asp.net?

我想在asp.net代碼后面獲取jquery select2插件的選擇值。

這是我的資料

客戶端:

 <select id="ddlcountry" runat="server"  class="select" style="width: 100%;">                                                                                                       
 </select>

背后的代碼:

var query1 = from pcountry in CTX.Countries
              orderby pcountry.Country1 ascending
              select new
             {
              pcountry.CountryId,
              pcountry.CountryName
              };

            if (query1 != null)
            {
                ddlcountry.DataSource = query1.ToList();
                ddlcountry.DataValueField = "CountryId";
                ddlcountry.DataTextField = "CountryName";
                ddlcountry.DataBind();               

                ddlcountry.Multiple = true;
            }

 protected void btnSave_Click(object sender, EventArgs e)
    {

  Now i want to get all the selected country values here ?

}

請幫助我,我將非常感激。

如果您沒有將<select>元素創建為<ASP:DropDownList>控件,那么我認為您將無法訪問屬性和方法,例如“ Datasource”等。但是,您可以訪問HTML元素其中添加了runat="server"屬性。

我相信<option>標簽是通用HTML控件類型。 我堅持使用VB.Net,所以我必須為您編寫一些偽代碼...

For each o as HtmlGenericControl in ddlcountry.Children.ofType(of HtmlGenericControl)
    If o.Attributes("selected") then
        ' Here you can access your option value using the "value" attribute (and InnerHtml works too, I think)
    End if
Next o

這是一個答案的鏈接,在該答案中,我展示了如何使用一個有效的示例遍歷通用控件:

感謝Lopside,感謝您的幫助。我嘗試了此操作,但對我而言不起作用。

根據您的回答,我想出一些解決方案,是否應該在客戶端獲取select2選項列表值並將其存儲到hiddenfield中,然后將隱藏字段值獲取到服務器端代碼中?

您可以循環瀏覽項目集合,以找出已選擇的國家。

foreach (ListItem li in ddlcountry.Items) {
    if ((li.Selected)) {
        //Selected Country
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM