Extract value in datalist

1

As I can extract the value of the query in my DataList , I have the following fragment in my page aspx

<div class="form-group">
  <label for="" class="col-md-2">País</label>
  <div class="col-md-4"><input class="form-control" list="lpais" name="paisc" id="paisc" placeholder="Seleccione pais" onBlur="return searchCountry();"><br></div>

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
     ConnectionString="<%$ ConnectionStrings:Administration %>" 
     ProviderName="<%$ ConnectionStrings:Administration.ProviderName %>" 
     SelectCommand="SELECT ltrim(rtrim([name])) as 'City' FROM [Country]">
</asp:SqlDataSource>

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource2">
  <ItemTemplate>
    <div><datalist id='lpais'> 
         <% for (int i = 0; i < DataList1.Items.Count;i++ )
         {
            var namecity = DataBinder.Eval(Container.Item); %>
            <option value='<%= namecity%>'>
         <% } %>
    </datalist></div>
  </ItemTemplate>
</asp:DataList>
</div>

I would like to obtain the value that the query brings, approximately it brings 500 records and assign them to option value

    
asked by Miguel 28.06.2017 в 17:34
source

1 answer

1

If all you need to do is show the values in a DropDown , the DropDownList control is the indicated one. Using DataList is best applied in a scenario where you are going to select, edit and delete values.

<asp:SqlDataSource ID="SqlDataSource2" runat="server"
     ConnectionString="<%$ ConnectionStrings:Administration %>" 
     ProviderName="<%$ ConnectionStrings:Administration.ProviderName %>" 
     SelectCommand="SELECT ltrim(rtrim([name])) as 'City' FROM [Country]">
</asp:SqlDataSource>

<asp:DropDownList id="CityDropdown" runat="Server" DataTextField="City" 
 DataValueField="City" DataSourceId="SqlDataSource2" />

Now, if you also want to select the item (DropDown) using jQuery, this will be useful:

$("#<%= CityDropdown.ClientID %>")
    
answered by 28.06.2017 / 18:06
source