在处理遗留的ASP.NET WebForms应用程序时,我偶然发现了以下错误:"指定的参数超出了有效值的范围。参数名称索引"。我有一个带有gridview的Tabcontainer,其中包含用户全名和2个ButtonField(编辑和查看)。当我单击View with RowCommand时,将生成另外5个选项卡,其中包含来自AD的所有用户信息。一切正常,直到我回到用户选项卡并单击另一个用户名。如何让我的页面删除为第一个用户创建的新5个标签,并为新用户添加5个新标签?谢谢
<ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajax:ToolkitScriptManager>
<ajax:TabContainer ID="TabContainer1" OnInit="TabContainer1_Init" runat="server" ActiveTabIndex = "0" ScrollBars="Vertical">
<ajax:TabPanel runat="server" HeaderText="Users" ID="TabPanel1">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width= "850px" Height = "650px" onrowcommand = "GridView1_RowCommand" >
<Columns>
<asp:BoundField DataField="name" HeaderText="Name"/>
<asp:BoundField DataField="SamAccountName" HeaderText="Username" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" />
<asp:ButtonField CommandName = "View" ImageUrl ="~/Resources/Images/view.gif" ButtonType = "Image"/>
<asp:ButtonField CommandName = "Edit" ImageUrl ="~/Resources/Images/edit.gif" ButtonType = "Image"/>
</Columns>
</asp:GridView>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TabContainer1" EventName="ActiveTabChanged" />
</Triggers>
</ContentTemplate>
</ajax:TabPanel>
</ajax:TabContainer>
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{
mydata();
}
}
public void mydata()
{
t = Regex.Replace(ldap, @"CN=[^,]*,* *", "", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
try
{
List<string> allUsers = new List<string>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "IDEA.COM", t);
UserPrincipal usr = new UserPrincipal(ctx);
// Create a PrincipalSearcher object.
PrincipalSearcher ps = new PrincipalSearcher(usr);
PrincipalSearchResult<Principal> fr = ps.FindAll();
GridView1.DataSource = fr;
GridView1.DataBind();
}
catch (Exception ex)
{
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int row = int.Parse(e.CommandArgument.ToString());
item = GridView1.Rows[row].Cells[1].Text;
var username = GridView1.Rows[row].Cells[0].Text;
string firstname = username.Remove(username.IndexOf("."));
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://idea.com";
DirectorySearcher objADSearcher = new DirectorySearcher(de);
de.AuthenticationType = AuthenticationTypes.Secure;
objADSearcher.SearchRoot = de;
objADSearcher.Filter = "(SAMAccountName=" + item + ")";
SearchResult results = objADSearcher.FindOne();
if (e.CommandName == "View")
{ if (results.ToString() != "") {int flags = Convert.ToInt32(results.Properties["userAccountControl"][0].ToString());
if (Convert.ToBoolean(flags & 0x0002)){Response.Write("<script> alert ('" + "Account Disabled for more then 90 days" + "') </script>");
else
{WindowsIdentity wi = new WindowsIdentity(item);
foreach (IdentityReference group in wi.Groups)
{
try
{
result1.Add(group.Translate(typeof(NTAccount)).ToString());
result1.Sort();
}
catch (Exception ex) { }
}
string tabid;
AjaxControlToolkit.TabPanel apptab = new AjaxControlToolkit.TabPanel();
apptab.HeaderText = firstname + " " + "Application";
apptab.ID = string.Format("Tab{0}{1}",suffix, "tab1");
AjaxControlToolkit.TabPanel sharedtab = new AjaxControlToolkit.TabPanel();
sharedtab.HeaderText = firstname + " " + "SharedCommon"; sharedtab.ID = string .Format ("Tab{0}{1}",suffix ,"tab2" );AjaxControlToolkit.TabPanel grouptab = new AjaxControlToolkit.TabPanel();grouptab.HeaderText = firstname + " " + "Internet/RAS";
grouptab.ID = string.Format("Tab{0}{1}",suffix, "tab3");
AjaxControlToolkit.TabPanel idtab = new AjaxControlToolkit.TabPanel();idtab.HeaderText = firstname + " " + "Info";
idtab.ID = string.Format("Tab{0}{1}",suffix, "tab4");
TabContainer1.Tabs.Add(grouptab);
TabContainer1.Tabs.Add(apptab);
TabContainer1.Tabs.Add(sharedtab);
TabContainer1.Tabs.Add(idtab);}}}