Getting property values ​​of dynamically created controls in asp.net

How can I determine the width of some asp.net control that was created dynamically? For example, I have a code like this:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <encosia:HighslideManager ID="HighslideManager1" runat="server" FadeInOut="true" OutlineType="RoundedWhite" ControlBar="false" /> <table style="width: 100%; padding-left: 15px; padding-right: 15px;"> <tr> <td valign="top" style="width: 50%; border-right: dotted 2px White;"> <asp:literal id="litText" runat="server" mode="PassThrough"></asp:literal> </td> <td valign="top" style="width: 50%"> <table style="width: 100%;" cellspacing="10"> <tr> <td valign="top" style="width: 50%;" id="imageTD" runat="server" oninit="imageTD_OnInit"> <asp:literal id="litEmptyText" runat="server" mode="PassThrough"></asp:literal> <asp:repeater id="Repeater1" runat="server"> <ItemTemplate> <center> <encosia:HighslideImage ID="HighslideImage1" runat="server" Width="200px" ImageUrl='<%# Eval("ImageURL", "images/images/{0}") %>' FullImageURL='<%# Eval("ImageURL", "images/images/{0}") %>' AlternateText='Image <%# Container.ItemIndex%>'/> <asp:Label ID="imageDescriptionLabel" runat="server" CssClass="longtext" Text= '<%# CutImageDescText(String.Format("{0}",Eval("Description")),imageTD.Width) %>' /> </center> </ItemTemplate> <SeparatorTemplate> <%# ((Container.ItemIndex % 2) == 1) ? "</td></tr><tr><td valign=\"top\" style=\"width:50%;\">" : "</td><td valign=\"top\" style=\"width:50%;\">"%> </SeparatorTemplate> </asp:repeater> </td> </tr> </table> </td> </tr> </table> 

I need to calculate imageTD width. I am trying to calculate its width in page events, but this property also looks like the property of other styles is empty. (((Please help me! Thank you!

+4
source share
5 answers

This can be done as follows:

  • If you want to use the td tag as a server, you need to set the "runat" of the table tag to "server" and set the identifier of this table
  • You also need to set the tr and td tags of the server and identifier
  • If the created td is not included in any data controls, such as a repeater, you can easily get any properties of this control on your server side.
Page example

.aspx

 <table runat="server" id="tabl1"> <tr runat="server" id="tr1"> <td runat="server" id="td1"> </td> </tr> </table> 

.aspx.cs code exaple

 protected void Page_Load(object sender, EventArgs e) { var h = td1.Width; } 
+1
source

So, I'm not so expert, so this is not a complete answer ... From what I see it is not a dynamically created control, I'm sure you mean that the size is dynamic? So how would that change depending on what's inside?

I wrote code with jQuery that gets the width of td and sets a hidden value for it, and then gets the value of this hidden field from asp.net. Perhaps the best ways send value from js to asp.net

One more thing, because of the return mail, I think, try to press the button twice. (Again, someone smarter can answer why this is happening)

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="testing._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript" /> </head> <body> <form id="form1" runat="server"> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="click" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:HiddenField ID="hid" runat="server" /> </form> <table> <tr> <td id="image2"> <img src="http://i38.tinypic.com/2el8jfb.jpg" /> </td> </tr> </table> <script type="text/javascript"> $(document).ready( function() { var w = $('#image2').width(); $("#hid").val(w); } ); </script> </body> </html> protected void click(object sender, EventArgs e) { TextBox1.Text = hid.Value; } 

I thought of all the servers using the id and runat server controls, but when I access this image using code using imageTD.Width.Value, it turns out 0, maybe someone can understand why this ... .

Hope this helps! =)

0
source

I believe that you can use Repeater1.FindControl and find your control as long as you have the sender e from the row command (you may need to add the preInit delegate so that it can run it). You will have to drop it with something like this.

Image test = (Image) e.Item.FindControl ("youImageName");

0
source

The answer is very simple: you cannot archive it on the server side.

0
source

Unable to know the β€œdynamic” width of the control. The only width you can get from the control is the one you set in the width attribute.

Think about fixing it with a pixel unit instead of a percentage, this once helps.

0
source

All Articles