Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
    {
TextBox1.Attributes.Add("onkeyup", "fillsort();");
}

 protected void fillsort()
    {
        DataList1.DataSource = ob.dataset("select * from Course where course_title like '" + TextBox1.Text + "%'");
        DataList1.DataBind();
    }


problem is this, on keyup nothing happens.. i mean fillsort() not called!!
why??
Posted

I would suggest onkeyup to be used on client side and make an AJAX call to the server side

If you need server side then use OnTextChanged
XML
<asp:TextBox ID='Textbox1' runat='server' OnTextChanged=' fillsort'>
    </asp:TextBox>
public void fillsort(Object sender, EventArgs e)
{
//your code
}
 
Share this answer
 
Hi,

We cannot call server methods directly from javascript.

It can possible only when that method is pagemethod.But here there is a problem in this situation those data to datalists are not effect while you calling server method its just used for to do some process in server side.

We can do this by using Jquery

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script language ="javascript" >
       function f1() {
           $.get("default22.aspx?action=getval", { val1: $("#txtname").val }, function (data) {
               $("#datalistdiv").html(data);
           });
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="txtname" onkeyup="f1()" />
        <br />
        <div id="datalistdiv">
        <asp:datalist id="DataList1" runat="server" width="789px" height="201px" xmlns:asp="#unknown">
            <HeaderTemplate>
                <table width="100%" align="center">
                    <tr>
                        <td>
                            Company ID
                        </td>
                        <td>
                            Company Name
                        </td>
                        <td>
                            Company Addr
                        </td>
                        <td>
                            Company Website
                        </td>
                    </tr>
            </HeaderTemplate>
            <itemtemplate>
                <tr>
                    <td>
                        <asp:label id="Lbluid" runat="server" text="<%#Eval("compid") %>"></asp:label>
                    </td>
                    <td>
                        <asp:label id="Lblpid" runat="server" text="<%#Eval("compname") %>"></asp:label>
                    </td>
                    <td>
                        <asp:label id="Label1" runat="server" text="<%#Eval("compoffaddr") %>"></asp:label>
                    </td>
                    <td>
                        <%#Eval("compsiteurl")%>
                    </td>
                </tr>
            </itemtemplate>
            <footertemplate>
                </footertemplate></table>
            
        </asp:datalist>
        </div>
    </div>
    </form>
</body>
</html>



And code behind file contains following code

C#
DataTable dt = new DataTable();
  protected void Page_Load(object sender, EventArgs e)
  {
      if (Request.QueryString["action"] != null)
      {

          Response.Clear();
          StringBuilder sb = new StringBuilder();
          StringWriter tw = new StringWriter(sb);
          HtmlTextWriter hw = new HtmlTextWriter(tw);
          DataList1.DataSource=dt;
          DataList1.DataBind();
          DataList1.RenderControl(hw);
          Response.Write(sb.ToString ());
          Response.End();


      }
  }


In the above code I used datatable(dt) for binding .you've to change whatever you want to bind

All the Best
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900