如何在aspx页面上将自定义值传递给asp.net控件?

时间:2021-11-18 04:13:50

I need to generate the following in ASP.NET page. What's the best, easiest way to do that?

我需要在ASP.NET页面中生成以下内容。什么是最好,最简单的方法?

Simplified example. So to custom property I need to pass string which would include index (i) to the property of the control. I could do it from codebehind, but it would be simpler and easier if I could keep it in .aspx file.

简化示例。因此,对于自定义属性,我需要将包含index(i)的字符串传递给控件的属性。我可以从代码隐藏中做到这一点,但如果我能将它保存在.aspx文件中,它会更简单容易。

<table>
<% 
    for( var i = 0; i < 10; i++ )
{
%><tr>
    <td>
        <cc1:CustomControl runat="server" CustomProperty="SomeText[<% i %>]"/>
    </td>
</tr>
<% } %>
</table>

Essentially I need to pass a custom, not predetermined value to the asp.net control.

基本上我需要将一个自定义的,而不是预定的值传递给asp.net控件。

3 个解决方案

#1


This probably won't work how you expect it to. Instead, add a place holder like this:

这可能不会像你期望的那样有效。相反,添加这样的占位符:

<table>
  <asp:PlaceHolder id="RowPlaceHolder" runat="server" />
</table>

And then in your code behind:

然后在你的代码背后:

for (int i = 0;i<10;i++)
{
   var tr = new HTMLTableRow();
   var td = new HTMLTableCell();
   var Custom = (CustomControl)LoadControl("MyControl.ascx");
   Custom.id="Custom" + i.ToString();
   Custom.CustomProperty = "SomeText[" + i.ToString() + "]";

   td.Controls.Add(Custom);
   tr.Controls.Add(td);
   RowPlaceHolder.Controls.Add(tr);
}

Going deeper, if the number 10 really is hard-coded, you'll find things much easier to deal with in the long run if you just go ahead and copy 10 entries for your control into the aspx markup manually. Dynamic controls in ASP.Net webforms are rife with pitfalls and gotchas.

更深入的是,如果数字10真的是硬编码的,那么从长远来看,如果你只是手动将10个控件复制到aspx标记中,你会发现更容易处理。 ASP.Net webforms中的动态控件充满了陷阱和陷阱。

If the number comes from some legitimate datasource, then you're likely better off actually using that datasource and binding it to a data control like a repeater.

如果数字来自某些合法数据源,那么您最好使用该数据源并将其绑定到数据控件(如转发器)。

#2


<%= i %>

Should work for you.

应该适合你。

#3


You could rock out with a repeater and use the Container.ItemIndex,

您可以使用转发器摇滚并使用Container.ItemIndex,

<asp:Repeater ID="rpt" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td> 
                <cc1:CustomControl runat="server" CustomProperty="SomeText[<%# Container.ItemIndex %>]"/>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

#1


This probably won't work how you expect it to. Instead, add a place holder like this:

这可能不会像你期望的那样有效。相反,添加这样的占位符:

<table>
  <asp:PlaceHolder id="RowPlaceHolder" runat="server" />
</table>

And then in your code behind:

然后在你的代码背后:

for (int i = 0;i<10;i++)
{
   var tr = new HTMLTableRow();
   var td = new HTMLTableCell();
   var Custom = (CustomControl)LoadControl("MyControl.ascx");
   Custom.id="Custom" + i.ToString();
   Custom.CustomProperty = "SomeText[" + i.ToString() + "]";

   td.Controls.Add(Custom);
   tr.Controls.Add(td);
   RowPlaceHolder.Controls.Add(tr);
}

Going deeper, if the number 10 really is hard-coded, you'll find things much easier to deal with in the long run if you just go ahead and copy 10 entries for your control into the aspx markup manually. Dynamic controls in ASP.Net webforms are rife with pitfalls and gotchas.

更深入的是,如果数字10真的是硬编码的,那么从长远来看,如果你只是手动将10个控件复制到aspx标记中,你会发现更容易处理。 ASP.Net webforms中的动态控件充满了陷阱和陷阱。

If the number comes from some legitimate datasource, then you're likely better off actually using that datasource and binding it to a data control like a repeater.

如果数字来自某些合法数据源,那么您最好使用该数据源并将其绑定到数据控件(如转发器)。

#2


<%= i %>

Should work for you.

应该适合你。

#3


You could rock out with a repeater and use the Container.ItemIndex,

您可以使用转发器摇滚并使用Container.ItemIndex,

<asp:Repeater ID="rpt" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td> 
                <cc1:CustomControl runat="server" CustomProperty="SomeText[<%# Container.ItemIndex %>]"/>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>