Code & !Code: Code: ASP.Net MVC Maintain Checkbox State

December 27, 2009 · Leave a Comment
Filed under: ASP, ASP.NET, ASP.NET 2.0, Featured 

Below is a quick and dirty way to maintain a check box checked state during postbacks with MVC:




<%= Html.CheckBox("IsSecure", (Request.Form["IsSecure"] ?? string.Empty).Contains("true"))%>

Assign Value to Asp.Net TextBox with mode="Password" | Official

September 12, 2009 · Leave a Comment
Filed under: ASP, ASP.NET, ASP.NET 2.0, Featured 
Introduction

ASP.NET server control doesn’t add/display the value in a password textbox because of security concerns. On most of the web sites at the time of registration password is asked. You must have noticed after submitting the page if you are again redirected to the page due to unsuccessful registration value of password field is lost.This is due to security concerns as everything is gets rendered on the client side So when you view the source in the browser password value will be there.

Solution 1

Add an attribute to textbox.Add this following line of code when you want set the value

TextBox1.Attributes.Add("value", "thePassword")

Above solution will work, but if a user does view source on the form, the password is visible as plain text and that is never good from a security point of view. Rather, I would use Viewstate to hold the password value and if the user doesn’t change it then retrieve the value from view state.

Solution 2

But if explicitly you want to set the value of password text box again then use following method in .NET 2.0

txtPassword.Attributes["value"] = Request.Form[txtPassword.ClientID]; //in C#.Net 2.0

Enjoy..

asp.net example: How to set change DetailsView pager font name

September 4, 2009 · Leave a Comment
Filed under: ASP, ASP.NET, ASP.NET 2.0, Featured 

How to set change DetailsView pager font name programmatically in asp.net

DetailsViewPagerFontName.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        DetailsView1.PagerStyle.Font.Name = "Comic Sans MS";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>How to set change DetailsView pager font name programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy; font-style:italic;">DetailsView Example: Change Pager Font Name</h2>
        <asp:SqlDataSource
            ID="SqlDataSource2"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="Select Top 10 ProductID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock From Products"
            CancelSelectOnNullParameter="false"
            >
        </asp:SqlDataSource>
        <asp:DetailsView
            ID="DetailsView1"
            runat="server"
            DataSourceID="SqlDataSource2"
            AllowPaging="true"
            BackColor="Wheat"
            ForeColor="Crimson"
            BorderColor="Tan"
            Width="500"
            AutoGenerateRows="false"
            HeaderText="Product Details"
            >
            <PagerStyle
                BackColor="Orange"
                Height="35"
                Font-Bold="true"
                HorizontalAlign="Right"
                Font-Size="Large"
                />
            <HeaderStyle
                BackColor="Peru"
                Font-Bold="true"
                ForeColor="Wheat"
                Font-Size="X-Large"
                HorizontalAlign="Center"
                Height="50"
                />
            <RowStyle Height="30" />
            <Fields>
                <asp:BoundField DataField="ProductID" HeaderText="Product ID" ReadOnly="true" />
                <asp:BoundField DataField="Productname" HeaderText="Product Name" />
                <asp:BoundField DataField="QuantityPerUnit" HeaderText="Quantity Per Unit" />
                <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" />
                <asp:BoundField DataField="UnitsInStock" HeaderText="Units In Stock" />
            </Fields>
        </asp:DetailsView>
        <br />
        <asp:Button
            ID="Button1"
            runat="server"
            Text="Change DetailsView Pager Font Name"
            Font-Bold="true"
            Height="45"
            ForeColor="SaddleBrown"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>

Create a Virtual Directory in IIS 7.0

March 12, 2009 · 1 Comment
Filed under: .Net, C#, Featured, IIS, VB.Net 

 

A virtual directory is a directory name, used in an address, which corresponds to a physical directory on the server. You can add a virtual directory that will include directory content in a site or an application without having to move the content physically into that site or application directory.

To add a virtual directory

You can perform this procedure by using the user interface (UI), by running Appcmd.exe commands in a command-line window, by editing configuration files directly, or by writing WMI scripts.
User Interface
To use the UI

1.

Open IIS Manager. For information about opening IIS Manager, see IIS 7.0: Open IIS Manager.
2.

In the Connections pane, expand the Sites node in the tree, and then click to select the site in which you want to create a virtual directory.
3.

In the Actions pane, click View Virtual Directories.
4.

On the Virtual Directories page, in the Actions pane, click Add Virtual Directory.
5.

In the Add Virtual Directory dialog box, type a name in the Alias box. This alias is used to access the content from a URL.
6.

In the Physical path box, type the physical path of the content folder, or click Browse to navigate through the file system to find the folder.
7.

Optionally, click Connect as to specify credentials that have permission to access the physical path. If you do not use specific credentials, select the Application user (pass-thru authentication) option in the Connect As dialog box.
8.

Optionally, click Test Settings to verify the settings that you specified for the virtual directory.
9.

Click OK.

Command Line

To add a virtual directory to the root application in a site, use the following syntax:

appcmd add vdir /app.name:string/ /path:string /physicalPath:string

The variable app.namestring is the site name and the / following the name specifies that the virtual directory should be added to the root application of the site. The variable pathstring is the virtual path of the virtual directory, such as /photos, and physicalPathstring is the physical path of the virtual directory’s content in the file system.

For example, to add a virtual directory named photos with a physical location of c:images to the root application in a site named contoso, type the following at the command prompt, and then press ENTER:

appcmd add vdir /app.name:contoso/ /path:/photos /physicalPath:c:images

To add a virtual directory to an application other than a site’s root application, type the name of the application after / in app.namestring. For example, to add the same virtual directory used in the previous example to an application named marketing in a site named contoso, type the following at the command prompt, and then press ENTER.

 

How to make Apache run ASP.NET / ASP.NET 2.0

December 15, 2008 · Leave a Comment
Filed under: Apache, ASP, ASP.NET, ASP.NET 2.0, Featured 

Even worked with ASP.NET 2.0 Site !

Following are the instruction to make Asp.Net work under apache:

– Install Apache 2.0.54

– Install Mod_AspDotNet

– Add at the end of C:Program FilesApache GroupApache2confhttpd.conf the following lines

#asp.net
LoadModule aspdotnet_module “modules/mod_aspdotnet.so”

AddHandler asp.net asax ascx ashx asmx aspx axd config cs csproj licx rem resources resx soap vb vbproj vsdisco webinfo

<IfModule mod_aspdotnet.cpp>
# Mount the ASP.NET /asp application
AspNetMount /SampleASP “c:/SampleASP”
#/SampleASP is the alias name for asp.net to execute
#”c:/SampleASP” is the actual execution of files/folders in that location

# Map all requests for /asp to the application files
Alias /SampleASP “c:/SampleASP”
#maps /SampleASP request to “c:/SampleASP”
#now to get to the /SampleASP type http://localhost/SampleASP
#It’ll redirect http://localhost/SampleASP to “c:/SampleASP”

# Allow asp.net scripts to be executed in the /SampleASP example
<Directory “c:/SampleASP”>
Options FollowSymlinks ExecCGI
Order allow,deny
Allow from all
DirectoryIndex index.htm index.aspx
#default the index page to .htm and .aspx
</Directory>

# For all virtual ASP.NET webs, we need the aspnet_client files
# to serve the client-side helper scripts.
AliasMatch /aspnet_client/system_web/(d+)_(d+)_(d+)_(d+)/(.*) “C:/Windows/Microsoft.NET/Framework/v$1.$2.$3/ASP.NETClientFiles/$4

<Directory “C:/Windows/Microsoft.NET/Framework/v*/ASP.NETClientFiles”>
Options FollowSymlinks
Order allow,deny
Allow from all
</Directory>
</IfModule>
#asp.net

– Create a directory c:SampleASP and insert in it the index.aspx

– Restart apache server :
Start-> Apache HTTP Server 2.0.54 ->
Control Apache Server -> Restart

– Open Explorer and navigate to http://localhost/SampleASP/index.aspx

If everything worked fine you should get a nice asp.net page working.

— index.aspx —

<%@ Page Language=”VB” %>
<html>
<head>
<link rel=”stylesheet”href=”intro.css”>
</head>
<body>
<center>
<form action=”index.aspx” method=”post”>
<h3> Name: <input id=”Name” type=text>
Category: <select id=”Category” size=1>
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
</h3>
<input type=submit value=”Lookup”>
<p>
<% Dim I As Integer
For I = 0 to 7 %>
<font size=”<%=I%>”> Sample ASP.NET TEST</font> <br>
<% Next %>
</form>
</center>
</body>
</html>