简体   繁体   中英

How to manage dynamically added asp.net controls with Control.AddAt in c# when during user navigation a Control could be disabled

My situation is complex, this is a simplified test, but by solving this, maybe I solve my complete problems.

I have a webform with some imagebutton. Some added in frontend, some at runtime in the codebehind.

This is aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ImageTester.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script>
        function ClientCheckForSave(value) {
            alert("ClientCheckForSave");
        }
        function CallFatturazione() {
            alert("CallFatturazione");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div runat="server" id="divImagePrint"><asp:imagebutton id="ImagePrint" runat="server" ToolTip="Stampa (SHIFT+F3)" OnClientClick="javascript:return ClientCheckForSave('ImagePrint')" ImageUrl="Images\stampa.png"></asp:imagebutton></div>
        <div runat="server" id="divImageHelpCon"><asp:imagebutton id="imageHelpCon" runat="server" ToolTip="Help Contestuale (SHIFT+F4)" OnClientClick="javascript:return ClientCheckForSave('ImageHelpCon')" ImageUrl="Images\helpCont.png"></asp:imagebutton></div>
        <div runat="server" id="divImageLibrary"><asp:imagebutton id="imageLibrary" runat="server" ToolTip="Library (SHIFT+F8)" OnClientClick="javascript:return ClientCheckForSave('ImageLibrary')" ImageUrl="Images\library.png"></asp:imagebutton></div>
    </div>
    </form>
</body>
</html>

This is codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace ImageTester
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Panel divImageFatturazione = new Panel();
            divImageFatturazione.ID = "divImageFatturazione";

            HtmlImage btnFatturazione = new HtmlImage();
            btnFatturazione.Src = "Images/fatturazione.png";
            btnFatturazione.Border = 0;
            btnFatturazione.ID = "Fatturazione";
            btnFatturazione.Attributes.Add("onclick", "javascript:return CallFatturazione();");
            btnFatturazione.Attributes.Add("style", "cursor:pointer");
            btnFatturazione.Attributes.Add("alt", "Fatturazione (CTRL+ALT+F)");
            btnFatturazione.Attributes.Add("title", "Fatturazione (CTRL+ALT+F)");

            divImageFatturazione.Controls.Add(btnFatturazione);

            Panel divImageRiepilogo = new Panel();
            divImageRiepilogo.ID = "divImageRiepilogo";

            HtmlImage btnRiepilogo = new HtmlImage();
            btnRiepilogo.Src = "Images/Riep_Dich.png";
            btnRiepilogo.Border = 0;
            btnRiepilogo.ID = "CallRiepilogo";
            btnRiepilogo.Attributes.Add("onclick", "javascript:return CallFatturazione();");
            btnRiepilogo.Attributes.Add("style", "cursor:pointer");
            btnRiepilogo.Attributes.Add("alt", "Cambia applicazione (SHIFT+F9)");
            btnRiepilogo.Attributes.Add("title", "Cambia applicazione (SHIFT+F9)");

            divImageRiepilogo.Controls.Add(btnRiepilogo);


            Control controlA = Page.FindControl("divImagePrint");
            Control parent = controlA.Parent;
            if (!(parent == null && parent.Controls.IndexOf(controlA) >= 0))
            {
                parent.Controls.AddAt(parent.Controls.IndexOf(controlA), divImageFatturazione);
                parent.Controls.AddAt(parent.Controls.IndexOf(controlA), divImageRiepilogo);
            }

            ((ImageButton)Page.FindControl("ImagePrint")).Enabled = false;
        }
    }
}

As you can see:

1) I have same imagebuttons on aspx.

2) I dynamically add some other imagebuttons using, in the codebehind, the method Control.AddAt(positionOfAControl, newControl)

3) At the end of the page_load, it's disabled the control used to retrieve the position in which I insert the new controls.

So, at first load, all seems ok.

It happens that during the user navigation, when the user clicks for example the first control added in codebehind (Fatturazione), the page loaded after callback has the imagebutton imageHelpCon disabled, if the user clicks on the next imagebutton (CallRiepilogo) the page loaded after callback has the imagebutton imageLibrary disabled. Why?

This is a piece of html at first load:

    <div>
        <div id="divImageFatturazione">
    <img src="Images/fatturazione.png" id="Fatturazione" border="0" onclick="javascript:return CallFatturazione();" style="cursor:pointer" alt="Fatturazione (CTRL+ALT+F)" title="Fatturazione (CTRL+ALT+F)" />
</div><div id="divImageRiepilogo">
    <img src="Images/Riep_Dich.png" id="CallRiepilogo" border="0" onclick="javascript:return CallFatturazione();" style="cursor:pointer" alt="Cambia applicazione (SHIFT+F9)" title="Cambia applicazione (SHIFT+F9)" />
</div><div id="divImagePrint"><input type="image" name="ImagePrint" id="ImagePrint" ***disabled="disabled***" title="Stampa (SHIFT+F3)" class="aspNetDisabled" src="Images\stampa.png" /></div>
        <div id="divImageHelpCon"><input type="image" name="imageHelpCon" id="imageHelpCon" title="Help Contestuale (SHIFT+F4)" src="Images\helpCont.png" onclick="javascript:return ClientCheckForSave(&#39;ImageHelpCon&#39;);" /></div>
        <div id="divImageLibrary"><input type="image" name="imageLibrary" id="imageLibrary" title="Library (SHIFT+F8)" src="Images\library.png" onclick="javascript:return ClientCheckForSave(&#39;ImageLibrary&#39;);" /></div>
    </div>

This is after first postback:

    <div>
        <div id="divImageFatturazione">
    <img src="Images/fatturazione.png" id="Fatturazione" border="0" onclick="javascript:return CallFatturazione();" style="cursor:pointer" alt="Fatturazione (CTRL+ALT+F)" title="Fatturazione (CTRL+ALT+F)" />
</div><div id="divImageRiepilogo">
    <img src="Images/Riep_Dich.png" id="CallRiepilogo" border="0" onclick="javascript:return CallFatturazione();" style="cursor:pointer" alt="Cambia applicazione (SHIFT+F9)" title="Cambia applicazione (SHIFT+F9)" />
</div><div id="divImagePrint"><input type="image" name="ImagePrint" id="ImagePrint" ***disabled="disabled"*** title="Stampa (SHIFT+F3)" class="aspNetDisabled" src="Images\stampa.png" /></div>
        <div id="divImageHelpCon"><input type="image" name="imageHelpCon" id="imageHelpCon" ***disabled="disabled"*** title="Help Contestuale (SHIFT+F4)" class="aspNetDisabled" src="Images\helpCont.png" /></div>
        <div id="divImageLibrary"><input type="image" name="imageLibrary" id="imageLibrary" title="Library (SHIFT+F8)" src="Images\library.png" onclick="javascript:return ClientCheckForSave(&#39;ImageLibrary&#39;);" /></div>
    </div>

Why at the postback, I have id="imageHelpCon" disabled="disabled" . I specify in page_load only ((ImageButton)Page.FindControl("ImagePrint")).Enabled = false; ???

I didn't find server side solution, I ended moving the creation of the elements on client side using js and jquery, doing like this the bad behaviour doesn't happen:

$("#divOnPage").after("<div id='divAdded1'>" +
    "<input " +
        "id='btnAdded1' " +
        "type='image' " +
        "src='Images/Navigator/image1.png' " +
        "border='0' " +
        "onclick='func1(\"\");return false;' " +
        "alt='title 1' " +
        "title='title 1' " +
        "style='cursor:pointer;margin-left:2px;' " +
    "/>" +
"</div>");
$("#divAdded1").after("<div id='divAdded2'>" +
    "<input " +
        "id='btnAdded2' " +
        "type='image' " +
        "src='Images/Navigator/image2.png' " +
        "border='0' " +
        "onclick='func2(\"\");return false;' " +
        "alt='title 2' " +
        "title='title 2' " +
        "style='cursor:pointer;margin-left:2px;' " +
    "/>" +
"</div>");

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