asp.net - auto-mate/CheatSheetWiki GitHub Wiki
Using VS2019
- Create a new project
- Select ASP .NET Web Application (.NET Framework)
- Name aspNetWebApp
- Create
- Select Web Forms Option ( and HTTPS Options as required )
- Run/F5 to see the basic framework on a dev server
- this page is called default and can be edited as required
This can be standalone or use a template (e.g. siteMaster)
- Adding the new page (No Template then Template Added later)
-
In Solution Explorer
-
Under Solution 'aspNetWebApp' (1 of 1 project) select aspNetWebApp
-
Right Click and add web form
-
Enter name e.g. T1
-
Test Page by entering "
Hello
" between the div on the new page. and Press F5.
-
-
Previous Page = T1.aspx
-
Add Master PageFile="~/Site.Master" to <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="T1.aspx.cs" Inherits="aspNetWebApp.T1" %>
-
T1.aspx should now look like the below
<%@ Page Title="?" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="T1.aspx.cs" Inherits="aspxSqlServer.T1" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div> <p>Hello</p> <div> </asp:Content>
-
Open SiteMaster
-
Copy
<li><a runat="server" href="~/About">About</a></li>
-
Paste and change to
<li><a runat="server" href="~/T1">T1</a></li>
// Change <p>Hello</p> to
<p>Hello<%: DateTime.Now.Date %></p>
-
Add the following in T1.aspx.cs
protected string retMyString()
{ return "from function retMyString()"; } -
Add the following in T1.aspx
<%: retMyString() %>
if ( this.Page.Request.Form.AllKeys.Length > 0 )
{
// Just Look at first posted item
//////////////////////////////////
string postDataName = this.Page.Request.Form.GetKey( 0 );
string[] postDataVal = this.Page.Request.Form.GetValues( 0 );
}
-
Select menu options view/SQL Server Explorer
-
Right click add sql server
-
Select DB and connect
-
In the properties get the connection string and copy
(something like) DataSourse=...;Integrated Security=True etc... -
In web.config add
-
In the page to display the data add (change ID "GV" as required)
<asp:GridView ID="GV" AutoGeneratecolumns="true" runat="server"> </asp:GridView>
-
In the associated aspx.cs file add
// *** Add this inside the Page_Load function *** //Connection String DataBaseConnectionString string dbConStr = ConfigurationManager.ConnectionStrings["<YOUR_CON_NAME>"].ConnectionString; var queryStr = "SELECT * FROM ETC..."; var dbConn = new SqlConnection(dbConStr); var dbAdpt = new SqlDataAdapter(queryStr, dbConn); var comBld = new SqlCommandBuilder(dbAdpt); var ds = new DataSet(); dbAdpt.Fill(ds); // change GV for the id you used (GV will only appear in intellisense/be valid if GV has runat="server" attribute set) GV.DataSource = ds.Tables[0]; GV.DataBind();
-
In the aspx page select the Design view (bottom left of code pane)
-
You can click on the grid select "Auto Format..." from a popup menu to style
-
This menu also has an "Edit Columns..." option to Add Buttons/Paging and Sorting Options
- Add Web form with master page (choose page to add this to)
- Add <asp:GridView id="your id" runat = "server"> to new page between asp:Content tags
- Go into design
- Click > on gridview
- Choose Data Source or pick new datasource
-
Follow instructions
-
Set up / paging/sorting/format options as required
-
In edit columns rename etc as required
-
Run f5
-
In edit columns add
-
Command field Edit/Update/Cancel;
-
(changes will fail unless update commands are added to asp:SqlDataSource e.g.)
SelectCommand="SELECT * FROM [SOME_TABLE]" UpdateCommand="UPDATE [SOME_TABLE] SET fieldNameToUp01=@fieldnameToUp01,... where firldnameForId=@firldnameForId"; DeleteCommand="<ADD_SQL>"; InsertCommand="<ADD_SQL>";
-
If setting a field as readonly add it as
- DataKeyNames="col1" or DataKeyNames="col1,col2" etc in asp:gridview
-
Copy T1 to T2 fo manual version
-
remove content so only below remains
<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="T2.aspx.cs" Inherits="aspNetWebApp.T2" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
• View code and change to class T2 and Page_load to protected void Page_Load(object sender, EventArgs e) { string dbConf = ConfigurationManager.ConnectionStrings["<YOUR_STRING>"].ConnectionString; var qry = "SELECT * FROM [<YOUR_DATA_TABLE>]"; var dbCon = new SqlConnection(dbConf); var dbAdpt = new SqlDataAdapter(qry, dbCon); var comBld = new SqlCommandBuilder(dbAdpt); var ds = new DataSet(); dbAdpt.Fill(ds); string outStr = "<table>"; int rw = ds.Tables[0].Rows.Count; for ( int n = 0; n < rw; n++ ) { //string d1 = ds.Tables[0] // assumes just 2 cols DataRow x = ds.Tables[0].Rows[n]; // .Field(n,0) //.Field[0]; outStr += "<tr><td>" + x.ItemArray[0].ToString() + "</td>"; outStr += "<td>" + x.ItemArray[1].ToString() + "</td></tr>"; } outStr += "</table>"; /* barry has to be runat="server" to appear in intellisense / be valid etc */ Barry.InnerHtml = outStr; }