【问题标题】:Button click event not firing and giving error按钮单击事件未触发并给出错误
【发布时间】:2013-02-11 01:44:28
【问题描述】:

我需要有关我的代码的指导。

 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 期间发生未处理的异常 *request。请查看堆栈跟踪以获取有关错误的更多信息以及它* *起源于代码中的位置。 *

异常详情:System.Web.HttpException:多个具有相同 ID 的控件 'labelName' 被发现。 ** 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*  
  • 我不知道为什么昨天会出现这个错误,但当我使用时它工作正常 * FindControl in Row 的特定单元格 它开始给我错误,但现在我删除 * 该代码并在单击按钮时显示错误。* 请给你的* **建议,让我可以更进一步

如果我以错误的格式发布问题或提供的数据不足,请见谅

【问题讨论】:

  • Web 应用程序中的每个控件都应该有一个唯一的 ID。
  • 你在哪里使用了 id 为'labelName'的标签,你能告诉我们gridview设计代码吗..
  • @AmolKolekar 请检查顶部的代码现在我动态创建了模板字段并以编程方式将它们添加到gridview
  • 方法CreateTemplatedGridView()在何时何地被调用?
  • 页面加载事件中的@Mt.Schneiders

标签: c# asp.net


【解决方案1】:

请阅读堆栈跟踪!

第一行:“找到多个具有相同 ID 'labelName' 的控件”

检查视图中是否有多个具有相同 ID 的控件。

编辑:

尝试 Ctrl + F 并搜索 labelName.. 如果您在同一个文档中找到 2.. 您找到了问题...

【讨论】:

  • 感谢您的关注和建议,但我在 gridview 中动态创建的模板字段中使用了这个“labelName”标签。有关参考,请参阅 tf createTemplatedgridview() 方法。我在那里使用的这个 labelName 但现在我试图读取 row.cells 控件。但未触发按钮单击事件。我放了断点,但这也不起作用。任何建议
  • 我没有在任何地方使用 Find Control。早些时候我使用过,但在删除后我得到了上述错误
  • 如果您不使用查找控件。你为什么不尝试制作一个没有 ID 的标签。问题必须与添加标签部分有关。你真的确定你的页面上没有标签或其他具有该名称的对象吗? (而且你也在后面的代码中制作它)你可以发布 aspx 页面吗?
【解决方案2】:

在您的页面中有两个或多个具有相同 id 的控件..找到该控件并为其指定不同的 id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-21
    • 2013-01-17
    • 2012-03-14
    • 2011-12-02
    • 2013-10-14
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多