0

I have created a tab in ASP.Net using Bootstrap.

<div>
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active">
            <a href="#divrecentQ" id="linkdivrecentQ" aria-controls="divrecentQ" role="tab" data-toggle="tab">
                Recent Questions
            </a>
        </li>
        <li role="presentation">
            <a href="#divpriorityQ" aria-controls="divpriorityQ" role="tab" data-toggle="tab">
                Priority Questions
            </a>
        </li>
    </ul>

    <div class="tab-content">
        <div role="tabpanel" class="tab-pane fade in active" id="divrecentQ">
            ...Recent
            <div role="tabpanel" class="tab-pane fade" id="divpriorityQ">
                ..Priority.
            </div>
        </div>

I want to make a server call on click of tabs, but since it is in data-toggle mode, it can't fire anything on server side. I tried to make a call using jQuery.

<script>


    $("#linkdivrecentQ").click(function () {

        alert($(this).attr('id'));
        loadimages($(this));
        $.ajax({
            type: 'POST',
            url: '<%= ResolveUrl("~/amain.aspx/LoadImages") %>',
            data: '{ }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                alert(msg.d)
            }
                    });
    });

</script>

For the code in server side:

public static void LoadImages()
{
    log.Debug("LoadImages is called");
}

But server code is not called. Thanks for your help.

Dheeraj Kumar
  • 3,917
  • 8
  • 43
  • 80

2 Answers2

1

Use this:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
<div> 
                <!-- Nav tabs -->
                <ul class="nav nav-tabs" role="tablist">
                    <li role="presentation" class="active"><a href="#divrecentQ" id="linkdivrecentQ" onclick="GetCurrentString();" aria-controls="divrecentQ" role="tab" data-toggle="tab">Recent Questions</a></li>
                    <li role="presentation"><a href="#divpriorityQ" aria-controls="divpriorityQ" role="tab" data-toggle="tab">Priority Questions</a></li>
                </ul>

                <!-- Tab panes -->
                <div class="tab-content">
                    <div role="tabpanel" class="tab-pane fade in active" id="divrecentQ">
                        ...Recent
                    </div>
                    <div role="tabpanel" class="tab-pane fade" id="divpriorityQ">..Priority.</div>

                </div>
            </div>

jquery function for the GetCurrentString function in #linkdivrecentQ anchor:

   function GetCurrentString() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetCurrentString",
            data: '{name : "pandey" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }

function OnSuccess(response) {
    alert(response.d);
}

here GetCurrentString is method in the server code

[System.Web.Services.WebMethod]
    public static string GetCurrentString(string name)
    {
        return "vishal "+name;
    }
RonyLoud
  • 2,408
  • 2
  • 20
  • 25
0

An approach I took was to make it an html server control (runat="server") and activate the tab without using data-toggle="tab".

     <a data-target="#divrecentQ" id="linkdivrecentQ" aria-controls="divrecentQ" 
        role="tab" runat="server" onserverclick="GetCurrentString" 
        onclick="showTab('#linkdivrecentQ');" clientidmode="static">
        Recent Questions
     </a>
  • changed href to data-target because onserverclick() uses href. You can see this in the inspector once it's rendered.
  • I placed clientidmode="static" so the id is exactly as stated and doesn't get a parent naming container aggregated to it. You could remove this and use onclick="showTab('#<%=linkdivrecentQ.ClientID%>');" instead.

The showTab() activates the data-target specified in the <a>.

    function showTab(id) {
        $(id).tab('show');
    }

I will also add I am using an update panel inside the tab panel <div> to keep the active tab from changing to default due to postback. If you don't want to use an update panel you could make the call to showTab() in the code behind instead of the onclick() on control.

cjadd7
  • 123
  • 1
  • 7