简体   繁体   English

在用户导航期间如何禁用控件时,如何使用c#中的Control.AddAt管理动态添加的asp.net控件

[英]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. 我有一个带有某些imagebutton的Web窗体。 Some added in frontend, some at runtime in the codebehind. 一些在前端中添加,一些在运行时在后面的代码中。

This is aspx: 这是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. 1)我在aspx上有相同的图像按钮。

2) I dynamically add some other imagebuttons using, in the codebehind, the method Control.AddAt(positionOfAControl, newControl) 2)我在背后的代码中使用Control.AddAt(positionOfAControl,newControl)方法动态添加了其他一些imagebutton

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. 3)在page_load的末尾,它禁用了用于检索插入新控件的位置的控件。

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. 碰巧,在用户导航期间,例如,当用户单击添加到代码隐藏(Fatturazione)中的第一个控件时,如果用户单击下一个图像按钮(CallRiepilogo),则在回调后加载的页面禁用了imageButton imageHelpCon。回调禁用了imagebutton imageLibrary。 Why? 为什么?

This is a piece of html at first load: 这是第一次加载时的一段html:

    <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" . 为什么在回发时,我有id =“ imageHelpCon” disabled =“ disabled” I specify in page_load only ((ImageButton)Page.FindControl("ImagePrint")).Enabled = false; 我仅在page_load中指定((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: 我没有找到服务器端的解决方案,我结束了使用js和jquery在客户端移动元素的创建,这样做不会发生不良行为:

$("#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>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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