ajaxtoolkit:TabPanel  包含一个updatePanel,UpdatePanel中有一个DropDownList,当触发SelectedIndexChange事件时,代码根据select到内容动态无刷新更改TabPanel的HeaderText。首先在SelectedIndexChange事件方法中直接给TabPanell.Header="xxx";是不行的。那么我们就得另外找方法。

例子:aspx代码:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.  
  3. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>  
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title>Untitled Page</title>  
  10.     <script language="javascript" type="text/javascript">  
  11.         function updatePanelName() {  
  12.             var span = document.getElementById('__tab_TabContainer1_tabpenal1');  
  13.             span.innerText = "sss";  
  14.         }  
  15.         function changeName(ddl) {  
  16.             var span = document.getElementById('__tab_TabContainer1_tabpenal1');  
  17.             span.innerText = ddl.selectedIndex;  
  18.         }  
  19.         function serverClick() {   
  20.             document.getElementById("changeNewInvoicesTabNameLinkButton").click();  
  21.         }  
  22.         function changeName1(ddl) {   
  23.             //var tc = $find(tabContainerId);  
  24. //            var tc = document.getElementById("TabContainer1");  
  25. //            var tabs = tc.get_tabs();  
  26.             //            tabs[0].set_headerText(ddl.selectedIndex); // changes the header of first tab  
  27. //            document.getElementById("IndexHiddenField1").value= ddl.selectedIndex;  
  28. //            serverClick();  
  29.  
  30.             $find('TabContainer1').get_tabs()[0]._header.innerHTML = ddl; //document.getElementById("FinalText").value;  
  31.  
  32.         }  
  33.     </script>  
  34. </head>  
  35. <body>  
  36.  
  37.     <form id="form1" runat="server">  
  38.      <asp:HiddenField ID="InvoiceDNHiddenField" runat="server" Value="0" />  
  39.        <asp:HiddenField ID="IndexHiddenField1" runat="server" Value="0" />  
  40.      <asp:HiddenField ID="FinalText" runat="server" />  
  41.      <asp:LinkButton ID="changeNewInvoicesTabNameLinkButton" runat="server" Style="display: none;" 
  42.         OnClick="changeNewInvoicesTabNameLinkButton_Click"></asp:LinkButton>  
  43.           
  44.     <div><cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ></cc1:ToolkitScriptManager>   
  45.         <cc1:TabContainer ID="TabContainer1" runat="server">  
  46.         <cc1:TabPanel ID="tabpenal1" HeaderText="tab1" runat="server">  
  47.             <ContentTemplate>  
  48.                <asp:UpdatePanel ID="UpdateNewInvoiceListGridView" runat="server">  
  49.                <Triggers>  
  50.                  <asp:AsyncPostBackTrigger ControlID="changeNewInvoicesTabNameLinkButton" EventName="Click" />  
  51.                </Triggers>  
  52.                    <ContentTemplate>  
  53.                           <asp:DropDownList ID="ddlTemp" runat="server" DropDownControlID="Label1"   
  54.                               AutoPostBack="true" onselectedindexchanged="ddlTemp_SelectedIndexChanged" >   
  55.                             <asp:ListItem>90</asp:ListItem>   
  56.                             <asp:ListItem>85</asp:ListItem>   
  57.                             <asp:ListItem>80</asp:ListItem>   
  58.                             <asp:ListItem>75</asp:ListItem>   
  59.                             <asp:ListItem>70</asp:ListItem>   
  60.                             <asp:ListItem>65</asp:ListItem>   
  61.                             <asp:ListItem>60</asp:ListItem>   
  62.                             <asp:ListItem>55</asp:ListItem>   
  63.                             </asp:DropDownList>   
  64.                             <asp:Button   
  65.                              runat="server"   
  66.                              ID="btnUpdate"   
  67.                              Text="Update"   
  68.                              OnClick="btnUpdate_Click" OnClientClick="updatePanelName()"  />   
  69.                             <asp:Label   
  70.                              ID="Label1"   
  71.                              runat="server"   
  72.                              ForeColor="green"   
  73.                              Font-Bold="true"   
  74.                              Font-Names="Courier New"   
  75.                              Font-Size="Large">   
  76.                             </asp:Label>  
  77.                    </ContentTemplate>  
  78.                </asp:UpdatePanel>  
  79.             </ContentTemplate>  
  80.         </cc1:TabPanel>  
  81.         </cc1:TabContainer>  
  82.     </div>  
  83.     </form>  
  84. </body>  
  85. </html>  

Aspx.cs:

 

 
  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12.  
  13. public partial class _Default : System.Web.UI.Page   
  14. {  
  15.     protected void Page_Load(object sender, EventArgs e)  
  16.     {  
  17.         InvoiceDNHiddenField.Value = "1";  
  18.     }  
  19.     protected void btnUpdate_Click(object sender, EventArgs j)  
  20.     {  
  21.         Label1.Text = "You selected: " + ddlTemp.SelectedItem.Text + " was changed at " + DateTime.Now.ToString();  
  22.         // tabpenal1.HeaderText = "sss";  
  23.         //TabContainer1.Tabs[0].HeaderText = "ss";  
  24.     }  
  25.     protected void ddlTemp_SelectedIndexChanged(object sender, EventArgs e)  
  26.     {  
  27.        // Label2.Text = ddlTemp.SelectedIndex.ToString();  
  28.         Label1.Text = "You selected: " + ddlTemp.SelectedItem.Text + " was changed at " + DateTime.Now.ToString();  
  29.         if (InvoiceDNHiddenField.Value == "1")  
  30.         {  
  31.             string text = "";  
  32.             if (ddlTemp.SelectedIndex == 0 )  
  33.             {  
  34.                 text = "Re-send Invoices";  
  35.                 this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "age1""changeName1('" + text + "');"true);  
  36.  
  37.                 ScriptManager.RegisterClientScriptBlock(this.UpdateNewInvoiceListGridView, typeof(Page), "key1""changeName1('" + text + "');"true);  
  38.  
  39.             }  
  40.             else 
  41.             {  
  42.                 //FinalText.Value = "New invoices";  
  43.                 text = "New invoices";  
  44.                 ScriptManager.RegisterClientScriptBlock(this.UpdateNewInvoiceListGridView, typeof(Page), "key1""changeName1('" + text + "');"true);  
  45.             }  
  46.         }  
  47.     }  
  48.     protected void changeNewInvoicesTabNameLinkButton_Click(object sender, EventArgs e)  
  49.     {  
  50.         if (InvoiceDNHiddenField.Value == "1")  
  51.         {  
  52.             string text="";  
  53.             if (IndexHiddenField1.Value == "0")  
  54.             {text= "Re-send Invoices";  
  55.                   this.Page.ClientScript.RegisterStartupScript(this.GetType(), "age1""changeName1('" + text + "');"true);  
  56.             }  
  57.             else 
  58.             {  
  59.                 //FinalText.Value = "New invoices";  
  60.                 text = "New invoices";  
  61.                 this.Page.ClientScript.RegisterStartupScript(this.GetType(), "age1""changeName1('" + text + "');"true);  
  62.             }  
  63.         }  
  64.     }  
  65.       
  66. }  

通过上面例子,可以发现实现思想是:

1.首先知道TabPanel的header的JS属性:

 

 

$find('TabContainer1').get_tabs()[0]._header.innerHTML

 或者$find(tabpanel1)._header.innerHTML;

2.然后再通过后台代码传入经过逻辑处理后的HeaderName作为JS参数,最后调用JS函数通过前台执行HeaderText赋值.

查了很多资料才找到解决方案,有需要的感谢我吧~~