Getting value of properties of dynamicly created controls in asp.net
How I can determinate width of some asp.net control, that was created dynamicly? For example I have such code:
<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>
</It开发者_高级运维emTemplate>
<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 width of imageTD. I've trying to calculate its width in pages events, but this property also like other styles property is empty. ((( Please help me! Thanks!
You can do this by folowing:
- If you want use "td" tag as server, you need set "runat" of table tag to "server" and set ID of this table
- Also you need set "server and ID" tr and td tags
- If created td do not into any databind controls, like a repeater you can easy get any properties from this control on your server side
.aspx page example
<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;
}
So Im not so much an expert so this isn't a complete answer ... From what I see its not a dynamically created control, I bet you mean the size is dynamic? Since it would change depending on what is inside?
I wrote some code with JQuery that gets the width of a td and set a hidden value to it and then get the value of that hidden field from asp.net. There could be better ways send the value from js to asp.net
One more thing, because of the postbacks, i think, try clicking the button twice. (Again maybe someone smarter can answer why that happens)
<%@ 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 going all server side, using control with an id and runat server, but when I access that image through code behind with imageTD.Width.Value, it turns out 0, maybe someone can figure why that is...
Really hope that helps you! =)
I belive you can use Repeater1.FindControl and find your control as long as you have your sender e from the row command (maybe you will need to add a delegate the preInit to get it to fire.). You will have to cast it something like this.
Image test = (Image)e.Item.FindControl("youImageName");
The answer is very simple: you can't archive this in server-side.
There's is no way to know the "dynamic" width of a control. The only width you can get from a control is the one you set into the width attribute.
Consider to fix it with pixels unit instead of percent, it sometime helps a lot.
精彩评论