简体   繁体   English

从中继器Asp.net C#外的谁背后的代码隐藏访问div

[英]Access to div from codebehind who's outside a repeater Asp.net C#

I know its maybe unusual, but i want add htmlGenericControl to a div (it's outside a repeater) in ItemDataBound from CodeBehind.. 我知道它可能很不正常,但是我想在CodeBehind的ItemDataBound中将htmlGenericControl添加到div(在转发器之外)中。

HtmlGenericControl slider = (HtmlGenericControl)e.Item.FindControl("slider");

htmlGenericControl input = new HtmlGenericControl("input");
input.Attributes.Add("type", "radio");
input.Attributes.Add("name", "slide_switch");
input.Attributes.Add("id", string.Format("projectImage-{0}", item.ProjectImageId));

slider.Controls.Add(input);

but its return null everytime. 但每次都返回null this is the aspx code: 这是aspx代码:

  <div class="slider">
      <asp:Repeater ID="rptProjectImages" runat="server" OnItemDataBound="rptProjectImages_ItemDataBound">

           <ItemTemplate>
           </ItemTemplate>

       </asp:Repeater>
   </div>

Didnt work with asp.net too long, but probably this should help 与asp.net的合作时间过长,但这可能会有所帮助

<div id="myDiv" runat="server">...</div>

and in codebehind myDiv should be accessible. 并且myDiv应该可以访问。

Several issues with this code. 此代码有几个问题。

First, your div is client-side only, from server-side point of view it's just a string. 首先,您的div仅限于客户端,从服务器端来看,它只是一个字符串。 Turn it into server-side control with: 使用以下命令将其转换为服务器端控制:

<div class="slider" runat="server" ID="slider">

Second, FindControl looks for immediate children only, in your case children of the repeater item. 其次, FindControl仅查找直属子级,在您的情况下为转发器项的子级。 slider is not one of those. slider不是其中之一。 Moreover, it is not a part of the repeater item template and should be accessible as in in code behind, so just 而且,它不是转发器项目模板的一部分,应该可以像后面的代码中那样访问它,因此

slider.Controls.Add(...

That is unless slider and the repeater you showed are a part of some other template of some "outer" control. 除非您显示的slider和转发器是某些“外部”控件的其他模板的一部分,否则这是必须的。 In which case make sure to use that "outer" control to call FindControl on. 在这种情况下,请确保使用该“外部”控件来调用FindControl

Finally, don't mess with id . 最后,不要搞乱id I bet this is either going to be overridden by ASP.NET, or will cause issues on the page. 我敢打赌这将被ASP.NET覆盖,否则将导致页面出现问题。 Instead set client ID mode to static and assign ID property: 而是将客户端ID模式设置为static并分配ID属性:

input.ClientIDMode = ClientIDMode.Static;
input.ID = string.Format("projectImage_{0}", item.ProjectImageId);

This is eventually output the same value for id you needed, but in more ASP.NET compliant way. 最终,它会为您所需的id输出相同的值,但采用与ASP.NET兼容的方式。 One note though is that I replaced "-" with "_" - server side controls cannot have hyphens in ID 不过请注意,我将“-”替换为“ _”-服务器端控件的ID中不能包含连字符

FindControl finds a control within another, but does so looking for the control's id. FindControl在另一个控件中查找控件,但这样做是在查找控件的ID。 Your "slider" control has no id, it uses a class named "slider" but has no id. 您的“滑块”控件没有ID,它使用名为“滑块”的类,但没有ID。

You will need to define the control as 您将需要定义控件为

<div runat="server" id="Slider" class="slider">
    <asp:Repeater ID="rptProjectImages" runat="server" OnItemDataBound="rptProjectImages_ItemDataBound">

         <ItemTemplate>
         </ItemTemplate>

     </asp:Repeater>
</div>

The runat="server" tells the framework to instantiate that control in your code behind. runat="server"告诉框架在您后面的代码中实例化该控件。 The id will be the name of the object that is that control. id将是该控件的对象的名称。 Then in your code, you can do 然后在您的代码中,您可以执行

htmlGenericControl input = new HtmlGenericControl("input");
input.Attributes.Add("type", "radio");
input.Attributes.Add("name", "slide_switch");
input.ID = string.Format("projectImage-{0}", item.ProjectImageId);

Slider.Controls.Add(input);

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

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