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

927 views Asked by At

I know its maybe unusual, but i want add htmlGenericControl to a div (it's outside a repeater) in ItemDataBound from CodeBehind..

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. this is the aspx code:

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

           <ItemTemplate>
           </ItemTemplate>

       </asp:Repeater>
   </div>
3

There are 3 answers

4
Andrei On BEST ANSWER

Several issues with this code.

First, your div is client-side only, from server-side point of view it's just a string. 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. slider is not one of those. 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. In which case make sure to use that "outer" control to call FindControl on.

Finally, don't mess with id. I bet this is either going to be overridden by ASP.NET, or will cause issues on the page. Instead set client ID mode to static and assign ID property:

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. One note though is that I replaced "-" with "_" - server side controls cannot have hyphens in ID

0
tym32167 On

Didnt work with asp.net too long, but probably this should help

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

and in codebehind myDiv should be accessible.

0
AudioBubble On

FindControl finds a control within another, but does so looking for the control's id. Your "slider" control has no id, it uses a class named "slider" but has no 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. The id will be the name of the object that is that control. 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);