locked
Dynamic Gridview CalendarExtender is not working RRS feed

  • Question

  • User-106287432 posted

    Hello! 
    I have DropdownList and after user select from it,  it generates Gridview from db ( the db table depends on dropdownlist selectedindex). I want to add CalendarExtender to dynamic cell( in c# code without html) which has "start" as a column name. 

    if (DataTable.Columns[i].ToString() == "start")
                        {
                            
                            TxtBox.ID = DataTable.Columns[i].ToString();
                            TxtBox.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                            DBTableGrid.Rows[0].Cells[i].Controls.Add(TxtBox);
    
                            
                            CalendarExtender1 = new CalendarExtender();
                            CalendarExtender1.TargetControlID = TxtBox.ID;
                            CalendarExtender1.ID = "CalendarExtender1"+i;
                            CalendarExtender1.Format = "dd/MM/yyyy";
                           
                            DBTableGrid.Rows[0].Cells[i].Controls.Add(CalendarExtender1);
    
                            
    
    
                        }

    But when i try to add new row nothing happened. 
    What can i do? Thank you for your time.

    Monday, March 1, 2021 11:23 AM

Answers

  • User1535942433 posted

    Hi HoneyMG,

    "Multiple controls with the same ID 'ProjectId' were found. FindControl requires that controls have unique IDs.",

    According to your description,You could check if you have add  twice of control's id 'ProjectId'.After you run the project,you could select 'ProjectId'.

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2021 8:27 AM

All replies

  • User1535942433 posted

    Hi HoneyMG,

    According to your description and codes,it's well.Do you have debug and find errors? 

    I suggest you could post  your full codes to us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    Tuesday, March 2, 2021 7:06 AM
  • User-106287432 posted
     private void CreateTextBoxes(bool LeaveEmptyRow, bool WriteTextToTxtBoxes)
            {
                List<GridViewRow> SelectedRow = new List<GridViewRow>();
    
                try
                {
                    //Checks to see if the update flag has been set
                    if (WriteTextToTxtBoxes)
                        SelectedRow = DecideAction(); //Loads the data from the row
    
                    //DataBind must happen after the call to DecideAction() so that the selected rows are not lost
                    DBTableGrid.DataBind();
    
                    //Add a textbox in each column
                    for (int i = 0; i < DataTable.Columns.Count; i++)
                    {
                        TextBox TxtBox = new TextBox();
                        DropDownList DropDownList1 = new DropDownList();
                        CalendarExtender CalendarExtender1;
    
                        if (WriteTextToTxtBoxes)
                        {
                            if (DataTable.Columns[i].ToString().ToLower().Contains("id"))
                            {
                                if (SelectedRow[0].Cells[i + 1].Text.ToLower().Contains("("))
                                {
                                    string[] ParsedText = SelectedRow[0].Cells[i + 1].Text.Split('(');
                                    SelectedRow[0].Cells[i + 1].Text = ParsedText[0].Trim();
                                }
                            }
                            TxtBox.Text = SelectedRow[0].Cells[i + 1].Text; //Sets the text from the Row's cells to the textboxes
    
                            if (SelectedRow[0].Cells[i + 1].Text == "&nbsp;")
                            {
                                TxtBox.Text = SelectedRow[0].Cells[i + 1].Text.Replace("&nbsp;", "");
                            }
                        }
    
                        if (i == 0)
                            TxtBox.ReadOnly = true;
    
    
                        TxtBox.ToolTip = ColumnTypes[i].Name;
                        TxtBox.Attributes.Add("class", "form-control");
                        TxtBox.ID = DataTable.Columns[i].ToString();
    
                        //.Cells[i + 1] to not add a textbox to the checkbox column
                        DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(TxtBox);
    
                        snackbar.InnerText += DataTable.Columns[i].ToString() + "}}";
                    
                        //DropDownList for Bool Values 
                        if (ColumnTypes[i].ToString().Contains("System.Boolean"))
                        {
                            TxtBox.Visible = false;
                            DropDownList1.Items.Add("True");
                            DropDownList1.Items.Add("False");
                            DropDownList1.DataBind();
                            DropDownList1.Attributes.Add("class", "form-control");
                            DropDownList1.ID = DataTable.Columns[i].ToString();
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(DropDownList1);
                            TxtBox.Text = DropDownList1.SelectedValue;
    
    
                        }
                         if (DataTable.Columns[i].ToString() == "start_date_time")
                        {
                            TxtBox.ID = DataTable.Columns[i].ToString();
                            TxtBox.ClientIDMode = ClientIDMode.Static;
                            
                            CalendarExtender1 = new CalendarExtender();
                            CalendarExtender1.TargetControlID = TxtBox.ID;
                            CalendarExtender1.ID = "CalendarExtender1"+i;
                            CalendarExtender1.Format = "dd/MM/yyyy";
    
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(TxtBox);
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.Add(CalendarExtender1);
    
                         
    
                        }
                   
    
                    }
                        Save.Visible = true;
                        Cancel.Visible = true;
                    
                    }
                catch (Exception ex)
                {
                    Logger.GetLogger().Error(String.Format("   {0}: {1}", ex.GetType().Name, ex.Message), CookieList);
                    throw;
                }
            }
    <asp:ScriptManagerProxy ID="ScriptManagerProxy" runat="server"></asp:ScriptManagerProxy>
                <asp:GridView ID="DBTableGrid" runat="server" CssClass="GridDataStyle" HeaderStyle-CssClass="GridStyleHeader" RowStyle-CssClass="GridStyleRows" PagerStyle-CssClass="GridStylePager" PageSize="10" AllowPaging="true" OnPageIndexChanging="Grid_Projects" ShowHeaderWhenEmpty="True">
                      
                     <Columns>
                            <asp:TemplateField ItemStyle-Width="120px">
                                <HeaderTemplate>
                                    <asp:CheckBox ID="SelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="SelectAll_CheckedChanged" />
                                    All
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="itmSelect" runat="server" AutoPostBack="true" OnCheckedChanged="itmSelect_CheckedChanged"></asp:CheckBox>
                                </ItemTemplate> 
                               
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

    I think the problem is with that "ScriptManagerProxy" tag but i can`t add ScriptManager because it is used in the MasterPage.

    Tuesday, March 2, 2021 7:23 AM
  • User475983607 posted

    Why are you adding controls in C# when the main purpose of a GridView is to render controls dynamically?  

    Tuesday, March 2, 2021 11:40 AM
  • User-106287432 posted

    Why are you adding controls in C# when the main purpose of a GridView is to render controls dynamically?  

    I didn`t write the code i just have to modify it (add CalendarExtender to date fields).

    Tuesday, March 2, 2021 11:48 AM
  • User1535942433 posted

    Hi HoneyMG,

    Since your codes are not full,I couldn't reprocude your codes.But I have tested and I have many problems.

    As far as I think,you need to check what's your errors.I think,you're wrong at the lines of adding controls.You could refer to below article:

    https://www.codeproject.com/Questions/451204/how-to-set-TargetControlID-ajax-calendar-extender

    Best regards,

    Yijing Sun

    Wednesday, March 3, 2021 6:13 AM
  • User-106287432 posted

    Hi HoneyMG,

    Since your codes are not full,I couldn't reprocude your codes.But I have tested and I have many problems.

    As far as I think,you need to check what's your errors.I think,you're wrong at the lines of adding controls.You could refer to below article:

    https://www.codeproject.com/Questions/451204/how-to-set-TargetControlID-ajax-calendar-extender

    Best regards,

    Yijing Sun

    Hey!
    I change my code a little bit but i still get exception when i try to add CalendarExtender to the Controls:

     Exception "System.Web.HttpUnhandledException"	

    My code: 

    if (DataTable.Columns[i].ToString() == "start_date_time")
                        {
                            TxtBox.ID = DataTable.Columns[i].ToString();
                            TxtBox.ClientIDMode = ClientIDMode.Static;
                            
                            CalendarExtender1.ID = "CalendarExtender1";
                            CalendarExtender1.TargetControlID = TxtBox.ID;
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.AddAt(0,TxtBox);
                            DBTableGrid.Rows[0].Cells[i + 1].Controls.AddAt(1, CalendarExtender1);
    
                       
                           
                        }

    I tried to add DropDownList in place of calendarExender and it is showing.

    Thursday, March 4, 2021 12:00 PM
  • User1535942433 posted

    Hi HoneyMG,

    According to your description,I think you problem is iis.

    Have you deploy your site on server, if yes then go to c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files and delete your site folder. restart IIS. check the application.

    Best regards,

    Yijing Sun

    Friday, March 5, 2021 8:14 AM
  • User-106287432 posted

    Hi HoneyMG,

    According to your description,I think you problem is iis.

    Have you deploy your site on server, if yes then go to c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files and delete your site folder. restart IIS. check the application.

    Best regards,

    Yijing Sun

    Hey, thank you, but it is still showing the same exception.

    Friday, March 5, 2021 3:28 PM
  • User1535942433 posted

    Hi HoneyMG,

    Do you have more details of  error "System.Web.HttpUnhandledException"?

    I suggest you could put your all code in try catch block you will find out the way to error.

    Best regards,

    Yijing Sun

    Tuesday, March 9, 2021 7:58 AM
  • User-106287432 posted

    Hi HoneyMG,

    Do you have more details of  error "System.Web.HttpUnhandledException"?

    I suggest you could put your all code in try catch block you will find out the way to error.

    Best regards,

    Yijing Sun

    Hey, Yijing Sun!
    Sorry for late reply i just saw your answer.
    When i put the code in try catch block i didn`t get any error message. 

    Details of the error:

    {
      "name": "Microsoft.ApplicationInsights.Dev.Exception",
      "time": "2021-03-17T14:11:10.8931583Z",
      "tags": {
        "ai.internal.sdkVersion": "web:2.4.1-1362",
        "ai.operation.id": "fUg4IoI5lqc=",
        "ai.location.ip": "::1",
        "ai.cloud.roleInstance": "ALLSAT-PC01",
        "ai.operation.name": "POST /Pages/Projects",
        "ai.operation.parentId": "|fUg4IoI5lqc=.f1891716_"
      },
      "data": {
        "baseType": "ExceptionData",
        "baseData": {
          "ver": 2,
          "properties": {
            "DeveloperMode": "true"
          },
          "exceptions": [
            {
              "id": 23186785,
              "typeName": "System.Web.HttpUnhandledException",
              "message": "Exception of type 'System.Web.HttpUnhandledException' was thrown.",
              "hasFullStack": true,
              "parsedStack": [
                {
                  "level": 0,
                  "method": "System.Web.UI.Page.HandleError",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 1,
                  "method": "System.Web.UI.Page.ProcessRequestMain",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 2,
                  "method": "System.Web.UI.Page.ProcessRequest",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 3,
                  "method": "System.Web.UI.Page.ProcessRequest",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 4,
                  "method": "System.Web.UI.Page.ProcessRequestWithNoAssert",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 5,
                  "method": "System.Web.UI.Page.ProcessRequest",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 6,
                  "method": "ASP.pages_projects_aspx.ProcessRequest",
                  "assembly": "App_Web_eoryladu, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
                },
                {
                  "level": 7,
                  "method": "System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 8,
                  "method": "System.Web.HttpApplication.ExecuteStepImpl",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 9,
                  "method": "System.Web.HttpApplication.ExecuteStep",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                }
              ]
            },
            {
              "id": 42798727,
              "outerId": 23186785,
              "typeName": "System.Web.HttpException",
              "message": "Multiple controls with the same ID 'ProjectId' were found. FindControl requires that controls have unique IDs.",
              "hasFullStack": true,
              "parsedStack": [
                {
                  "level": 0,
                  "method": "System.Web.UI.Control.FillNamedControlsTable",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 1,
                  "method": "System.Web.UI.Control.FillNamedControlsTable",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 2,
                  "method": "System.Web.UI.Control.FindControl",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 3,
                  "method": "System.Web.UI.Control.FindControl",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 4,
                  "method": "AjaxControlToolkit.ExtenderControlBase.FindControlHelper",
                  "assembly": "AjaxControlToolkit, Version=17.1.1.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
                },
                {
                  "level": 5,
                  "method": "AjaxControlToolkit.ExtenderControlBase.FindControl",
                  "assembly": "AjaxControlToolkit, Version=17.1.1.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
                },
                {
                  "level": 6,
                  "method": "System.Web.UI.ExtenderControl.RegisterWithScriptManager",
                  "assembly": "System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                },
                {
                  "level": 7,
                  "method": "AjaxControlToolkit.ExtenderControlBase.OnPreRender",
                  "assembly": "AjaxControlToolkit, Version=17.1.1.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
                },
                {
                  "level": 8,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 9,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 10,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 11,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 12,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 13,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 14,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 15,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 16,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 17,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 18,
                  "method": "System.Web.UI.Control.PreRenderRecursiveInternal",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                },
                {
                  "level": 19,
                  "method": "System.Web.UI.Page.ProcessRequestMain",
                  "assembly": "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
                }
              ]
            }
          ]
        }
      }
    }

    Here i recieve message: Multiple controls with the same ID 'ProjectId' were found. FindControl requires that controls have unique IDs.", but this control is actrually the ID of other textbox in the selected row in gridview which is used only one time. 

    In debug i recieve that exception here: 

      protected void NewForm_Click(object sender, EventArgs e)
            {
                LoadTableWithData(GetClassType(), true);
                ShowForm.Visible = false;
            }

    before closing }.

    Thank you for your reply.

    Wednesday, March 17, 2021 2:22 PM
  • User1535942433 posted

    Hi HoneyMG,

    "Multiple controls with the same ID 'ProjectId' were found. FindControl requires that controls have unique IDs.",

    According to your description,You could check if you have add  twice of control's id 'ProjectId'.After you run the project,you could select 'ProjectId'.

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2021 8:27 AM
  • User-106287432 posted

    yij sun

    Hi HoneyMG,

    HoneyMG

    "Multiple controls with the same ID 'ProjectId' were found. FindControl requires that controls have unique IDs.",

    According to your description,You could check if you have add  twice of control's id 'ProjectId'.After you run the project,you could select 'ProjectId'.

    Best regards,

    Yijing Sun

    Thank you! 
    The problem was that there were duplication with ID  DropDownList and Textboxes with the same id, but the Textbox was not visible, that is why it didn`t throw exception before, 


    Friday, March 19, 2021 12:16 PM