How can I access custom Textbox attributes in ASP.Net? - asp.net

I am using/abusing CSS classes and custom html attributes to provide default data to a set of textboxes. The code-front for this looks like the following (with some supporting javascript to handle checking/setting the default data when the field is blank):
<asp:TextBox ID="TXT_LenderName" class='defaultText' data-default='Institution Name' runat="server"></asp:TextBox>
This works.
I am working on the code-behind to process this form. I would like to be able to compare the value of the TXT_LenderName.Text to the value of the data-default attribute, but I haven't been able to find a way to get the value of a custom html attribute. Suggestions?

This is tested and worked
string customAttrDataDefault = TXT_LenderName.Attributes["data-default"];
txtpassword.Attributes.Add("value","Password value");

try this:
TXT_LenderName.Attributes["AttributeName"]= value;//here get or set the value.

If the control, like the TextBox control inherits from the System.Web.UI.WebControls.Control class then it should have an Attributes property which is a name value pair collection of the control's attributes.

Related

How can I add properties/attributes to an HTML markup container tag in ASP.Net

I've seen all of the usual pages with information about how to create a sub-tag that allows content within a user control (using ITemplate and INamingContainer) but I've yet to see anyone able to add properties that become attributes to said tags, for example:
<asp:MyControl runat="server" ID="myControlTest" SomeAttribute="SomeValue">
<Content ContentAttribute="Something">
Blah
</Content>
</asp:MyControl>
If you see the ContentAttribute on the Content tag, that is what I'd like to be able to achieve, but if I set it all up using ITemplate and INamingContainer etc, I can add a property that does in fact appear in Intellisense for that tag but when I run the code, it says Content does not have property/attribute named ContentAttribute (it also gives the same as a warning in VS IDE but still allows me to compile it).
I have tried everything to make this work and so far the only way seems to be if I make the Content property on MyControl a class that inherits from System.Web.UI.Control and implements ITemplate. That works but unfortunately I have to specify the runat attribute on the Content tag (because it sees it as a control rather than a sub-tag) and I'd rather not do that if possible.
Hope I have explained this well enough, if I haven't please let me know and I'll do my best to elaborate further.
Thanks in advance.
I think what you're proposing is something like a MIME email where there are a variable number of sections, each with an identifier for the client to choose the best version of the email it can handle. I assume you're wanting to select the appropriate template at runtime, based on that attribute.
The standard .NET controls don't implement that way, so far as I can tell. Think of the Repeater which has:
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>...</HeaderTemplate>
<ItemTemplate>...</ItemTemplate>
<FooterTemplate>...</FooterTemplate>
</asp:Repeater>
Each of the subitems (templates) has a different name, not the same name with a separate attribute.
Is there any way for you to define, ahead of time, what all of the possible sections might be, the way the repeater does?
<asp:MyControl runat="server" ID="myCtlTest">
<SomethingTemplate>Blah</SomethingTemplate>
<OtherTemplate>Blah</OtherTemplate>
</asp:MyControl>
I'm guessing not but wanted to throw it out there in case.
Alternately, could the ContentAttribute move to MyControl? The SETter would then load/build the template for you depending on the value.
<asp:MyControl runat="server" ID="myCtlTest" ContentAttribute="Something">
<Template></Template>
</asp:MyControl>
...or it could be loaded with a method instead of using the property SETter.
If you will always need multiple templates, perhaps a combination of those two concepts would help.
<asp:MyControl runat="server" ID="myControlTest"
SomethingTemplate="Something"
OtherTemplate="Other">
<SomethingTemplate></SomethingTemplate>
<OtherTemplate></OtherTemplate>
</asp:MyControl>

how to set user control style attribute?

how can i set style attribute in code behind c#?
thanks
niall
Basically like this
Control.Style.Add("background-color", "red");
Or like this for any other attribute:
Control.Attributes.Add("style", "color: red;");
A user control isn't directly converted to a single HTML object - it is a collection of objects grouped together, therefore you can't set its "style".
If you want to hide it, then it should have a Visible attribute which you can set to false, however, this means that it won't be rendered to the page at all, and so subsequently can't be made visible in client code.

How to conditionally make a DetailsView field read-only?

Say I have a DetailsView with a bunch of fields, and I want only certain kinds of users to edit a few of them. They're too few to split them into another DetailsView, so what I'm thinking is to find some way to only allow a user to edit them based on some code-behind logic, effectively making them read-only at will.
I feel it's important to mention that the fields are both TemplateFields, not normal BoundFields with ReadOnly properties.
Any ideas? For some reason the required functions don't jump at me from reading the documentation.
Oh and I need eveyone to see their specific values, I just want to restrict edit access to them.
Hrm apparently it was as simple as setting the EditItemTemplate property of the fields in question to null. Seems to be working fine so far!
Edit: A short code sample showing how I did it:
foreach (DataControlField field in dvDRDetails.Fields)
if (!fieldstoignore.Contains(field.HeaderText))
if (field is TemplateField)
((TemplateField)field).EditItemTemplate = null;
else if (field is BoundField)
((BoundField)field).ReadOnly = true;
Where fieldstoignore is an array of field headers that I always have set as editable. The rest fall in two categories: TemplateField that require the hack I discussed above and BoundField that have a ReadOnly property I can set.

Cannot access custom properties on nested user control

Ok, please understand the architecture here first.
OurMasterPage.aspx has user control Header.ascx
Header.ascx has a user control in it called LandingPageTopNav
LandingPageTopNav.ascx has a public property named "LandingPage" that is there to be able to set by the user using this control.
And I have a Third.aspx page in which I need to set the LandingPageTopNav property to a LandingPage object
The problem is that I can't get this to work in my ThirdPage.aspx.cs:
Master.LandingPageTopNav.LandingPage = this.landingPage;
Master.LandingPageTopNav.Visible = true;
And that is, I can't get the first line to work where I'm trying to reference the LandingPage property. The second line is fine. My Third.aspx definitely can reference my master page objects otherwise from code-behind.
I'd venture to guess that the LandingPageTopNav property of OurMasterPage doesn't return a value typed as LandingPageTopNav. It probably returns the correct control typed as something more generic (e.g. Control); which is why setting the Visible property works but not the LandingPage property.

Difference between assigning a property and adding a attribute in a asp.net control

I am creating ASP.NET textbox controls dynamically. I want to know the difference between assigning the property of the control and adding it as an attribute.
For ex:
I can do:
TextBox txtBox = new TextBox();
txtBox.MaxLength = 100;
or I could do
txtBox.Attributes.Add("maxlength", "100);
The first example is strongly typed, so the compiler will check to make sure that a) MaxLength exists and b) an integer is set for that property.
The second example will work, but the compiler has no way to check to see if the attribute you are adding is correct.
In the end they get translated to the maxlength HTML attribute. TextBox offers a property mostly for convenience.
First one is better because of misspelling or wrong using.
Also first one is more readable.

Resources