将值从其他控件发送到.aspx页面中的用户控件属性

时间:2022-10-19 22:18:46

Is there a way pass values from other controls (e.g. "selected value of dropdownlist", "value from query string") to a User Control using a property within the tag itself and NOT from the code behind?

有没有办法将其他控件的值(例如“dropdownlist的选定值”,“查询字符串的值”)传递给用户控件,使用标签本身内的属性而不是后面的代码?

2 个解决方案

#1


Yes you can, you just need to use the <% %> in the presentation code. Your code would look something like this:

是的,你只需要在演示代码中使用<%%>即可。您的代码看起来像这样:

<asp:DropDownList id="ddlFoo" runat="server">
    ...
</asp:DropDownList>
<asp:TextBox id="txtBar" runat="server" Text='<%# ddlFoo.SelectedValue %>' />

<%-- For query string --%>
<asp:TextBox id="txtBar" runat="server" 
    Text='<%# Request.QueryString["Key_Value"] %>' />

The SO post In ASP.Net, what is the difference between <%= and <%# gives a good listing of the different binding mechanisms you can use.

SO帖子在ASP.Net中,<%=和<%#之间的区别是给出了可以使用的不同绑定机制的良好列表。

#2


Yes it is. For example

是的。例如

 <uc1:CompetitionClassification ID="CompetitionClassification" runat="server" OnlyTopFive="True" />

in this case, the parameter OnylTopFive is passed within tag of my custom control.

在这种情况下,参数OnylTopFive在我的自定义控件的标记内传递。

then in server side of my control, i have :

然后在我控制的服务器端,我有:

private bool onlyTopFive;
  public bool OnlyTopFive
    {
        get
        {
            return this.onlyTopFive;
        }
        set
        {
            this.onlyTopFive = value;
        }
    }

#1


Yes you can, you just need to use the <% %> in the presentation code. Your code would look something like this:

是的,你只需要在演示代码中使用<%%>即可。您的代码看起来像这样:

<asp:DropDownList id="ddlFoo" runat="server">
    ...
</asp:DropDownList>
<asp:TextBox id="txtBar" runat="server" Text='<%# ddlFoo.SelectedValue %>' />

<%-- For query string --%>
<asp:TextBox id="txtBar" runat="server" 
    Text='<%# Request.QueryString["Key_Value"] %>' />

The SO post In ASP.Net, what is the difference between <%= and <%# gives a good listing of the different binding mechanisms you can use.

SO帖子在ASP.Net中,<%=和<%#之间的区别是给出了可以使用的不同绑定机制的良好列表。

#2


Yes it is. For example

是的。例如

 <uc1:CompetitionClassification ID="CompetitionClassification" runat="server" OnlyTopFive="True" />

in this case, the parameter OnylTopFive is passed within tag of my custom control.

在这种情况下,参数OnylTopFive在我的自定义控件的标记内传递。

then in server side of my control, i have :

然后在我控制的服务器端,我有:

private bool onlyTopFive;
  public bool OnlyTopFive
    {
        get
        {
            return this.onlyTopFive;
        }
        set
        {
            this.onlyTopFive = value;
        }
    }