我正在使用Ajax选项卡控件,其中包含每个选项卡中的网格。网格有下拉列表和按钮,我想在按钮点击gridview Row时触发gridview的RowCommand事件。但问题是每当我点击按钮时,会触发Tabcontainet_ActiveTabChanged事件,并且在触发RowCommand事件之前再次绑定网格视图。
我无法理解为什么即使我没有故意触发此事件也会自动触发。在这种情况下如何触发RowCommand事件? 我尝试了更新面板和没有更新面板。
答案 0 :(得分:3)
这很奇怪但是如果活动标签发生变化或者实际ActiveTabIndex属性值等于0,TabContainer会在每次回发时触发ActiveTabChanged事件。我无法找到任何这种行为的原因,因此请使用下面的解决方案来承担风险。实际上有两种解决方案可用:第一种是下载AjaxControlToolkit源代码,更改TabContainer控件的LoadPostData方法并使用自定义dll:
实际方法:
protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
int tabIndex = ActiveTabIndex;
bool result = base.LoadPostData(postDataKey, postCollection);
if (ActiveTabIndex == 0 || tabIndex != ActiveTabIndex)
{
return true;
}
return result;
}
只需从上面的代码中删除ActiveTabIndex == 0
条件。
或者您可以创建自己继承自TabContainer的类,重写该方法并使用此类而不是默认类:
namespace AjaxControlToolkit
{
/// <summary>
/// Summary description for MyTabContainer
/// </summary>
public class MyTabContainer : TabContainer
{
protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
int tabIndex = ActiveTabIndex;
if (SupportsClientState)
{
string clientState = postCollection[ClientStateFieldID];
if (!string.IsNullOrEmpty(clientState))
{
LoadClientState(clientState);
}
}
if (tabIndex != ActiveTabIndex)
{
return true;
}
return false;
}
}
}
答案 1 :(得分:0)
对于任何使用VB.NET的人:
Namespace AjaxControlToolkit
Public Class MyTabContainer
Inherits TabContainer
Protected Overrides Function LoadPostData(postDataKey As String, postCollection As NameValueCollection) As Boolean
Dim tabIndex = ActiveTabIndex
If SupportsClientState Then
Dim clientState = postCollection(ClientStateFieldID)
If Not String.IsNullOrEmpty(clientState) Then
LoadClientState(clientState)
End If
End If
If tabIndex <> ActiveTabIndex Then
Return True
End If
Return False
End Function
End Class
结束命名空间
Yuriy Rozhovetskiy的道具
答案 2 :(得分:0)
我也无缘无故地激活了ActiveTabChanged事件。
我用下面修好了;把它作为ActiveTabChanged事件的第一行。改变&#34; tabMain&#34;使用您的实际标签ID:
Dim ctrl As Control = Nothing
'get the event target name and find the control
Dim ctrlName As String = Page.Request.Params.Get("__EVENTTARGET")
If (Not String.IsNullOrEmpty(ctrlName)) Then
ctrl = Page.FindControl(ctrlName)
If ctrl IsNot Nothing Then
If ctrl.ID <> "tabMain" Then
Exit Sub
End If
End If
Else
Exit Sub
End If
如果事件来自除tabMain之外的任何地方(在上面的示例中),请离开。
这为我解决了几个小时的挫败感!