如何使用Javascript访问EditItemTemplate中的下拉列表的ID?

时间:2015-08-26 07:28:52

标签: javascript c# asp.net

我想访问GridView的EditItemTemplate中的dropdownlist的id。我可以使用

访问FooterRow中的id
 var ddlpresentday = document.getElementById("<%=((DropDownList)gridEarning.FooterRow.
FindControl("ddlFPresentDay")).ClientID%>");

但在GridView处于编辑模式时遇到问题。 请帮助我。

GridView的:

<asp:GridView ID="gridEarning" runat="server">
  <asp:TemplateField HeaderText="Present Days to be considerd on" 
    SortExpression="TAXABLE" meta:resourcekey="TemplateField19Resource">
    <EditItemTemplate>
      <asp:DropDownList ID="ddlEditPresentDay" CssClass="ddlEditPresentDay" 
        runat="server" Enabled="false" Height="20px" Width="110px"      
        meta:resourcekey="ddlPresentDayResource2">
          <asp:ListItem Value="0" meta:resourcekey="ddlPreDaysListItemResource1">All Days</asp:ListItem>
          <asp:ListItem Value="1" meta:resourcekey="ddlPreDaysListItemResource2">Business Days</asp:ListItem>
          <asp:ListItem Value="2" meta:resourcekey="ddlPreDaysListItemResource3">Off Days and Holidays</asp:ListItem>
      </asp:DropDownList>
    </EditItemTemplate>
    <FooterTemplate>
      <asp:DropDownList ID="ddlFPresentDay" runat="server" Height="20px" Enabled="false" Width="110px" meta:resourcekey="ddlPresentDayResource2">
        <asp:ListItem Value="0" meta:resourcekey="ddlPreDaysListItemResource1">All Days</asp:ListItem>
        <asp:ListItem Value="1" meta:resourcekey="ddlPreDaysListItemResource2">Business Days</asp:ListItem>
        <asp:ListItem Value="2" meta:resourcekey="ddlPreDaysListItemResource3">Off Days and Holidays</asp:ListItem>
      </asp:DropDownList>
    </FooterTemplate>
    <ItemTemplate>
      <asp:Label ID="lblPresentDays" runat="server" Text="Helllo"
        meta:resourcekey="lblPresentDaysResource1"></asp:Label>
    </ItemTemplate>
  </asp:TemplateField>
</GridView>

1 个答案:

答案 0 :(得分:0)

JQuery在这种情况下是非常有用的(特别是css类选择器)。无论如何,即使没有jquery,你也可以定义具有唯一id的自定义包装元素,并依赖于该元素 - 例如:

<FooterTemplate>
  <span id="gridFooter">
    <asp:DropDownList ... ></asp:DropDownList>
  </span>
</FooterTemplate>

然后您可以选择它:

var ddlpresentday = document.getElementById("gridFooter").firstElementChild;

修改

添加jQuery解决方案:

<FooterTemplate>
  <asp:DropDownList CssClass="unique-footer-select" ... ></asp:DropDownList>
</FooterTemplate>  

var ddlpresentday = $(".unique-footer-select");