获取asp.net中动态创建的控件的属性值

时间:2010-10-20 09:48:17

标签: c# asp.net

我如何确定动态创建的某些asp.net控件的宽度? 例如,我有这样的代码:

<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>

我需要计算imageTD的宽度。 我试图在页面事件中计算它的宽度,但是这个属性也像其他样式属性一样是空的。 ((( 请帮我! 谢谢!

5 个答案:

答案 0 :(得分:1)

您可以通过以下方式执行此操作:

  1. 如果要使用“td”标记作为服务器,则需要将表标记的“runat”设置为“server”并设置此表的ID
  2. 您还需要设置“服务器和ID”tr和td标记
  3. 如果创建了td,请不要进入任何数据绑定控件,例如转发器,您可以轻松地从服务器端的此控件获取任何属性
  4. .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 :(得分:0)

所以我不是一个专家,所以这不是一个完整的答案......从我看到它不是一个动态创建的控件,我打赌你的意思是大小是动态的?因为它会根据里面的内容而改变吗?

我用JQuery编写了一些代码来获取td的宽度并为其设置隐藏值,然后从asp.net获取该隐藏字段的值。可以有更好的方法将值从js发送到asp.net

还有一件事,因为回发,我想,尝试点按两次按钮。 (也许更聪明的人可以回答为什么会发生这种情况)

<%@ 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;
        }

我想到了所有服务器端,使用带有id和runat服务器的控件,但是当我通过imageTD.Width.Value后面的代码访问该图像时,它结果为0,也许有人可以想出为什么这是..

真希望对你有所帮助! =)

答案 2 :(得分:0)

我相信你可以使用Repeater1.FindControl并找到你的控件,只要你有来自row命令的你的发送者e(也许你需要添加一个委托preInit来解雇它。)。你将不得不施展它 像这样的东西。

图像测试=(图像)e.Item.FindControl(“youImageName”);

答案 3 :(得分:0)

答案很简单:你不能在服务器端存档它。

答案 4 :(得分:0)

无法知道控件的“动态”宽度。您可以从控件获得的唯一宽度是您在width属性中设置的宽度。

考虑用像素单位而不是百分比来修复它,它有时会帮助很多。