简体   繁体   English

动态添加的ASP.NET控件从后面的代码中看不到

[英]ASP.NET controls added dynamically not visible from the code behind

I'm adding a dynamically built set of checkboxes to an asp.net page from the code behind with something like this in a recursive fashion: 我正在以递归的方式从后面的代码向asp.net页面添加一组动态构建的复选框:

pnlPageAccessList.Controls.Add(myCheckboxControl);

The controls show up fine on the page, but they don't show up when I view source, nor can I access them from the code behind. 控件在页面上显示正常,但是当我查看源代码时它们不显示,也不能从后面的代码中访问它们。 If I add the controls in the on_init method, they work. 如果我在on_init方法中添加控件,它们就可以工作。 But I have some business rules driving changes to the list of controls itself that require I fire the add method elsewhere. 但是我有一些业务规则驱动对控件列表本身的更改,这需要我在别处激活add方法。 Has anyone seen this before? 谁看过这个吗? I'm away from work so I can't copy the exact code. 我没有工作,所以我无法复制确切的代码。

I have two terrible ideas for how to get it working. 关于如何让它发挥作用,我有两个可怕的想法。 One involves some jQuery and a set of hidden controls holding a big array of integers; 一个涉及一些jQuery和一组隐藏的控件,包含大量的整数; the other is run the method on_init AND on my other events so the controls at least show up. 另一个是运行方法on_init和我的其他事件,所以控件至少会显示出来。 Both smell like ugly hacks. 两者都闻起来像丑陋的黑客。 The second one I suspect won't work to read the values out of the checkboxes. 我怀疑第二个不会从复选框中读取值。

On the server side the page is recreated from scratch every postback, so if you add any controls dynamically, you have to re-add them on every postback. 在服务器端,每次回发都会从头开始重新创建页面,因此如果您动态添加任何控件,则必须在每次回发时重新添加它们。

As you add the controls at runtime they are not known at compile time, so there is no variables declared for the controls in the Page object. 当您在运行时添加控件时,它们在编译时是未知的,因此没有为Page对象中的控件声明变量。 If you want to access the controls you either have to keep the reference from when you create the controls, or locate them in the Controls collection where you put them. 如果要访问控件,则必须在创建控件时保留引用,或者在放置它们的Controls集合中找到它们。

如果您可以设置复选框控件的ID,则可以使用后面的代码中的FindControl方法来检索控件实例。

@Anero is right that you can add an ID and use FindControl. @Anero是正确的,您可以添加ID并使用FindControl。

You can also use a checkbox list, and add checkboxes to that list. 您还可以使用复选框列表,并向该列表添加复选框。 Then they're already in a predefined control in your markup and code-behind. 然后他们已经在您的标记和代码隐藏中的预定义控件中。

You don't say where the method has to be fired, but once they're added dynamically, they do have to be added on every postback. 您没有说出必须触发该方法的位置,但是一旦它们被动态添加,就必须在每次回发时添加它们。 You probably have a little more flexibility than just adding them at the Init event, as long as you stay aware of where things like validation occurs (if it matters in this case), or where you want to handle the checkbox contents. 您可能比在Init事件中添加它们更具灵活性,只要您了解验证发生的位置(在这种情况下是否重要),或者您想要处理复选框内容的位置。 You can defer as late as PreRender for getting checkbox content. 您可以推迟到PreRender获取复选框内容。

Well, looks like I'm going to have to do it clientside. 嗯,看起来我将不得不做客户端。 Thanks for the answers. 谢谢你的回答。 I'd be able to do it On_Init, but doing it clientside with a hidden control is gonna save me a lot of overhead and be more flexible moving forward. 我能够做到On_Init,但是使用隐藏控件做客户端会为我节省很多开销并且更加灵活地向前发展。

If anyone's curious, here's the jQuery for finding all the checked checkboxes and putting their value attribute into a hidden control in a comma delimited list: 如果有人好奇,这里是用于查找所有已选中复选框并将其value属性放入逗号分隔列表中的隐藏控件的jQuery:

<script type="text/javascript">
    $(document).ready(function () {
        $('[id*=PagesPanel]').find(':checkbox').click(function () {
            $('[id*=PagesPanel]').find(':checked').each(function () {
                $('[id*=lblHiddenPageArray]').append($(this).val() + ", ");
            });
        });
    });
</script>

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

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