locked
Disable or Enable Panel control based on RadioButtonList selectedItem, not working RRS feed

  • Question

  • User1216627406 posted

    I have a RadioButtonList and Panel control. Both are inside a Repeater control.

    <asp:Repeater ID="repeater2" runat="server" OnItemDataBound="repeater_ItemDataBound">
    
           <td>
    	Purchased:<asp:RadioButtonList ID="rblType" runat="server" ValidationGroup ="stype" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;">
    	<asp:ListItem Text="New" />
    	<asp:ListItem Text="Used" />
           </asp:RadioButtonList><br />
           <asp:RequiredFieldValidator style="color:#ff0000;" id="RequiredFieldValidator1"
           ControlToValidate="rblType"
           ErrorMessage="Please choose New or Used"
           ValidationGroup ="stype"
           runat="server"/>
          </td>
    
          <td colspan="2">
           <asp:Panel ID="uPanel" runat="server" Enabled="false">
    	<span style="text-align:left;"> If purchased used this year, write the name and address of the
    	 previous owner.</span>
    	<br />
    	 <div class="input text select"> NAME: <asp:TextBox ID="txtPrevOnwerName" style="width:200px;" runat="server"></asp:TextBox>
    	 ADDRESS: <asp:TextBox ID="txtprevAddr" style="width:200px;" runat="server"></asp:TextBox>
    	 <span style="white-space:nowrap;">CITY:<asp:TextBox ID="txtprevCity" style="width:100px; margin-left:15px;" runat="server"></asp:TextBox>STATE: 
    	  <asp:DropDownList ID="ddlPrevState" runat="server" AppendDataBoundItems="True">
    	  <asp:ListItem Value="" Selected="False"></asp:ListItem>
    	  </asp:DropDownList>    
    	 ZIP:<asp:TextBox ID="txtprevzip" runat="server"></asp:TextBox></div></span></asp:Panel>
           </td>
    </asp:Repeater>

    By default, the panel control is disabled.

    Our requirement is that when a user selects item New, the Panel control should be disabled.

    If user selects Used, the Panel control should be enabled so user can fill the boxes inside the Panel control.

    I have tried the following code inside repeater_ItemDataBound but nothing happens. It doesn't change from disabled to enabled or visa versa.

    Any ideas what's wrong with the code below?

            foreach (RepeaterItem item in Repeater2.Items)
            {
                string ddlType = (item.FindControl("rblType") as RadioButtonList).SelectedItem.Text;
                System.Web.UI.WebControls.Panel uPanel1 = item.FindControl("uPanel") as System.Web.UI.WebControls.Panel;
    
                if (ddlType == "New")
                {
                    txtbox.Enabled = false;
                }
    
                else
                {
                    txtbox.Enabled = true;
                }
            }

    Thanks in advance

    Thursday, August 10, 2017 2:13 PM

Answers

  • User2103319870 posted

    Initially, uPanel is disabled.

    You click Used, it is enabled but that's it.

    You cannot re-disable it.

    So you need to enable the panel if user selects "used" and disable the panel if user selects "new". If that is the case then you just need add another condition to selected index changed event like below

     protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
            {
                //Find the radiobutton list control
                RadioButtonList rdolist = sender as RadioButtonList;
                //Find the repeater Item 
                RepeaterItem item = rdolist.NamingContainer as RepeaterItem;
                //Check if radiobuttonlist selected control is "Used"
                if ((item != null) && (rdolist.SelectedItem.Text == "Used"))
                {
                    //if so then find the panel control
                    Panel pnl = item.FindControl("uPanel") as Panel;
                    //then enable the panel
                    pnl.Enabled = true;
                }
                else if (rdolist.SelectedItem.Text == "New")
                {
                    //if so then find the panel control
                    Panel pnl = item.FindControl("uPanel") as Panel;
                    //then disable the panel
                    pnl.Enabled = false;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 10, 2017 7:28 PM

All replies

  • User2103319870 posted

    It doesn't change from disabled to enabled or visa versa.

    you can use RadioButtonList Selectedindex changed event to enable and disable the panel of selected row

    Change your Radiobuttonlist like below

      <asp:RadioButtonList ID="rblType" runat="server" ValidationGroup="stype" RepeatDirection="Horizontal" TextAlign="Right" Style="display: inline;" AutoPostBack="true" OnSelectedIndexChanged="rblType_SelectedIndexChanged">
                                     <asp:ListItem Text="New" />
                                     <asp:ListItem Text="Used" />
                                 </asp:RadioButtonList><br />

    now we can use the SelectedIndexChanged event of radiobuttonlist

     protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
            {
                //Find the radiobutton list control
                RadioButtonList rdolist = sender as RadioButtonList;
                //Find the repeater Item 
                RepeaterItem item = rdolist.NamingContainer as RepeaterItem;
                //Check if radiobuttonlist selected control is "Used"
                if ((item != null) && (rdolist.SelectedItem.Text == "Used"))
                {
                    //if so then find the panel control
                    Panel pnl = item.FindControl("uPanel") as Panel;
                    //then enable the panel
                    pnl.Enabled = true;
                }
            }

    Thursday, August 10, 2017 2:29 PM
  • User1216627406 posted

    Thanks for the prompt response as always.

    It is still not working.

    Is the SelectedChanged coded to work for a Repeater control?

    I know it is a silly question because I see this:

           RepeaterItem item = rdolist.NamingContainer as RepeaterItem;

     

    Thursday, August 10, 2017 2:51 PM
  • User2103319870 posted

    Is the SelectedChanged coded to work for a Repeater control?

    Yes. 

    Thursday, August 10, 2017 3:01 PM
  • User1216627406 posted

    It looks really good but when I click either New or Used, all the screen does is jump but no changes to panel control in terms disable or enable.

    Thursday, August 10, 2017 3:14 PM
  • User2103319870 posted

    It looks really good but when I click either New or Used, all the screen does is jump but no changes to panel control in terms disable or enable.

    Make sure you not reloading the Repeater on every postbacks. I tested and it works fine. Please see the complete code below

    HTML

    <asp:Repeater ID="Repeater1" runat="server">
                    <HeaderTemplate>
                        <table>
                            <tr>
                                <th>Name</th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>Purchased:
                                 <asp:RadioButtonList ID="rblType" runat="server" ValidationGroup="stype" RepeatDirection="Horizontal" TextAlign="Right" Style="display: inline;" AutoPostBack="true" OnSelectedIndexChanged="rblType_SelectedIndexChanged">
                                     <asp:ListItem Text="New" />
                                     <asp:ListItem Text="Used" />
                                 </asp:RadioButtonList><br />
                                <asp:RequiredFieldValidator Style="color: #ff0000;" ID="RequiredFieldValidator1"
                                    ControlToValidate="rblType"
                                    ErrorMessage="Please choose New or Used"
                                    ValidationGroup="stype"
                                    runat="server" />
                            </td>
                            <td colspan="2">
                                <asp:Panel ID="uPanel" runat="server" Enabled="false">
                                    <span style="text-align: left;">If purchased used this year, write the name and address of the
    	 previous owner.</span>
                                    <br />
                                    <div class="input text select">
                                        NAME:
                                        <asp:TextBox ID="txtPrevOnwerName" Style="width: 200px;" runat="server"></asp:TextBox>
                                        ADDRESS:
                                        <asp:TextBox ID="txtprevAddr" Style="width: 200px;" runat="server"></asp:TextBox>
                                        <span style="white-space: nowrap;">CITY:<asp:TextBox ID="txtprevCity" Style="width: 100px; margin-left: 15px;" runat="server"></asp:TextBox>STATE: 
    	                                      <asp:DropDownList ID="ddlPrevState" runat="server" AppendDataBoundItems="True">
                                                  <asp:ListItem Value="" Selected="False"></asp:ListItem>
                                              </asp:DropDownList>
                                            ZIP:<asp:TextBox ID="txtprevzip" runat="server"></asp:TextBox>
                                    </div>
                                    </span>
                                </asp:Panel>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    C#:

    DataTable cstable;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AdventureWorksConnectionString"].ToString());
                    string sSQL = "Select top 10 FirstName FROM DimCustomer";
                    SqlCommand cmd3 = new SqlCommand(sSQL, con);
                    con.Open();
                    cstable = new DataTable();
                    cstable.Load(cmd3.ExecuteReader());
    
                    Repeater1.DataSource = cstable;
                    Repeater1.DataBind();
                }
            }
    
            protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
            {
                //Find the radiobutton list control
                RadioButtonList rdolist = sender as RadioButtonList;
                //Find the repeater Item 
                RepeaterItem item = rdolist.NamingContainer as RepeaterItem;
                //Check if radiobuttonlist selected control is "Used"
                if ((item != null) && (rdolist.SelectedItem.Text == "Used"))
                {
                    //if so then find the panel control
                    Panel pnl = item.FindControl("uPanel") as Panel;
                    //then enable the panel
                    pnl.Enabled = true;
                }
            }

    Thursday, August 10, 2017 3:26 PM
  • User1216627406 posted

    I have tested your code in isolation but unfortunately, it does what mine does.

    I created a completely new project and the only page in it is the page that contains your code.

    Initially, uPanel is disabled.

    You click Used, it is enabled but that's it.

    You cannot re-disable it.

    I think I made this complaint before but is so amazing that I can do this without Gridview or Repeater easily.

    In fact, I could easily have done this in jquery if I wasn't using Repeater.

    I don't know what it is about Repeater that makes it so difficulty to manipulate  tasks such as the one I am struggling with now.

    And I have about a total of 4 similar situations.

    I was hoping to solve this and use the code to solve the remaining three.

    Thursday, August 10, 2017 7:08 PM
  • User2103319870 posted

    Initially, uPanel is disabled.

    You click Used, it is enabled but that's it.

    You cannot re-disable it.

    So you need to enable the panel if user selects "used" and disable the panel if user selects "new". If that is the case then you just need add another condition to selected index changed event like below

     protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
            {
                //Find the radiobutton list control
                RadioButtonList rdolist = sender as RadioButtonList;
                //Find the repeater Item 
                RepeaterItem item = rdolist.NamingContainer as RepeaterItem;
                //Check if radiobuttonlist selected control is "Used"
                if ((item != null) && (rdolist.SelectedItem.Text == "Used"))
                {
                    //if so then find the panel control
                    Panel pnl = item.FindControl("uPanel") as Panel;
                    //then enable the panel
                    pnl.Enabled = true;
                }
                else if (rdolist.SelectedItem.Text == "New")
                {
                    //if so then find the panel control
                    Panel pnl = item.FindControl("uPanel") as Panel;
                    //then disable the panel
                    pnl.Enabled = false;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 10, 2017 7:28 PM
  • User1216627406 posted

    A2H, Many thanks for your help. I really appreciate it.

    It is working.

    The only issue is that the dropdownlist is not getting disabled which is weird but other than that, it is working.

    Your version of dropdownlist was not dynamically getting populated which makes sense because there was no where sName and sID was being bound.

    That's probably why your dropdownlist was getting disabled.

    UPDATE: Sorry A2H, I misspoke about dropdownlist not being disabled. It is. I misspoke because it is didn't change color like the rest.


    Thanks for your help.

    Thursday, August 10, 2017 9:56 PM
  • User2103319870 posted

    because it is didn't change color like the rest.

    You can use css to style to dropdownlist appear in grey color which resembles disabled control.

    Add the below css to your page

     <style>
            .disabledcss
            {
                background-color: #F9F9F9;
                color: blue;
                border: 1px solid gray;
                color: Gray;
            }
        </style>

    You can apply css to dropdownlist from css like below

                Dropdownlist1.CssClass = "disabledcss";
    

    Friday, August 11, 2017 1:05 AM
  • User1216627406 posted

    Thanks very much.

    Friday, August 11, 2017 2:58 PM