How to hide or disable radiobuttonlist and textbox based on condition of another two textboxes when edit button is clicked

孤街醉人 提交于 2019-12-11 17:44:39

问题


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

 <asp:textbox id="tb1" runat="server" text='<%# Bind("DATE_1", "{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_1"), Eval("DATE_2")) %>' OnClick="EditButton_Click" />

 <asp:radiobuttonlist id="rbl1" runat="server" 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" text='<%# Bind("COMMENTS") %>' maxlength="1000"/>                                 

Need to hide or disable rbl1 and tb3 if tb1 or tb2 doesn't have any value (i.e null) when edit button is clicked.

protected void EditButton_Click(object sender, EventArgs e)
    {
        TextBox t1 = FormViewName.FindControl("tb1") as TextBox;
        TextBox t2 = FormViewName.FindControl("tb2") as TextBox;
        RadioButtonList rbl = FormViewName.FindControl("rbl1") as RadioButtonList;
        TextBox t3 = FormViewName.FindControl("tb3") as TextBox;

        //if ("".Equals(tdcd) || "".Equals(tdrcd))
        if (!string.IsNullOrEmpty(t1.Text) && !string.IsNullOrEmpty(t2.Text))
        {
            FormViewName.FindControl("rbl1").Visible = true;
            FormViewName.FindControl("tb3").Visible = true;
        }
        else
        {
            FormViewName.FindControl("rbl1").Visible = false;
            FormViewName.FindControl("tb3").Visible = false;
        }
    }                                                                             

Error: Oject reference not set to an instance of an object


回答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;
}



回答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>




回答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;
    }
}


来源:https://stackoverflow.com/questions/11357746/how-to-hide-or-disable-radiobuttonlist-and-textbox-based-on-condition-of-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!