0

I have bootstrap navbars and for each tab I have a separate data to bind.So on tab change event I want to find the tab index and do the respective bindings.

For that I have taken asp HiddenField and had the OnValueChanged event

<asp:HiddenField ID="hdnField" runat="server" OnValueChanged="hdnField_ValueChanged" />

and setting the value in the jquery as following

<script type="text/javascript">
        $(document).ready(function () {
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var activeTab1 = e.target;

                var activeTab = $(e.target).text(); // Get the name of active tab
                var previousTab = $(e.relatedTarget).text(); // Get the name of previous tab
                varTab = $("#myTab").tabs("option", "selected");
                document.getElementById("<%= hdnField.ClientID %>").value = varTab;   //Setting value for hiddenfield
                $(".active-tab span").html(activeTab);
                $(".previous-tab span").html(previousTab);
            });
        });
    </script>

But the OnValueChanged event is not firing. How to set the index value for the hidden field

My html for navbars is

<ul class="nav nav-tabs" id="myTab">
<li class="active"><a data-toggle="tab" href="#sectionA">Menu</a></li>
<li><a data-toggle="tab" href="#sectionB">About</a></li>
<li><a data-toggle="tab" href="#sectionC">Help</a></li>
</ul>
Learner
  • 71
  • 13

1 Answers1

0

The HiddenField ValueChanged event only fires when the page is posted back to the server. Once a trigger is posted back, it looks in viewstate to compare old vs new value, and if different, the ValueChanged event is raised.

If you are trying to trigger from the client, you can kind of force a change event like so: hidden input change event

Not sure if you want things running on client...

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • How to pass navbar index to hiddenfield in the same function which I mentioned above – Learner Jul 31 '15 at 17:35
  • If the code you have above sets the navbar, what you have will work fine. But ValueChanged doesn't fire in the code behind until the page posts back to the server (as a POST operation). Any navigation that triggers a GET request will not be a postback, and thus won't trigger the event. – Brian Mains Jul 31 '15 at 17:37
  • What should I have to do inorder to fire to the code behind or what else events can be used to achieve the same – Learner Jul 31 '15 at 17:41
  • Anything on the server needs to post back to the server; you can trigger a __doPostBack(..); (read up on that for the exact arguments) but then you are triggering a postback every time someone clicks a nav item... I don't really know why you need ValueChanged in the first place, so if you could add that to your post that would help. I would try to do whatever you can on the client. – Brian Mains Jul 31 '15 at 17:42
  • I'm quiet new to this,actually I have written the code to run depending on the index number.So if I can get that index it is easy to perform.But finding difficulty to achieve this.If you better idea do help me with a sample program – Learner Jul 31 '15 at 17:52