0

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

Learner
  • 43
  • 1
  • 2
  • 10

3 Answers3

0

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;
}
sp_m
  • 2,647
  • 8
  • 38
  • 62
  • Shows the id names(tb1, tb2,tb3 and rbl1) are not in current context. I'm using formview – Learner Jul 06 '12 at 08:07
  • @Viswa,you need to find control within your formview using findcontrol Take look at this [FormviewAnswers](http://stackoverflow.com/questions/4476168/asp-net-vb-how-to-access-controls-inside-a-formview-from-the-code-behind) – sp_m Jul 06 '12 at 09:02
  • Getting error: Object reference not set to an instance of an object. – Learner Jul 06 '12 at 09:47
  • if (!string.IsNullOrEmpty(t1.Text) && !string.IsNullOrEmpty(t2.Text)) find in c# code – Learner Jul 06 '12 at 10:48
-1

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;
    }
}
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
-1

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>

balexandre
  • 73,608
  • 45
  • 233
  • 342