Change CSS classes from code

I’ve taken AnthonyWJones original code and amended it so that it works no matter what scenario: static class WebControlsExtensions { public static void AddCssClass(this WebControl control, string cssClass) { List<string> classes = control.CssClass.Split(new char[] { ‘ ‘ }, StringSplitOptions.RemoveEmptyEntries).ToList(); classes.Add(cssClass); control.CssClass = classes.ToDelimitedString(” “); } public static void RemoveCssClass(this WebControl control, string cssClass) { List<string> … Read more

is there an authorizeattribute equivalent to just standard web forms (not MVC) for .net

You can set this up in web.config with the authorization element. <configuration> <system.web> <authorization> <allow roles=”domainname\Managers” /> <deny users=”*” /> </authorization> </system.web> </configuration> Basically domain groups are translated into roles when using <authentication mode=”Windows” />. You can read more about it on MSDN

Find a control in a webform

The issue is that FindControl() does not traverse certain control children such as a templated control. If the control you are after lives in a template, it won’t be found. So we added the following extension methods to deal with this. If you are not using 3.5 or want to avoid the extension methods, you … Read more

Populate TreeView from DataBase

It will probably be something like this. Give some more detail as to what exactly you want to do if you need more. //In Page load foreach (DataRow row in topics.Rows) { TreeNode node = new TreeNode(dr[“name”], dr[“topicId”]) node.PopulateOnDemand = true; TreeView1.Nodes.Add(node); } /// protected void PopulateNode(Object sender, TreeNodeEventArgs e) { string topicId = e.Node.Value; … Read more

submit disabled fields

You can make your fields readonly, this will disallow changes to the value of your controls. Edit: You could easily write a “serializeDisabled” function, iterating over the disabled form elements which have a name attribute and using the jQuery.param function at the end, to generate the serialized string: (function ($) { $.fn.serializeDisabled = function () … Read more

DropDownList AppendDataBoundItems (first item to be blank and no duplicates)

Instead of using AppendDataboundItems=”true” (which will cause the problem you are talking about), respond to the DataBound event for the DropDownList and then add your “blank” item to the top of the list. <asp:DropDownList runat=”server” ID=”MyList” ondatabound=”MyListDataBound”></asp:DropDownList> Then in your code behind: protected void MyListDataBound(object sender, EventArgs e) { MyList.Items.Insert(0, new ListItem(“- Select -“, “”)); … Read more

ASP.NET Web Forms and ASP.NET Web Pages

There are three flavors of ASP.NET Full and there is also ASP.NET Core (the new one that works on Linux and Mac). For ASP.NET Full The first one is the oldest and is called Web Forms. Basically it is a high-level component-oriented web framework that works with controls like buttons and grids that encapsulate behaviour … Read more

After add MapPageRoute to an asp.net mvc project, the site stops to enter in Home Controller

Solved! So, we need to add a route contraint to the webforms route to ensure that it only catches on incoming routes, not outgoing route generation. Add the following class to your project (either in a new file or the bottom of global.asax.cs): public class MyCustomConstraint : IRouteConstraint{ public bool Match(HttpContextBase httpContext, Route route, string … Read more