2

I want to show checkbox or radiobutton according to typeId.

<%# (Eval("TypeId").ToString() == "1") ? "CheckBox" : "RadioButton"%>
<asp:CheckBox ID="CheckBox" runat="server" Text='<%#Eval("OptionName")%>' />
<asp:RadioButton ID="RadioButton" runat="server" Text='<%#Eval("OptionName")%>' />

If typeId == 1 I want to show checkbox that is in my code line else I want to show radio button. How can I do this.

Thanks.

4
  • Use the visible property of both controls. Commented Jun 15, 2012 at 9:06
  • This is too difficult for me. Because I used 3 nested repeater and this code in third level repeater :) Commented Jun 15, 2012 at 9:08
  • Why does that make it difficult? Commented Jun 15, 2012 at 9:13
  • beceuse of more codes :) Commented Jun 15, 2012 at 9:23

4 Answers 4

2

You can use the Visible property:

<asp:CheckBox ID="CheckBox" runat="server" 
    Text='<%#Eval("OptionName")%>' 
    Visible='<%# Eval("TypeId").ToString() == "1" %>'
/>
<asp:RadioButton ID="RadioButton" runat="server" 
    Text='<%#Eval("OptionName")%>' 
    Visible='<%# Eval("TypeId").ToString() != "1" %>'
/>
Sign up to request clarification or add additional context in comments.

Comments

0

Add two bool properties to your datasource called, for example, ShowCheckBox and ShowRadioButton

public bool ShowCheckBox
{
    get
    {
         return TypeId.ToString() == "1";
    }
}

and use these to data bind to the Visible properties

<asp:CheckBox ID="CheckBox" runat="server" Visible='<%#Eval("ShowCheckBox")%>' />

Comments

0

Try this:

<asp:CheckBox ID="CheckBox" runat="server" Visible="<%# (Eval("TypeId").ToString() == "1") ? True : False %>" Text='<%#Eval("OptionName")%>' />

1 Comment

This is good idea. but occur "string too bool not converted error". I solved it with Tim Schmelter's answer
0

In the Repeater.ItemDatabound event, add the appropriate control there.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.