2014-09-23 48 views
0

在下面的代碼中,我有一個gridview內的下拉列表,我想獲取下拉列表的id。在哪裏我得到對象引用error.I嘗試下面的代碼它拋出對象引用的錯誤在頁面上加載。請幫我解決這個問題。要獲取在gridview下拉的Id

$(document).ready(OnReady); 

     function OnReady() { 
      //Handle the change event for the drop down list 
      $("#ddlLocation").change(onChange); 
     } 

     function onChange() { 
      //create the ajax request 
      $.ajax 
      (
      { 
       type: "POST", //HTTP method 
       url: "NewIndent.aspx/OnContinentChange", //page/method name 
       data: "{'continentName':'" + $("#<%=ddlLocation.ClientID%>").val() + "'}", //json to represent argument 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: callback, 
       error: onError 
      } 
     ); 

     } 

     //Handle the callback on success 
     function callback(msg) { 
      //it shows object object 
      var val = msg.d; 
      var countries = val.split(';'); 
      var length = countries.length; 
      //var ID = gvProduct.DataKeys[e.Row.RowIndex]["ddlProduct"].ToString(); 
      document.getElementById('<%=gvProduct.FindControl("ddlProduct").ClientID%>').options.length = 0; 
      // var Ier = gvProduct.DataKeys[e.Row.RowIndex]["ddlProduct"].ToString(); 
      var dropDown = document.getElementById('<%=gvProduct.FindControl("ddlProduct").ClientID%>'); 
      for (var i = 0; i < length - 1; ++i) { 
       var option = document.createElement("option"); 
       option.text = countries[i]; 
       option.value = countries[i]; 

       dropDown.options.add(option); 
      } 
     } 

     //Handle the callback on error 
     function onError() { 
      alert('something went wrong'); 
     } 



<asp:GridView Width="100%" runat="server" ID="gvProduct" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" ShowFooter="true" 
          PageSize-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true" AllowPaging="true" AllowSorting="true"        
          CssClass="mGrid" OnRowDataBound="gvProduct_RowDataBound" OnRowDeleting="gvProduct_RowDeleting" 
          PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"> 
<asp:TemplateField HeaderText="Product Name" ItemStyle-Width="350px"> 
            <ItemTemplate> 
             <asp:DropDownList ID="ddlProduct" runat="server" OnSelectedIndexChanged="ddlProduct_SelectedIndexChanged" AutoPostBack="true" Style="width: 100%; height:23px" ></asp:DropDownList>                    


            </ItemTemplate>         
           </asp:TemplateField> 
    </asp:GridView>  
+0

你想在GridView中使用_which_'DropDownList'的ID嗎?網格中的每一行都包含該下拉列表。 – 2014-09-23 13:51:39

+0

@Tim Schmelter是的,我想獲得內部網格下拉的id(ddlProduct)。 – user3930037 2014-09-23 13:53:47

+0

@Tim Schmelter我想獲得id = ddlProduct的下拉列表 – user3930037 2014-09-23 13:57:11

回答

0

ddlProductGridView中連續排列。如果您的GridView中有多個單行,則不會有單個行,而是其中的一大堆。他們都是不同的。您需要首先獲得您想要的值爲ddlProduct的行。

通常一個GridView使得客戶端作爲table,與tr的和td的,所以你需要遍歷DOM才能到dropdowlist。

0

既然你已經在使用jQuery,爲什麼不把類名添加到你的下拉列表中&用於選擇你的下拉列表?這比爲您的網格中的所有客戶端ID生成腳本要容易得多。