2013-02-08 182 views
1

我需要關於我的代碼的指導。按鈕單擊事件不會觸發併發生錯誤

public class FacultyTemplatedynamic:ITemplate 
{ 
    ListItemType ItemType; 
    string FieldName; 
    string InfoType; 
    private bool isEditMode = false; 
    protected bool isInEditMode 
    { 
     get { return this.isEditMode; } 
     set { this.isEditMode = value; } 
    } 
    public FacultyTemplatedynamic(ListItemType itemType, string field_name, string info_type) 
    { 
     FieldName = field_name; 
     ItemType = itemType; 
     InfoType = info_type; 
    } 

    public void InstantiateIn(Control container) 
    { 
     switch (ItemType) 
     { 
      case ListItemType.Header: 
       Literal header_litrl = new Literal(); 
       header_litrl.Text = "<b>" + FieldName + "</b>"; 
       container.Controls.Add(header_litrl); 
       break; 
      case ListItemType.Item: 
       switch (InfoType) 
       { 
        case "Command": 
         ImageButton edit_button = new ImageButton(); 
         edit_button.ID = "edit_button"; 
         edit_button.ImageUrl = "~/Images/edit.gif"; 
         edit_button.CommandName = "Edit"; 
         edit_button.ToolTip = "Edit"; 
         container.Controls.Add(edit_button); 
         break; 
        default: 
         Label field_lbl = new Label(); 
         field_lbl.ID = "labelName"; 
         //field_lbl.Visible =!(bool) isInEditMode; 
         field_lbl.Text = String.Empty; //we will bind it later through 'OnDataBinding' event 
         field_lbl.DataBinding += new EventHandler(OnDataBinding); 
         container.Controls.Add(field_lbl); 
         break; 
       } 
       break; 
      case ListItemType.EditItem: 
       if (InfoType == "Command") 
       { 
        ImageButton update_button = new ImageButton(); 
        update_button.ID = "update_button"; 
        update_button.CommandName = "Update"; 
        update_button.ImageUrl = "~/Images/update.gif"; 

        update_button.ToolTip = "Update"; 
        update_button.OnClientClick = "return confirm('Are you sure to update the record?')"; 
        container.Controls.Add(update_button); 

        ImageButton cancel_button = new ImageButton(); 
        cancel_button.ImageUrl = "~/Images/cancel.gif"; 
        cancel_button.ID = "cancel_button"; 
        cancel_button.CommandName = "Cancel"; 
        cancel_button.ToolTip = "Cancel"; 
        container.Controls.Add(cancel_button); 

       } 
       else// for other 'non-command' i.e. the key and non key fields, bind textboxes with corresponding field values 
       { 
        Label field_lbl1 = new Label(); 
        field_lbl1.ID = "LabelEdit"; 
        //field_lbl1.Visible = (bool)isInEditMode; 
        field_lbl1.Text = String.Empty; //we will bind it later through 'OnDataBinding' event 
        field_lbl1.DataBinding += new EventHandler(OnDataBinding); 
        container.Controls.Add(field_lbl1); 
       } 
       break; 
     } 
    } 

    private void OnDataBinding(object sender, EventArgs e) 
    { 
     object bound_value_obj = null; 
     Control ctrl = (Control)sender; 
     IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer; 
     bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName); 

     switch (ItemType) 
     { 
      case ListItemType.Item: 
       Label field_ltrl = (Label)sender; 
       field_ltrl.Text = bound_value_obj.ToString(); 

       break; 
      case ListItemType.EditItem: 
       TextBox field_txtbox = (TextBox)sender; 
       field_txtbox.Text = bound_value_obj.ToString(); 

       break; 
     } 
    } 
} 




private void CreateTemplatedGridView() 
     { 
      PopulateTable(); 
      for (int i = 0; i < dt.Columns.Count; i++) 
      { 
       TemplateField tf = new TemplateField(); 
       tf.HeaderTemplate = new FacultyTemplatedynamic(ListItemType.Header, dt.Columns[i].ColumnName, dt.Columns[i].DataType.Name); 
       tf.ItemTemplate = new FacultyTemplatedynamic(ListItemType.Item, dt.Columns[i].ColumnName, dt.Columns[i].DataType.Name); 
       tf.EditItemTemplate = new FacultyTemplatedynamic(ListItemType.EditItem, dt.Columns[i].ColumnName, dt.Columns[i].DataType.Name); 
       GrdMarkAttendance.Columns.Add(tf); 
      } 
      TemplateField tf1 = new TemplateField(); 
      GrdMarkAttendance.Columns.Add(tf1); 
      GrdMarkAttendance.DataSource = dt; 
      GrdMarkAttendance.DataBind(); 
     } 

    private void PopulateTable() 
     { 
      dt = new DataTable(); 
      dt = obj.GetRegister(); 
     }  

    protected void GrdMarkAttendance_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      PopulateTable(); 
      int i = dt.Columns.Count;   
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 

       DropDownList ddl = new DropDownList(); 
       ddl.ID = "ddl"; 
       ddl.Items.Insert(0, "Present"); 
       ddl.Items.Insert(1, "Absent"); 
       ddl.Items.Insert(2, "Leave");    
       e.Row.Cells[i].Controls.Add(ddl); 
      } 
     }  

    protected void Button1_Click(object sender, EventArgs e) 
     { 

     } 

當我運行該頁面並點擊按鈕,我得到了以下錯誤

服務器錯誤「/」應用。

找到具有相同ID'labelName'的多個控件。 FindControl要求控件具有唯一的ID。

描述:執行當前Web *請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及* *源自代碼的位置。 *

異常詳細信息:System.Web.HttpException:用相同的ID'標籤'找到了多個控制。 ** FindControl ** 要求控件具有唯一的ID。

* 源錯誤:*在當前web請求的執行過程中生成

未處理的異常。 *有關異常來源和位置的信息可以使用* 以下的異常堆棧跟蹤進行標識。

* 堆棧跟蹤:*

*[HttpException (0x80004005): Multiple controls with the same ID 'labelName' were found.* *FindControl requires that controls have unique IDs.]* 
    *System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection* *controls) +265* 
    *System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection* * controls)* +311 
    *System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +145* 
    *System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +365* 
    *System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +365* 
    *System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +365* 
    *System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +365* 
    *System.Web.UI.Page.FindControl(String id) +38* 
    *System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)* +287 
    *System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean* *includeStagesAfterAsyncPoint) +4911* 
  • 我不知道爲什麼這個錯誤就要昨天是wroking罰款,但是當我使用 * FindControl已在行的特殊細胞它開始給我錯誤,但現在我刪除*該代碼和點擊按鈕它顯示錯誤。* 請給你的* **建議,以便如果我格式錯誤後的問題,我還可以移動

對不起或提供足夠的數據

+0

在你每一個控制web應用應該有一個唯一的ID相同id..Find兩個或更多的控制。 – coder

+0

你在哪裏使用ID爲'labelName'的標籤,你可以向我們展示gridview設計代碼.. –

+0

@AmolKolekar請檢查頂部的代碼現在我動態創建模板字段並將它們添加到gridview編程 – Ranjan

回答

1

請閱讀堆棧跟蹤!

第一行:「用相同的ID‘標籤’多個控件中發現」

檢查您查看多個控件使用相同的ID。

編輯:

嘗試按Ctrl + F,搜索標籤。如果你在同一文件中找到2 ..你找到了你的問題...

+0

感謝您的關注和建議,但是我在GridView中動態創建的模板字段中使用了這個'labelName'標籤。參考請參閱createTemplatedgridview()方法。這個labelName我用那裏,但現在我試圖讀取row.cells控制。但未點擊按鈕點擊事件。我把斷點,但這也不起作用。任何建議 – Ranjan

+0

請看看代碼 – Ranjan

+0

我沒有在任何地方使用Find Control。早些時候我用過,但刪除後,如果您不使用查找控制,我得到上述錯誤 – Ranjan

0

在您的網頁存在與控制,並給它不同的ID

+0

我同意你,但請閱讀我的問題... – Ranjan