Asp.Net: Databinding a array of strings

by Pieter Brinkman 30. January 2009 03:47

You can use a string array as datasource and view the string values by using the Container.DataItem property.

Code example

Codebehind:

string[] testData = {"1","two","3","4"};
rptDemo.DataSource = testData;
rptDemo.DataBind();


And in the .aspx:

<asp:Repeater runat="server" ID="rptDemo">
    <ItemTemplate>
        <%# Container.DataItem %>
    </ItemTemplate>
</asp:Repeater>
Categories: ASP.Net | Controls

Asp.Net : Setting the DefaultButton for the LoginControl

by Pieter Brinkman 22. January 2009 08:01

You can set the DefaultButton property of the Login control by putting the Login control in a Panel and set the DefaultButton property of the panel to the LoginControlId$LoginButton.

Code example:

<asp:Panel ID="panelLogin" runat="server" 
           DefaultButton="Login1$LoginButton"> 
     <asp:Login ID="Login1" runat="server" />
</asp:Panel>

 

Cheers,
Pieter

Categories: ASP.Net | Controls

Linq to Sql: Retrieve properties from related data (LoadWith)

by Pieter Brinkman 15. January 2009 06:04

You have to specify which related-data you want to retrieve from a object so you can access them outside the Linq data-context. You can achieve this by using the LoadWith method of the DataLoadOptions Class. The LoadWith method accepts an lambda expression that specifies which object you want to retrieve.

In the following example I have a employee table that has a relation with the company table. In my code I want to show the employee with the company name (outside the DataContext).

 

Employee employee;

using (LinqDataContext db = new (LinqDataContext())
{
   DataLoadOptions dlo = new DataLoadOptions(); 
   dlo.LoadWith<Employee>(e => e.Company);
   db.LoadOptions = dlo;

   employee = from item in db.Employees
                      select item).First<Employee>();
}

string companyName = employee.Company.Name;

 

Because of the DataLoadOptions I can now use the company properties to print the company name outside the DataContext.

Enjoy.

 

Categories: ASP.Net | Linq | MSSQL

Regex: Check Asp.Net Membership is valid.

by Pieter Brinkman 14. January 2009 04:55

The following RegularExporessionValidator checks if the text-input is correct format for a user-name.

 

<asp:RegularExpressionValidator runat="server" ID="userNameRegexVD" ControlToValidate="userNameTxt" ValidationExpression="^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$" ErrorMessage="Invalid username" />

 

Categories: ASP.Net

Membership: Update Password MembershipUser (ChangePassword)

by Pieter Brinkman 13. January 2009 04:32

You can change the password of a MembershipUser object in C# with the ChangePassword method. The only problem of this method is that it needs the old password(string) as input parameter and I only have the hashed version off the password. To get the old password you can user the MembershipUser.ResetPassword() method. This method will reset the password and returns the new password as string.

 

MembershipUser user = Membership.GetUser(userNameTxt.Text);
user.ChangePassword(user.ResetPassword(), passwordTxt.Text);

 

Don't for get to check that the enablePasswordReset setting is true in the web.config.

 

    <membership>
      <providers>
        <remove name="AspNetSqlMembershipProvider" />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
        connectionStringName="SiteSqlServer"
        enablePasswordRetrieval="false"
        enablePasswordReset="true"
        requiresQuestionAndAnswer="false"
        applicationName="/"
        requiresUniqueEmail="false"
        minRequiredPasswordLength="3"
        minRequiredNonalphanumericCharacters="0"
        passwordFormat="Hashed"
        maxInvalidPasswordAttempts="5"
        passwordAttemptWindow="10"
        passwordStrengthRegularExpression=""/>
      </providers>
    </membership>

 

Goodluck!

Categories: ASP.Net

Microsoft Tag: Snap my vCard

by Pieter Brinkman 8. January 2009 02:46

When surfing the internet I start reading about Microsoft Tag Beta (of course). With Microsoft Tag you can create links with a image-pattern. The image-pattern can be decoded to a link with the Microsoft Tag program on your Windows Mobile (with camera).

You can create graphical image-patterns with the following actions:

  • Link to an URL
  • Free text
  • vCard
  • Dialer

It looks very promising. Not every tag on a screen gets recognized at once, but the printed tags work very well.

I've placed my vCard in a tag on my blog. Just try to tag it ;-)

Categories: Microsoft