Posts

Showing posts from May, 2009

ASP.NET Snippet: Delete All Users and Roles

Deleting all users and related data from a site using ASP.NET authentication is fairly simple. Useful if copying a database and you want to remove all the users, but keep the structure and other data (e.g. user content) intact. Delete all users: foreach (MembershipUser u in Membership.GetAllUsers()) { Membership.DeleteUser(u.UserName, true); } Delete all roles: foreach (string role in Roles.GetAllRoles()) { Roles.DeleteRole(role); }

ASP.NET Snippet: Quick Password Reset

Here is a simple way, via code to reset a password when you are using the built-in ASP.NET authentication system. Useful if you either don't have a reset password form, or you just want to quickly change a password. Create a blank page, and place in the code behind Page_Load event. C# protected void Page_Load(object sender, EventArgs e) { MembershipUser u = Membership.FindUsersByName("Username")["Username"]; u.UnlockUser(); u.ChangePassword(u.ResetPassword(), "newpassword"); } VB Public Sub Page_Load(sender As Object, e As EventArgs) Dim u As MembershipUser = Membership.FindUsersByName("Username")("Username") u.UnlockUser() u.ChangePassword(u.ResetPassword(), "newpassword") End Sub Just delete the page when done. Update (8 July 2009) : If a question and answer is required when you create a user, you have to pass on the answer to u.ResetPassword , e....

Get ListControl values (CheckBoxList, RadioButtonList, DropDownList, ListBox) from a Repeater the easy way (ASP.NET C#)

Building on Get TextBox values from a Repeater the easy way (ASP.NET C#) , here is a function that will get the selected values from WebControls that inherit from ListControl ( CheckBoxList , RadioButtonList , DropDownList and ListBox ) It returns null if the ListControl matching the given ID does not exist in the repeater or an empty string array if nothing is selected. public string[] ListControlValues(RepeaterItem itm, string controlId) { string[] output = null; ListControl t = itm.FindControl(controlId) as ListControl; if (t != null) { ArrayList ar = new ArrayList(); foreach (ListItem li in t.Items) { if (li.Selected) ar.Add(li.Value); } output = (string[])ar.ToArray(typeof(string)); } return output; } Use in Repeater ItemCommand protected void MyRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e) { switch (e.CommandName.ToString()) { case "Save": int recordID = Convert.ToInt32(e.CommandArgument); string[] listBoxValues = L...

Get TextBox values from a Repeater the easy way (ASP.NET C#)

The asp:Repeater control allows you to attach an ItemCommand event to it, for instance, updating a record in a database. <asp:Repeater ID="MyRepeater" DataSource='<%# MyData %>' runat="server" OnItemCommand="MyRepeater_ItemCommand"> <HeaderTemplate><ul></HeaderTemplate> <ItemTemplate><li> Url: <asp:TextBox ID="Urdl" Text='<%# Eval("Url") %>' runat="server" /> Text: <asp:TextBox ID="Text" Text='<%# Eval("Text") %>' runat="server" /> <asp:Button ID="SaveButton" CommandName="Save" CommandArgument='<%# Eval("RecordID") %>' Text="Save" runat="server" /> </li></ItemTemplate> <FooterTemplate></ul></FooterTemplate> </asp:Repeater> MyRepeater_ItemCommand is then defined: protected void MyRepeater_ItemC...

Binding data to a Repeater using Lazy Loading (ASP.NET C#)

Lazy loading is a method of only loading data as and when you need it. Rather than loading it on page load, you can define a property that can then be bound to a WebControl. For example, in the page is an asp:Repeater : <asp:Repeater ID="MyRepeater" DataSource='<%# MyData %>' runat="server"> <HeaderTemplate><ul></HeaderTemplate> <ItemTemplate><li><a href="<%# Eval("Url") %>"><%# Eval("Text") %></a> (Record ID: <%# Eval("RecordID") %>)</li></ItemTemplate> <FooterTemplate></ul></FooterTemplate> </asp:Repeater> MyData in the DataSource attribute of asp:Repeater is a property defined in the CodeBehind page: private DataTable _MyData; public DataTable MyData { get { if (_MyData == null) { _...