views:

613

answers:

2

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?

+1  A: 

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.

Gavin Miller
@LFSR Consulting: Parser Error Message: Server tags cannot contain <% ... %> constructs.
Brian David Berman
@LRSR Consulting: I tried your example and it sticks <%= ddlFoo.SelectedValue %> into the textbox as the value (as if it's just a string)
Brian David Berman
@Brian, that's my bad, I can never remember which value to use; try: <%# ddlFoo.SelectedValue %>
Gavin Miller
A: 

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.

then in server side of my control, i have :

private bool onlyTopFive;
  public bool OnlyTopFive
    {
        get
        {
            return this.onlyTopFive;
        }
        set
        {
            this.onlyTopFive = value;
        }
    }
DaDa
I'm trying to pass values from other controls, not hard-coded values.
Brian David Berman