Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi guys,

i have a 2 drop down and 2 text box like drp1, drp2, txt1,txt2.

i want when i selected a value from drp1 then txt1 will be visible set true. and after when i slect a value from drp2 then txt2 will be visible. but i got some error when i select a value from drp1 txt1 is visible after it when i selected a drp2 value txt2 is visible but txt1 visibility will be false. that all will be set on autopostback true. can i do that with autopostback TRUE.

please help!!!!!!
Posted

Hi I didn't understand what is the issue . Can you try the following code :
ASP.NET
<body>
    <form id="form1" runat="server">
    <div>
          <asp:dropdownlist id="DropDownList1" runat="server" autopostback="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged" xmlns:asp="#unknown">
                   <asp:listitem value="A">A1-Visible</asp:listitem>
                   <asp:listitem>B1-Visible</asp:listitem>
                   <asp:listitem>C1-Hide</asp:listitem>
               </asp:dropdownlist>
         <asp:dropdownlist id="DropDownList2" runat="server" autopostback="True" onselectedindexchanged="DropDownList2_SelectedIndexChanged" xmlns:asp="#unknown">
                   <asp:listitem>P1-Visible</asp:listitem>
                   <asp:listitem>Q1-Visible</asp:listitem>
                   <asp:listitem>R1-Hide</asp:listitem>
                   </asp:dropdownlist>
  
    </div>
        <asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown">TextBox1</asp:textbox>
        <asp:textbox id="TextBox2" runat="server" xmlns:asp="#unknown">TextBox2</asp:textbox>
    </form>
</body></body>


C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           //If you are not setting the TextBox1 and TextBox2 visible property to false in designer[aspx page], you can use the following code to hide the text boxes on load.
           DropDownList1.SelectedIndex = 2; //  when page load happens TextBox1 will be invisible.
           DropDownList2.SelectedIndex = 2; //  when page load happens TextBox2 will be invisible
           (DropDownList1 as IPostBackDataHandler).RaisePostDataChangedEvent();
           (DropDownList2 as IPostBackDataHandler).RaisePostDataChangedEvent();
       }
   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (DropDownList1.SelectedIndex.Equals(2))
           TextBox1.Visible = false;
       else
           TextBox1.Visible = true;
   }
   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (DropDownList2.SelectedIndex.Equals(2))
           TextBox2.Visible = false;
       else
           TextBox2.Visible = true;
   }


Regards
Dominic
 
Share this answer
 
v3
Comments
Member 8214635 16-Nov-12 13:55pm    
when i select a item from drp1 (drp1 set to autopostback true) then textbox1 is visible coz of post back it is visible. But when i select a value from drp2 then textbox2 will visible but textbox1 will invisible.Because when i select a value from drp2 again page will refresh So on page load i set a textbox will be invisible.
You got it. So, please help me again.
Dominic Abraham 16-Nov-12 14:06pm    
At the initial page load time what you are expecting ? That is, Before changing any value in Dropdownlist . Do you want see two text boxes, or text box1 only or text box2 only or both hide.
Hi
Have you checked the above code ? I think , in that example there is no issue with postback.

Step0 : By default, both TextBox will be visible.
Step1 : Select 'C1-Hide' --> TextBox1 will be invisible, TextBox2 will be visible
Step2 : select 'R1-Hide' --> TextBox1 will be invisible, TextBox2 will be invisible
Step3 : Select 'A1-Visible' -> TextBox1 will be visible, TextBox2 will be invisible
Step4 : Select 'P1-Visible' -> TextBox1 will be visible, TextBox2 will be visible

and again

Step5 : Select 'C1-Hide' -> TextBox1 will be invisible, TextBox2 will be visible

I think all the scenarios are satisfying here.What else you are looking for.

Regards
Dominic
 
Share this answer
 
Comments
Member 8214635 16-Nov-12 14:35pm    
No NO. By default all textbox is invisible.

Look, On page load textbox is invisible.

when i am select a value from drp1 then textbox1 is visible.
same as when i select a value from drp2 then textbox2 is visible.

but problem is there when i select a value from drp2 then previous action on drp1 select visible textbox1 will be discard. means textbox1 is again invisible.
Dominic Abraham 16-Nov-12 14:42pm    
HiSolution1 has improved. Please check that.
Dominic Abraham 18-Nov-12 1:31am    
If the solution is helpful, please mark it as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900