Autocomplete
textbox can be implemented in several ways as i had discussed also in
earlier posts, but HTML 5 have some inbuild feature by which we can
implement autocomplete feature for textbox quite easily. The most likely
thing for me about HTML 5 is, its several inhancement in basic HTML
input controls like <b>list</b> property of textbox.
There are several other also, like addition of new tags like <b>datalist</b>. We'll create autocomplete textbox using these two new additions only. But before starting the actual business, lets setup our environment first:
1) We can use visual studio 2010 with service pack 1. Without sp1, visual studio 2010 doesn't have HTML 5.
2) After sp1 we'll need to go to Tool Menu -> Option -> TextEditor -> HTML TAB -> Validation -> Select HTML 5 from Target dropdown.
You can bind it through database also:
Create HTML like:
and in code behind
But there is one limitation to this approach, many browsers doesn't support HTML5.
There are several other also, like addition of new tags like <b>datalist</b>. We'll create autocomplete textbox using these two new additions only. But before starting the actual business, lets setup our environment first:
1) We can use visual studio 2010 with service pack 1. Without sp1, visual studio 2010 doesn't have HTML 5.
2) After sp1 we'll need to go to Tool Menu -> Option -> TextEditor -> HTML TAB -> Validation -> Select HTML 5 from Target dropdown.
- <input list="Employees" type="text" />
- <datalist id="Employees">
- <option value="Adarsh"></option>
- <option value="Madhukar"></option>
- <option value="Amar"></option>
- <option value="Avinash"></option>
- <option value="Kundan"></option>
- <option value="Amit"></option>
- <option value="Viresh"></option>
- <option value="Vivek"></option>
- </datalist>
You can bind it through database also:
Create HTML like:
- <input list="lstEmployees" type="text" id="Employee" runat="server" />
- <datalist id="lstEmployees" runat="server"></datalist>
and in code behind
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- bindListEmployee();
- }
- private void bindListEmployee()
- {
- using (Model.ModelContainer context = new Model.ModelContainer())
- {
- var q = from c in context.Employees
- select new {c.Id, Employee = c.EmployeeName + " "+ c.Designation };
- foreach (var item in q)
- {
- lstEmployees.InnerHtml += "<option value='"+ item.Employee +"'>";
- }
- }
- }
But there is one limitation to this approach, many browsers doesn't support HTML5.
No comments:
Post a Comment