Click here to Skip to main content
15,357,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to hide a set of textboxex on a web form depending on the basis of value in a dropdownlist.If dropdownlist value is changed i have to hide unhide textboxes depending on my requirement.I m using a table in my page,some textboxes are common for all,i dont need to hide them.Right now i am giving id to each and hiding them from code behind.Also i m filling values in these textboxes by selecting values from an autocomplete textbox.But i am facing some issues with them.how can i do this in a proper way ?
Please help..
Posted
Comments
Ankur\m/ 19-Mar-14 3:05am
   
What issues?
pwavell 19-Mar-14 3:43am
   
issues about hide/unhide.In some cases when they need to be invisible they are visible.i am hiding complete row of table by trow1.visible=false.and i have many rows which i need to hide.
Ankur\m/ 19-Mar-14 6:23am
   
See my solution.

You can iterate through controls and hide in the following way -
C#
foreach(Control ctl in Page.Controls)
{
 ctrlType= Convert.ToString(ctrl.GetType());
 if(ctrlType== "System.Web.UI.WebControls.TextBox")
 {
  ((TextBox)ctl).Visible = false;
 }
}
   
Comments
Ankur\m/ 19-Mar-14 3:10am
   
He needs to hide "some" textboxes and not all. And he is using ID for that which is correct.
Abhinav S 19-Mar-14 3:48am
   
He can check for his conditions within this loop.
For e.g. if conditions are based on values, he can check these values and then hide / unhide accordingly.
Ankur\m/ 19-Mar-14 6:27am
   
Not sure but I think we do not need to loop over all the controls on the page for this. Actually the question is not very clear. By what I understand it can easily be done on the client side.
You can do this on client side very easily as well. Post only if you need some data from Server otherwise hide/show should be done on client side. Assign a common class to all those textboxes you need to hide/show. On change of dropdown, write a JavaScript function in which you select elements by class name and set their display style to none/block. The syntax will be very much easier if you are using jQuery. Try and let us know how it goes.
   
ASP.NET
<table id="tablehide"  runat="server">

<tr id="trhide"  runat="server">
<td id="tdhide"  runat="server">

</td>

</tr>

</table>


C#
Codebehind:

tablehide.visible=false;
(or)
trhide.visible=false;
(or)
tdhide.visible=false;


C#
tablehide.visible=true;
(or)
trhide.visible=true;
(or)
tdhide.visible=true;



place your usercontrol with in the table and hide the whole things.
   
v2
Comments
Ankur\m/ 19-Mar-14 3:12am
   
Seriously? Why would you need to create server side table controls for that? TextBox is already accessible on server side.
King Fisher 19-Mar-14 3:21am
   
if i'm going to hide 10 textbox .what to do?so, do i have to write like this
txt1.visible=false;
txt2.visible=false;
txt3.visible=false;
...
txt10.visible=false:

is this good coding.
Ankur\m/ 19-Mar-14 6:21am
   
And how would you not do that by creating server side table controls? Do you know, you cannot have same id for server side controls. So you will need to create as many table controls as TextBoxes you need to hide. And instead of setting TextBox.Visible you would set tableID.Visible. Can you please explain how is it different?
King Fisher 19-Mar-14 6:37am
   
both are same but we can short the lines.

if <tabel id="trtable" runat="server">
<tr >
<td>
txtbox1
</td>

</tr>
<tr>
<td>
txtbox2
</td>
</tr>
<tr>
<td>
txtbox3
</td>
</tr>
<tr>
<td>
txtbox3
</td>
</tr>
<tr>
<td>
txtbox4
</td>
</tr>
</table>

u said :
txtbox1.visible=false;
txtbox2.visible=false;
txtbox3.visible=false;
txtbox4.visible=false;

i said this:
trtable.visible=false;


Ankur\m/ 19-Mar-14 6:42am
   
And suppose my textboxes are at different places and not together. Say some in a panel, some after other controls. How do I do it then? You code doesn't help.
And since you are creating more controls you are not shortening the number lines, but increasing it. I am sorry but that's not a good answer.
King Fisher 19-Mar-14 6:49am
   
if its in different place .we cant use this.BUT IF IT's IN SAME PLACE..? what do i do?.is your ans is good?
pwavell 19-Mar-14 3:45am
   
i dont want to hide all textboxes . i m doing the same thing "trow1.visible=false/true".and i need to replace this code by some proper code.
King Fisher 19-Mar-14 3:56am
   
what do you mean proper code?

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