Show and hide labels with assosicatedControls and their controls - c#

I want to show and hide a label and its control. I can do this in c# in the code behind. But, I can only show/hide the control. Any ideas?
<asp:label AssociatedControlID="thisLabel" runat="server">This:
<asp:label ID="thisLabel" CssClass="ascontrol" runat="server" />
</asp:label>
I want to be able to show and hide that whole thing depending on what user gets to the page. I just need to know how to show/ hide that whole thing in the c# code behind...cannot seem to get the visibility of the wrapper label to go away.

You haven't supplied a server-side Id:
<asp:Label ID="label_MyControl" AssociatedControlID="txt_MyControl" runat="server" />
<asp:TextBox ID="txt_MyControl" runat="server" />
What you've done is nest a asp:Label control within another asp:Label control....

Since I normally hide more than one field contiguously, I tend to wrap the whole thing in an asp:Panel and hide the panel. However, that's just my particular usage. But since it's my usage, I tend to block those sorts of things out into panels even for something as simple as your example.
Just my nickel's worth, your mileage may vary, as always.

It should work if you get you r markup correct, like this:
<asp:Label ID="lblYear" runat="server" Text="Year (yyyy):"
AssociatedControlID="txtYear"></asp:Label>
<asp:TextBox ID="txtYear" runat="server" Columns="30" MaxLength="4"></asp:TextBox>
Then in the code behind you could have:
lblYear.visible = False
txtYear.Visible = False
Now, my understanding of the "AssociatedControlID" property of an asp:label is mainly for accessibility purposes. You don't need to have the AssociatedControlID value set to make things work as I've shown.

Related

I'm using a repeater to dynamically add labels to an asp.net page, but the text is stacking vertically for some reason. Anyone know why this happens?

Output
Code
<asp:Repeater ID="rptPBM" runat="server">
<ItemTemplate>
<asp:Label runat="server" ID="lblPBM" EnableViewState="false" Text='<%# Container.DataItem.ToString() %>'/>
<br />
</ItemTemplate>
</asp:Repeater>
I'm trying to add labels to a page dynamically using the above repeater, but when they are added it stacks vertically. Anyone ever encounter this before?
You are adding a line break after each label (br tag). Was that intended?
Remember that for each item in your data set it will write out that whole statement between the tags.
Maybe try writing out the html manually until you get it how you like and then put it into the item template.
Found the problem, the DataSource and DataBind() were in a loop so it was adding one letter at a time. Once I moved them out, it works fine.

Format label text inside UpdatePanel via front end

I have this label that get's its text from code behind. Due to the complexity of the code behind but mostly - not suppose to change any code behind - need to modify the label text on the front-end so it will look like currency.
Here is a label so far that sits inside the update panel:
<asp:Label ID="txtRFee" name="txtRFee" runat="server"></asp:Label>
This is what I added to the label:
<asp:Label ID="txtRFee" name="txtRFee" runat="server" Text="<%# Eval("txtRFee", "{0:c}") %>"></asp:Label>
It always comes out blank. Since it is sitting inside the asp:UpdatePanel inside the Eval when giving it ID need to specify that it sits inside the UpdatePanel but I tried pretty much everything. it will not compile. Tried txtRFee.ClientID etc
Text on the label comes out but Eval is being ignored. Any help is appreciated!
It looks like quotes are your problem. Change the outer quotes aroung the Text attribute to single quotes, like this:
<asp:Label ID="txtRFee" name="txtRFee" runat="server" Text='<%# Eval("txtRFee", "{0:c}") %>'></asp:Label>
There's a comment on this question that address the need for single quotes around an Eval() that uses double quotes. Good luck.

How to avoid AutoPostBack in a FormView?

I have an inherited ASPX page which has a FormView within a Form tag.
ie:
<body style="margin:0px;" bgcolor="skyblue">
<form id="form1" runat="server" action="Default.aspx">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="background-color: #87ceeb; vertical-align: top; text-align: center;">
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert" DataSourceID="SqlDataSource1"
OnItemInserting="FormView_Inserting" OnItemInserted="FormView_Inserted" BackColor="#00C0C0" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px">
<InsertItemTemplate>
etc
etc
As you can see I am using Ajax because further down the form there are CalendarExtender controls to handle dates.
The date textbox fields all have AutoPostBack="true" because the program would do nothing on the server side. ie: no events were firing. This was happening across all browsers - IE, FF and Chrome. Don't care about Safari, etc at this stage.
The point is I think I have fallen into "autopostback hell", where too many controls that do validation and other control manipulation have AutoPostBacks set to "true".
What is the better way to handle situations where controls such as textboxes, dropdownlists, etc need to perform actions before the form is SUBMITted? I would assume this is a common requirement in any form development project.
I saw somewhere (link) that wrapping FormView controls inside an UpdatePanel and ContentTemplate avoid the use of AutoPostBack. Can someone please explain some more on that?
Thank you
UPDATE
Setting my controls to AutoPostBack="true" is not a solution, because it upsets other areas of my DetailsView control and they do a postback as well. Again I suspect I have to wrap the formview inside something like an UpdatePanel to avoid autopostback entirely or do the data manipulation entirely in JScript, which I hate. But I may not have any choice.
It depends on what kind of validation you are doing. If you are doing simple validation (like required fields or regular expression validation) then you can just do the validation in javascript; no post back is required. There's even an <asp:RequiredFieldValidator> and <asp:RegularExpressionValidator> that you can use.
If you need to validate your data against the database, then you need to make a server call somehow. If you don't want to do a full page refresh, then you need to use AJAX. The easy way is to wrap each field that needs server-side validation in an <UpdatePanel>. For example:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
ChildrenAsTriggers="True" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" />
<asp:Label ID="Label1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Even though AutoPostBack is true, it will only update the contents of the <UpdatePanel>, so it won't disrupt your other content.
As a note, UpdatePanels will still post all of your form data with each partial post back. (The benefit it that the response only updates the contents of the UpdatePanel, not the entire page.) What this means is that if you have a large form with a lot of fields, you could be sending a lot more data than you want over the wire, because the AJAX post back will post the value of every field back to the server, when you really only need the value of one field. To avoid this, you can use page methods instead of update panels. However, they are a lot more work since you need to write a bunch of javascript.

Can't pass 'complex objects' as parameters in Design view

I've created my own Validation controllers, to work with my own User Controls.
My Problem :
the classic validators accept parameters in 'design view', e.g.
<asp:RequiredFieldValidator ControlToValidate="txtFirstname" runat="server" />
and I'd like to do the same in my controls, but it appears when I pass 'ControlToValidate' a control ID, I get this error :
Cannot create an object of type 'System.Web.UI.WebControls.WebControl'
from its string representation 'txtFirstname' for the
'ControlToValidate' property.
What 'pattern' do I need to implement to ensure I can make the most of my 'ascx' page instead of having to hook up everything in my 'ascx.cs' code-behind page.
p.s. I'm calling the 'tags' I create on the 'ascx' page 'design view', but I think that is probably the wrong term, which I suspect is half the reason I cant find anythign on google for this.
Your code simply should look something like this (all in the .ascx):
<asp:TextBox id="txtFirstName"
Text="Enter a value"
runat="server"/>
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
ControlToValidate="txtFirstName"
Text="Required Field!"
runat="server"/>
I'm guessing that you're not placing the RequiredFieldValidator in the same scope - causing this problem.

Need help with error- The name 'lnk1' does not exist in the current context

This is my link button:-
<asp:LinkButton ID="lnk1" Text="Set as Default" runat="server" Visible="false" OnClick="lnk1_OnClick"></asp:LinkButton>
In Code Behind I am simply making it visible
lnk1.Visible = true;
I have checked the IDs over n over..whats wrong ? Intellisense wont detect it either..I am doing something really silly..just cant figure what it is ..help!
I even restarted Visual Studio..still same error
Is the contol part of another control template? E.G. part of a repeaters ItemTemplate etc?
Update:
Since OP has said it's part of a repeaters ItemTemplate, just thought I'd explain what to do (Even though OP has sorted it)
You need to call FindControl on the Repeater, or Controls.OfType() depending on the situation, to get the control.
ASP:
<asp:Repeater runat="server" ID="rptrTest">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtBxName" />
<asp:CheckBox runat="server" ID="chkBx1" />
<asp:CheckBox runat="server" ID="chkBx2" />
</ItemTemplate>
</asp:Repeater>
C#
IEnumerable<CheckBox> chkBoxes = rptrTest.Controls.OfType<CheckBox>();
TextBox txtBxName = (TextBox)rptrTest.FindControl("txtBxName");
What I'll often do for commonly used controls (though wether it's a good idea or not I'm sure someone will now let me know), is create a member which executes this code.
private TextBox _txtBxName;
public TextBox txtBxName {
get {
if (_txtBxName == null) {
_txtBxName = (TextBox)rptrTest.FindControl("txtBxName");
}
return _txtBxName;
}
}
Sometimes the designer class is not re-generated correctly. Things you can try:
select the line, cut, save, rebuild,
paste back, save
delete the designer
.cs file, right click the aspx,
convert to web application -> this
will generate the designer class from
scratch
Since I do not have the rights to comment; so ...
In which event you are making that item visible? try doing that in PageLoad.
Can you show your markups?
Alternatively, you can try to Find the control.

Categories

Resources