Skip to content Skip to sidebar Skip to footer

How To Hide Or Disable Radiobuttonlist And Textbox Based On Condition Of Another Two Textboxes When Edit Button Is Clicked

I have a formview with textboxes and radiobuttonlist and edit button in the order as follows '

Solution 1:

Try this one: On Edit button click event:

if (string.IsNullOrEmpty(tb1.Text) && string.IsNullOrEmpty(tb2.Text))
        {
            rbl1.Visible = false;
            tb3.Visible = false;
        }

EDIT

TextBox txt = (TextBox)FormView1.FindControl("tb1");

TextBox txt1 = (TextBox)FormView1.FindControl("tb2");

TextBox tb3= (TextBox)FormView1.FindControl("tb3");

 RadioButtonList rb1= (RadioButtonList)FormView1.FindControl("rbl1");


    if (string.IsNullOrEmpty(txt.Text) && string.IsNullOrEmpty(txt1.Text))
            {
                rb1.Visible = false;
                tb3.Visible = false;
            }
          else

              {
                rb1.Visible = true;
                tb3.Visible = true;
}

Solution 2:

I would do something like this:

 <asp:textbox id="tb1" runat="server" text='<%# Bind("DATE_2", "{0:d}") %>' />
 <asp:calendarextender id="tb1_CalendarExtender" runat="server" targetcontrolid="tb1" />

 <asp:textbox id="tb2" runat="server" text='<%# Bind("DATE_2", "{0:d}") %>' />
 <asp:calendarextender id="tb2_CalendarExtender" runat="server" targetcontrolid="tb2" />

 <asp:button id="EditButton" runat="server" causesvalidation="False" commandname="Edit" text="Edit" enabled='<%# CanEdit(Eval("DATE_2"), Eval("DATE_2")) %>'
             onclientclick="verifyEditControls();" />

 <div class="hide edit-controls">
     <asp:radiobuttonlist id="rbl1" runat="server" repeatdirection="Horizontal" text='<%# Bind("DIAG_LL_APPROVAL") %>'>
        <asp:ListItem>Approved</asp:ListItem>
        <asp:ListItem>Rejected</asp:ListItem>
        <asp:ListItemSelected="True">None</asp:ListItem>
    </asp:radiobuttonlist>
    <asp:textbox id="tb3" runat="server" text='<%# Bind("COMMENTS") %>' maxlength="1000"/>
 </div>

and add a javascript (I'm using jQuery here for the sake of simplicity)

<script>
    function verifyEditControls() {
        var c1 = '#<%= tb1.ClientID %>',
            c2 = '#<%= tb2.ClientID %>';

        if($(c1).val().length > 0 || $(c2).val().length > 0)
            $(".edit-controls").show();
        else 
            $(".edit-controls").hide();

        return $(".edit-controls").is("visible");
    }
</script>

This will disable the postback if the controls are not visible and only show the controls wrapped <div>, but if the edit button is pressed and the controls are visible, a true is sent and your page will postback to the method called on the button.

Just add a Command or Click server event for that.

If you want to use a normal button, there is no need to ask .NET Framework to create one for you, just use <button id="EditButton">Edit</button>


Solution 3:

On button click you can do a check if tb1 and tb2 are not string.IsNullOrEmpty if so then set the visibilty property of rbl1 and tb3 to false.

or you can use the OnTextChanged event of tb1 and tb2 when no text is typed in rbl1 and tb3 are hidden, when someone adds text then they will automaticly be set to visible

Btw: you need to use better naming conventions

<asp:textbox id="tb1" runat="server" text='<%# Bind("DATE_2", "{0:d}") %>' />
 <asp:calendarextender id="tb1_CalendarExtender" runat="server" targetcontrolid="tb1" />

 <asp:textbox id="tb2" runat="server" text='<%# Bind("DATE_2", "{0:d}") %>' />
 <asp:calendarextender id="tb2_CalendarExtender" runat="server" targetcontrolid="tb2" />

 <asp:button id="EditButton" runat="server" causesvalidation="False" 
        commandname="Edit" text="Edit" 
        enabled='<%# CanEdit(Eval("DATE_2"), Eval("DATE_2")) %>' 
        onclick="EditButton_Click" />

 <asp:radiobuttonlist id="rbl1" runat="server" Visible="false" repeatdirection="Horizontal" text='<%# Bind("DIAG_LL_APPROVAL") %>'>
      <asp:ListItem>Approved</asp:ListItem>
      <asp:ListItem>Rejected</asp:ListItem>
      <asp:ListItem Selected="True">None</asp:ListItem>
 </asp:radiobuttonlist>
 <asp:textbox id="tb3" runat="server" Visible="false" text='<%# Bind("COMMENTS") %>' maxlength="1000"/> 

and in code behind

protected void EditButton_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(tb1.Text) && !string.IsNullOrEmpty(tb2.Text))
    {
        rbl1.Visible = true;
        tb3.Visible = true;

        // do your stuff
    }
    else 
    {
        rbl1.Visible = false;
        tb3.Visible = false;
    }
}

Post a Comment for "How To Hide Or Disable Radiobuttonlist And Textbox Based On Condition Of Another Two Textboxes When Edit Button Is Clicked"