2012-03-06 43 views
0

名單(S)當我點擊我的刪除按鈕是我的意圖,以卸下面板,然後通過其餘面板迭代,並給他們從頭開始即新的ID如果我從6個面板的列表中刪除面板(3),我將遍歷並給他們所有新的面板面板0-5。重複面板/ ID列表後重建我的控制

我的問題是,我一直運行到後刪除我的面板在那裏我有重複的ID名稱錯誤。對於我的生活,我不能看到我出錯的地方,所以我希望我只是盲目或者建議。

我在我的btnDelete功能設置斷點,並通過它的一步,但我沒有看到我的邏輯缺陷的,爲什麼我遇到的問題具有相同ID的多個面板。

ASPX:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" EnableTheming="true" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
<link href="App_Themes/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" 
    type="text/css" /> 
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" 
    rel="stylesheet" type="text/css" /> 
<script src="Includes/JavaScript/jquery-1.7.1.js" type="text/javascript"></script> 
<script src="Includes/JavaScript/jquery.ui.datepicker.js" type="text/javascript"></script> 
<script src="Includes/JavaScript/jquery.ui.widget.js" type="text/javascript"></script> 
<script src="Includes/JavaScript/jquery.ui.core.js" type="text/javascript"></script> 
<%--Script for the Dropdown Datepicker--%> 
<script type="text/javascript"> 
    $(function() { 
     $("input.datepicker").datepicker({ showOn: 'button', buttonImage: 'Includes/Images/calender.gif', buttonImageOnly: false, onSelect: function() { } 
     }); 
    }); 
</script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server"> 
<h1> 
    Search 
    <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="Includes/Images/action_help.gif" 
     NavigateUrl="~/user_manual.pdf" Target="_blank" ToolTip="Search Help"></asp:HyperLink></h1> 
<table border="0" cellpadding="6" cellspacing="0"> 
<tr> 
<td> 
    <asp:Button ID="btnAdd" runat="server" Text="Add Control" onclick="btnAdd_Click" /> 
    <asp:Button ID="btnClear" runat="server" Text="Reset" onclick="btnClear_Click" /> 
    <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /> 

</td> 
<td> 
    <asp:TextBox ID="txtTitle" class="searchPage" runat="server"></asp:TextBox> 
</td> 
</tr> 
<tr> 
<td> 
<asp:PlaceHolder ID="myPlaceholder" runat="server"></asp:PlaceHolder> 
</td> 

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Runtime.Serialization; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 

public partial class Search : BasePage 
{ 
    List<Users> userroles = new List<Users>(); 
    //Panel that contains all our Dynamically added user controls. 
    List<Panel> persistControls = new List<Panel>(); 

    public int userid = 0; 
    public byte IsActive = 1; 
    public int error = 0; 

protected override void OnInit(EventArgs e) 
{ 
    base.OnInit(e); 
} 

// Calls three functions responsible for pulling from the Database and binding the Datagrid. 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     GetClustersFromDB(userid); 
     BindGrid(); 
     BindState();   
    } 
    else 
    { 
     // Where I am recreating my controls. 
     DynamicQueryRecreateLogic(); 
    } 
} 

protected void DynamicQueryRecreateLogic() 
{ 
    if (Session["persistControls"] != null) 
    { 
     // Local Value that contains the members of the current persistControls session, not yet pushed to the page. 
     persistControls = (List<Panel>)Session["persistControls"]; 
     int count = 0; 

     foreach (Panel panel in persistControls) 
     { 
      //AddQuestionTypeDropDownList(count); 
      panel.ID = "panel" + "(" + count.ToString() + ")"; 

      foreach (Control control in panel.Controls) 
      { 
       if (control.GetType() == typeof(DropDownList)) 
       { 
        control.ID = "list" + "(" + count.ToString() + ")"; 
        DropDownList list = (DropDownList)panel.FindControl(control.ID); 
        list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); 
       } 
       if (control.GetType() == typeof(TextBox)) 
       { 
        control.ID = "txtBox" + "(" + count.ToString() + ")"; 
       } 
      } 

      // Re-Adding our Remove Button 
      Button btnRemove = new Button(); 
      btnRemove.Click += new EventHandler(btnDelete_Click); 
      btnRemove.Text = "Remove"; 
      btnRemove.CommandArgument = count.ToString(); 

      // Pushing to Placeholder 
      myPlaceholder.Controls.Add(panel); 
      myPlaceholder.Controls.Add(btnRemove); 
      count++; 
     } 
    } 
} 

private DropDownList AddQuestionTypeDropDownList() 
{ 
    DropDownList list = new DropDownList(); 

    list.ID = "list" + "(" + persistControls.Count.ToString() + ")"; 
    list.Items.Add(new ListItem("--Select One--", "")); 
    list.Items.Add(new ListItem("Title", "1")); 
    list.Items.Add(new ListItem("Contact", "2")); 
    list.Items.Add(new ListItem("Date Created", "3")); 
    list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); 
    list.AutoPostBack = true; 

    return list; 
} 

private TextBox AddFieldTypeTextBox(int count) 
{ 
    TextBox box = new TextBox(); 
    box.ID = "txtBox" + "(" + count.ToString() + ")"; 
    return box; 
} 

protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     Panel panelContainer = new Panel(); 
     panelContainer.ID = "panel" + "(" + persistControls.Count.ToString() + ")"; 

     panelContainer.Controls.Add(AddQuestionTypeDropDownList()); 

     Button btnRemove = new Button(); 
     btnRemove.Click += new EventHandler(btnDelete_Click); 
     btnRemove.Text = "Remove"; 
     btnRemove.CommandArgument = persistControls.Count.ToString(); 

     persistControls.Add(panelContainer); 

     myPlaceholder.Controls.Add(panelContainer); // Pushes the Panel to the page. 
     myPlaceholder.Controls.Add(btnRemove); // Pushes our Button to the page. 

     Session["persistControls"] = persistControls; // put it in the session 
    } 
    catch 
    { 
     throw; 
    } 
} 

protected static string DecipherCountNumber(string IDHolder) 
{ 
    int start = IDHolder.IndexOf("("); 

    if (start == -1) 
    { 
     return IDHolder; 
    } 
    else 
    { 
     return IDHolder.Substring(start + 1).Replace(")", string.Empty); 
    } 
} 

protected void list_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //I need to fix the dynamic Add location. I need to track some type of enumeration or attributes of the panel for recreation. 
    try 
    { 
     DropDownList list = (DropDownList)sender; 
     string IDHolder = list.ID.ToString(); 
     int count = Convert.ToInt32(DecipherCountNumber(IDHolder)); 
     Panel panelContainer = persistControls.Find(delegate(Panel panel) { return panel.ID == "panel" + "(" + count.ToString() + ")"; }); 

     if (list.SelectedIndex == 1) 
     { 
      //panelContainer.Controls.Add(AddFieldTypeTextBox(count)); 
     } 
     if (list.SelectedIndex == 2) 
     { 
      //panelContainer.Controls.Remove(FindControl("txtBox" + "(" + count.ToString() + ")")); 
     } 
     if (list.SelectedIndex == 3) 
     { 
      //panelContainer.Controls.Remove(FindControl("txtBox" + "(" + count.ToString() + ")")); 
     } 

     Session["persistControls"] = persistControls; 
    } 
    catch 
    { 
     throw; 
    } 
} 

protected void btnClear_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     Session["persistControls"] = null; 
     Response.Redirect(Request.Url.ToString()); 
    } 
    catch 
    { 
     throw; 
    } 
} 

protected void btnDelete_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     int deleteThisOne = int.Parse(((Button)sender).CommandArgument); 
     persistControls.Remove(persistControls[deleteThisOne]); 

     Session["persistControls"] = persistControls; 

     Response.Redirect(Request.Url.ToString(), false); 
    } 
    catch 
    { 
     throw; 
    } 
} 
+0

我不知道你正在試圖解決什麼業務問題,但是這似乎是一個大量的工作,很少的回報。通過閱讀btnDelete_Click,您將從集合中移除面板,將集合保存到會話中,然後重定向到相同的URL(這將始終繞過Form_Load中的IsPostback)。似乎有點複雜... – mgnoonan 2012-03-07 13:38:08

+0

我正在工作的遺留系統。我有一個可以通過非常特定的參數進行過濾的綁定表。原始過濾器本質上是一個面板,根據您從DDL選擇的選項進行填充。 而沒有任何以往的經驗,我還是一個少年的大學,這是概念,我想出迄今。 添加控件將填充新的面板採用了單一的DropDownList,這取決於你從DDL新字段將被添加到該面板中選擇什麼,並最終成爲一個「查詢」,將被髮送到一個存儲過程。 – Intern1990 2012-03-07 14:34:37

+0

我們可以看到ASPX代碼嗎? – mgnoonan 2012-03-07 15:09:48

回答

0

我修改DynamicQueryControls做娛樂的OnInit而不是在pageLoad的。在PageLoad級別創建此問題時出現了一些問題。