简体   繁体   中英

Add Ajax CalendarExtender to a dynamic textBox in ASP.NET C#

Is there a way to add Ajax CalendarExtender to a dynamic ASP.NET textbox control? Basically I'm trying to do the following:

    protected void Page_Load(object sender, EventArgs e)
    {
        database.DB myDB = new database.DB();
        DataTable myVars = new DataTable();

        string myTopicID = (string)Session["myTopicID"];
        bool myInvite = (bool)Session["myInvite"];
        bool mySig = (bool)Session["mySig"];
        string myLogo = (string)Session["myLogo"];
        string myImage = (string)Session["myImage"];
        string myLanguage = (string)Session["myLanguage"];

        myVars = myDB.getVarFields(myTopicID, myLanguage);

        AjaxControlToolkit.CalendarExtender calenderDate = new AjaxControlToolkit.CalendarExtender();

        for (int i = 0; i < myVars.Rows.Count; i++)
        {
            Label label = new Label();
            TextBox text = new TextBox();
            label.Text = Convert.ToString(myVars.Rows[i]["varName"]);
            myPlaceHolder.Controls.Add(label);

            text.ID = Convert.ToString(myVars.Rows[i]["varName"]);

            myPlaceHolder.Controls.Add(new LiteralControl("&nbsp;"));

            myPlaceHolder.Controls.Add(text);

            if (Convert.ToString(myVars.Rows[i]["varName"]).Contains("Date:"))
            {
                calenderDate.TargetControlID = "ContentPlaceHolder1_" + text.ID; 
                myPlaceHolder.Controls.Add(calenderDate);
            }

            myPlaceHolder.Controls.Add(new LiteralControl("<br />"));
        }
    }

The error I get when I run the code above is the following:

The TargetControlID of '' is not valid. A control with ID 'ContentPlaceHolder1_Date:' could not be found. 

Which makes sense I suppose since the actual text box does not exist yet. But is there a way around this?

I think ASP.NET will be smart enough to handle it if you just use text.ID, you shouldn't need to add the ContentPlaceHolder1_ prefix.

If that doesn't work, you can use the TextBox' ClientIdMode property to set it to static, then text.ID will definitely work.

The following code worked locally for me:

AjaxControlToolkit.CalendarExtender calenderDate = new AjaxControlToolkit.CalendarExtender();

            for (int i = 0; i < 2; i++)
            {
                Label label = new Label();
                TextBox text = new TextBox();
                label.Text = Convert.ToString("varName");
                ph1.Controls.Add(label);

                text.ID = "myId" + i;

                ph1.Controls.Add(new LiteralControl("&nbsp;"));

                ph1.Controls.Add(text);


                calenderDate.TargetControlID = text.ID;
                ph1.Controls.Add(calenderDate);


                ph1.Controls.Add(new LiteralControl("<br />"));
            }

Only differences I think you may want to investigate: I'm using latest ControlToolkit from Nuget, I'm using a ToolkitScriptManager instead of default ScriptManager. One thing that may be important to you is making sure you make text.ID unique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM