ajaxToolkit:TabContainer添加标签时确认



我希望我的ajaxToolkit:TabContainer有一个tabpanel,允许用户添加另一个选项卡。我只希望它回发时,他们已经点击"+"选项板,没有其他。我似乎无法阻止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);
    }

谢谢,亚历克斯

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

function checkTab(sender, e) 
{
    if (sender.get_activeTab().get_headerText().replace("<span>", "").replace("</span>", "") != "+") 
    {
        return false;
    }
    else 
    {
       return confirm('Are you sure?');
    }
}

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

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>

感谢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);
    }

相关内容

  • 没有找到相关文章

最新更新