2

我希望我的 ajaxToolkit:TabContainer 有一个允许用户添加另一个选项卡的选项卡面板。我只希望它在他们单击“+”选项卡面板而不是其他选项时回发。我似乎无法阻止 Javascript 中的事件冒泡:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
    function checkTab(sender, e) {
        if (sender.get_activeTab().get_headerText().replace("<span>", "").replace("</span>", "") != "+") {
            cancelBubble(e);

        }
        else {
            if (!confirm('Are you sure?')) {
                cancelBubble(e);
            }
        }
    }
    function cancelBubble(e) {
        if (e) {
            e.stopPropagation();
        }
        else {
            window.event.cancelBubble = true;
        }
    }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager" runat="server">
</asp:ScriptManager>
  <ajaxToolkit:TabContainer ID="MyTabContainer" runat="server" OnActiveTabChanged="MyTabContainer_OnActiveTabChanged"
        AutoPostBack="true" OnClientActiveTabChanged="checkTab">
        <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="My First Tab" Enabled="true">
            <ContentTemplate>
                My first tab
            </ContentTemplate>
        </ajaxToolkit:TabPanel>
        <ajaxToolkit:TabPanel ID="AddTabPanel" runat="server" HeaderText="+" Enabled="true">
            <ContentTemplate>
            </ContentTemplate>
        </ajaxToolkit:TabPanel>
    </ajaxToolkit:TabContainer>
</div>
</form>
</body>
</html>


 protected void MyTabContainer_OnActiveTabChanged(object sender, EventArgs e)
    {
        TabPanel tp = new TabPanel();
        tp.HeaderText = "New Tab";
        MyTabContainer.Tabs.Add(tp);
    }

谢谢,亚历克斯

4

3 回答 3

1

您可以return false;在 JavaScript 中使用来停止 PostBack。所以我认为你所需要的就是:

function checkTab(sender, e) 
{
    if (sender.get_activeTab().get_headerText().replace("<span>", "").replace("</span>", "") != "+") 
    {
        return false;
    }
    else 
    {
       return confirm('Are you sure?');
    }
}
于 2013-04-17T13:25:15.220 回答
0

感谢 jadarnel27,这是我寻求的最终解决方案:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
<script type="text/javascript">
    function addTab() {

        if (confirm('Are you sure?')) {
            document.getElementById('<%=AddTabButton.ClientID %>').click();
        }
    }
</script>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server">
</asp:ScriptManager>
 <asp:Button ID = "AddTabButton" runat="server" OnClick="AddTabButton_OnClick" CssClass="DisplayNone" />
  <ajaxToolkit:TabContainer ID="MyTabContainer" runat="server">
    <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="My First Tab" Enabled="true">
        <ContentTemplate>
            My first tab
        </ContentTemplate>
    </ajaxToolkit:TabPanel>
    <ajaxToolkit:TabPanel ID="AddTabPanel" runat="server" HeaderText="+" Enabled="true" OnClientClick="addTab">
        <ContentTemplate>
        </ContentTemplate>
    </ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
</div>
</form>
</body>
</html>


protected void AddTabButton_OnClick(object sender, EventArgs e)
    {
       TabPanel tp = new TabPanel();
    tp.HeaderText = "New Tab";
    MyTabContainer.Tabs.Add(tp);
    }
于 2013-04-18T10:06:14.253 回答
0

将下面的脚本添加到您的项目中,并在页面的最底部添加参考:

Sys.Extended.UI.TabPanel.prototype.raiseClick = function (eventArgs) {
    var eh = this.get_events().getHandler("click");
    if (eh) {
        eh(this, eventArgs);
    }
};

Sys.Extended.UI.TabPanel.prototype._header_onclick = function (e) {
    e.preventDefault();
    var eventArgs = new Sys.CancelEventArgs();
    this.raiseClick(eventArgs);

    if (eventArgs.get_cancel() === true)
        return;

    this.get_owner().set_activeTab(this);
    this._setFocus(this);
};

现在我们添加了取消单击客户端上特定选项卡的功能。使用示例:

 <script type="text/javascript">
      function AddTabOnClientClick(sender, args) {
           args.set_cancel(!confirm("Are you sure?"));
      }
 </script>

 <form id="form1" runat="server">
 <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
 </ajaxToolkit:ToolkitScriptManager>
 <div>
      <asp:UpdatePanel runat="server">
           <ContentTemplate>
                <ajaxToolkit:TabContainer ID="MyTabContainer" runat="server" AutoPostBack="true"
                     ActiveTabIndex="0">
                     <ajaxToolkit:TabPanel ID="TabPanel1" runat="server" HeaderText="My First Tab" Enabled="true">
                          <ContentTemplate>
                               My first tab
                          </ContentTemplate>
                     </ajaxToolkit:TabPanel>
                     <ajaxToolkit:TabPanel ID="AddTabPanel" runat="server" HeaderText="+" OnClientClick="AddTabOnClientClick">
                          <ContentTemplate>
                          </ContentTemplate>
                     </ajaxToolkit:TabPanel>
                </ajaxToolkit:TabContainer>
           </ContentTemplate>
      </asp:UpdatePanel>
 </div>
 </form>
 <script src="Scripts/MyAjaxToolkitExtensions.js" type="text/javascript"></script>
于 2013-04-17T15:40:34.050 回答