Data binding with html5 DataList in asp.net

I am trying to use HTML5. Is it possible to bind data with a datalist in html5 as we bind data from the datatable control to asp.net.

Where can I find this information. any pointers are much appreciated. :)

thank

+5
source share
1 answer

1) Assign runat="server"to the data directory so that it can be accessed from the code:

Enter your favorite browser name:<br />
<input id="browserName" list="browsers" />
<datalist id="browsers" runat="server" /> 

2) Scroll through DataTable, build and combine the list of options with StringBuilderand add the result to the InnerHtmldatalist property

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("BrowserName");
        table.Rows.Add("IE");
        table.Rows.Add("Chrome");
        table.Rows.Add("Firefox");
        table.Rows.Add("Opera");
        table.Rows.Add("Safari");

        var builder = new System.Text.StringBuilder();

        for (int i = 0; i < table.Rows.Count; i++)
            builder.Append(String.Format("<option value='{0}'>",table.Rows[i][0]));
        browsers.InnerHtml = builder.ToString();
    }

, WCF jQuery, datalist HTTP :

1) AutoCompleteHandler.ashx

2) AutoCompleteHandler.ashx put:

public class AutoCompleteHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Clear();

        var options = new System.Text.StringBuilder();
        options.Append("<option value='IE'>");
        options.Append("<option value='Chrome'>");
        options.Append("<option value='Firefox'>");
        options.Append("<option value='Safari'>");
        options.Append("<option value='Opera'>");

        context.Response.Write(options.ToString());
        context.Response.End();
    }
    public bool IsReusable
    {
        get{return false;}
    }
}

3) jQuery, datalist :

<script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.post('AutoCompleteHandler.ashx', function (data) {
            $('#browsers').html(data);
        });
    });
</script>
+8

All Articles