Kvadiyatar
Kvadiyatar

Reputation: 964

how to call server side function from javascript

i am looking to set sorting on ASP Repeater. see my repeater code:

 <asp:Repeater runat="server" ID="RptClientDetails">
                    <HeaderTemplate>
                        <table id="example" class="dynamicTable table table-striped table-bordered table-primary">
                            <thead>
                                <tr>
                                    <th>

                                 <a href="#" onclick="ClientSort('ClientID')">Client ID</a>


                                    </th>
                                    <th>
                                     <a href="#" onclick="ClientSort('Name')">Name</a>


                                    </th>
                                    <th>
                                     <a href="#" onclick="ClientSort('TotalBalanceDue')">Total Balance Due</a>


                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                    </HeaderTemplate>
                    <ItemTemplate>

here, i am calling javascript function. see my javascript function code:

function ClientSort(SortExpress) {

           <%= Sorting(SortExpress) %>
         }

from here i want to call my .net server side function.

public void Sorting(string SortExpression)
{
    string s = SortExpression;

}

so, have you idea how can i call it? or directly from repeater i can call this server side function..Thanks

Upvotes: 2

Views: 8862

Answers (4)

Chandra
Chandra

Reputation: 1

I had a similar issue got fixed by following code...hope someone will benefit:- I need to call a server side function (button click event functionality) from client side just by Onblur of a Textbox:

asp:TextBox ID="txtNum" runat="server" CssClass="Textbox" Width="170px" MaxLength="8" onblur="javascript:return check2();"/>"

 //JAVASCRIPT - onblur calls button click serverside function
 function check2() {
     document.getElementById("<%=btncheck.ClientID%>").click(); 
 }

asp:Button ID="btncheck" runat="server" Text="Verify" OnClick="check1" />

//CODE BEHIND - On clicking the Button
protected void check1(object sender, EventArgs e)
{
    string str1 = txtBox.Text;
    CheckCCN2(str1);        
}
//Onblur of the Textbox txtNum   
public void CheckCCN2(string strCCNNum)
{
  //all your server side code    
}

Upvotes: 0

watraplion
watraplion

Reputation: 287

Directly you can call server side code from repeater control, just use Linkbutton instead of href.

<asp:Repeater runat="server" ID="RptClientDetails">
   <HeaderTemplate>
            <table id="example" class="dynamicTable table table-striped table-bordered table-primary">
                <thead>
                    <tr>
                        <th>
                            <%--<a href="#" onclick="ClientSort('ClientID')">Client ID</a>--%>
                            <asp:LinkButton ID="lbtn" Text="Client ID" ForeColor="Blue" OnCommand="lbtnSorting_Click" CommandArgument="ClientID" runat="server"></asp:LinkButton>
                        </th>
                        <th>
                            <asp:LinkButton ID="LinkButton1" Text="Name" ForeColor="Blue" OnCommand="lbtnSorting_Click" CommandArgument="Name" runat="server"></asp:LinkButton>
                            <%--<a href="#" onclick="ClientSort('Name')">Name</a>--%>
                        </th>
                        <th>
                            <asp:LinkButton ID="LinkButton2" Text="Total Balance Due" ForeColor="Blue" OnCommand="lbtnSorting_Click" CommandArgument="TotalBalanceDue" runat="server"></asp:LinkButton>
                            <%--<a href="#" onclick="ClientSort('TotalBalanceDue')">Total Balance Due</a>--%>
                        </th>
                    </tr>
                </thead>
                <tbody>
        </HeaderTemplate>
</asp:Repeater>

And the Code Behind :

protected void lbtnSorting_Click(object sender, CommandEventArgs e)
    {
        string s = e.CommandArgument.ToString();
    }

Upvotes: 2

bash.d
bash.d

Reputation: 13207

You cannot simply call an ASP.NET-method from your JavaScript. This is because ASP.NET is purely server-side and JavaScript is client-side.
ASP.NET generates HTML and JavaScript from your .NET code and this happens ONLY during postbacks or Initial loads.
ASP.NET renders a side, provides it and from this Moment on is out of the game until postback.

See ASP.NET-page lifecycle and this post.

Upvotes: 2

Igor
Igor

Reputation: 15893

To invoke server-side code without reloading the page, use jquery ajax function: http://api.jquery.com/jQuery.ajax/

Upvotes: 1

Related Questions