ajaxtoolkit:TabPanel 包含一个updatePanel,UpdatePanel中有一个DropDownList,当触发SelectedIndexChange事件时,代码根据select到内容动态无刷新更改TabPanel的HeaderText。首先在SelectedIndexChange事件方法中直接给TabPanell.Header="xxx";是不行的。那么我们就得另外找方法。
例子:aspx代码:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- <script language="javascript" type="text/javascript">
- function updatePanelName() {
- var span = document.getElementById('__tab_TabContainer1_tabpenal1');
- span.innerText = "sss";
- }
- function changeName(ddl) {
- var span = document.getElementById('__tab_TabContainer1_tabpenal1');
- span.innerText = ddl.selectedIndex;
- }
- function serverClick() {
- document.getElementById("changeNewInvoicesTabNameLinkButton").click();
- }
- function changeName1(ddl) {
- //var tc = $find(tabContainerId);
- // var tc = document.getElementById("TabContainer1");
- // var tabs = tc.get_tabs();
- // tabs[0].set_headerText(ddl.selectedIndex); // changes the header of first tab
- // document.getElementById("IndexHiddenField1").value= ddl.selectedIndex;
- // serverClick();
- $find('TabContainer1').get_tabs()[0]._header.innerHTML = ddl; //document.getElementById("FinalText").value;
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:HiddenField ID="InvoiceDNHiddenField" runat="server" Value="0" />
- <asp:HiddenField ID="IndexHiddenField1" runat="server" Value="0" />
- <asp:HiddenField ID="FinalText" runat="server" />
- <asp:LinkButton ID="changeNewInvoicesTabNameLinkButton" runat="server" Style="display: none;"
- OnClick="changeNewInvoicesTabNameLinkButton_Click"></asp:LinkButton>
- <div><cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" ></cc1:ToolkitScriptManager>
- <cc1:TabContainer ID="TabContainer1" runat="server">
- <cc1:TabPanel ID="tabpenal1" HeaderText="tab1" runat="server">
- <ContentTemplate>
- <asp:UpdatePanel ID="UpdateNewInvoiceListGridView" runat="server">
- <Triggers>
- <asp:AsyncPostBackTrigger ControlID="changeNewInvoicesTabNameLinkButton" EventName="Click" />
- </Triggers>
- <ContentTemplate>
- <asp:DropDownList ID="ddlTemp" runat="server" DropDownControlID="Label1"
- AutoPostBack="true" onselectedindexchanged="ddlTemp_SelectedIndexChanged" >
- <asp:ListItem>90</asp:ListItem>
- <asp:ListItem>85</asp:ListItem>
- <asp:ListItem>80</asp:ListItem>
- <asp:ListItem>75</asp:ListItem>
- <asp:ListItem>70</asp:ListItem>
- <asp:ListItem>65</asp:ListItem>
- <asp:ListItem>60</asp:ListItem>
- <asp:ListItem>55</asp:ListItem>
- </asp:DropDownList>
- <asp:Button
- runat="server"
- ID="btnUpdate"
- Text="Update"
- OnClick="btnUpdate_Click" OnClientClick="updatePanelName()" />
- <asp:Label
- ID="Label1"
- runat="server"
- ForeColor="green"
- Font-Bold="true"
- Font-Names="Courier New"
- Font-Size="Large">
- </asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
- </ContentTemplate>
- </cc1:TabPanel>
- </cc1:TabContainer>
- </div>
- </form>
- </body>
- </html>
Aspx.cs:
- using System;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- InvoiceDNHiddenField.Value = "1";
- }
- protected void btnUpdate_Click(object sender, EventArgs j)
- {
- Label1.Text = "You selected: " + ddlTemp.SelectedItem.Text + " was changed at " + DateTime.Now.ToString();
- // tabpenal1.HeaderText = "sss";
- //TabContainer1.Tabs[0].HeaderText = "ss";
- }
- protected void ddlTemp_SelectedIndexChanged(object sender, EventArgs e)
- {
- // Label2.Text = ddlTemp.SelectedIndex.ToString();
- Label1.Text = "You selected: " + ddlTemp.SelectedItem.Text + " was changed at " + DateTime.Now.ToString();
- if (InvoiceDNHiddenField.Value == "1")
- {
- string text = "";
- if (ddlTemp.SelectedIndex == 0 )
- {
- text = "Re-send Invoices";
- this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "age1", "changeName1('" + text + "');", true);
- ScriptManager.RegisterClientScriptBlock(this.UpdateNewInvoiceListGridView, typeof(Page), "key1", "changeName1('" + text + "');", true);
- }
- else
- {
- //FinalText.Value = "New invoices";
- text = "New invoices";
- ScriptManager.RegisterClientScriptBlock(this.UpdateNewInvoiceListGridView, typeof(Page), "key1", "changeName1('" + text + "');", true);
- }
- }
- }
- protected void changeNewInvoicesTabNameLinkButton_Click(object sender, EventArgs e)
- {
- if (InvoiceDNHiddenField.Value == "1")
- {
- string text="";
- if (IndexHiddenField1.Value == "0")
- {text= "Re-send Invoices";
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "age1", "changeName1('" + text + "');", true);
- }
- else
- {
- //FinalText.Value = "New invoices";
- text = "New invoices";
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "age1", "changeName1('" + text + "');", true);
- }
- }
- }
- }
通过上面例子,可以发现实现思想是:
1.首先知道TabPanel的header的JS属性:
$find('TabContainer1').get_tabs()[0]._header.innerHTML
或者$find(tabpanel1)._header.innerHTML;
2.然后再通过后台代码传入经过逻辑处理后的HeaderName作为JS参数,最后调用JS函数通过前台执行HeaderText赋值.
查了很多资料才找到解决方案,有需要的感谢我吧~~