Home
Home Page
And again about functional programming on Python
Where the Python has come crawling?
Practical application OOP in PHP5
Functional programming in language Python
Approaches of language Python - an amusing example of optimization
Use ext/mysqli: Part I - the Review and the prepared expressions
How to receive the maximal result from registration in catalogues
XML-RPC In language Python
The API-specification of databases of language Python, version 2.0
Programming of the Web-client in language Python
Useful advice for job with XML
Future Web - behind semantics
Whether it is necessary to cancel spaces of names XML?
Use AJAX in ASP.NET
ASP .NET 2.0: Reference pages
Patterns of registration
What is ASP.NET. Installation and the test project.
Anatomy ASP.NET. ASP.NET in operation.
Server elements of management Continuation.
Links
 
topic


. Server elements of management Continuation.

Button


Button is a command button, pressing on which often results in sending the data on the server. It is possible to create buttons of two types: for data transfer of the form (submit button) or command buttons for performance of the various functions connected to the given button. If on the form there are some buttons, property CommandName allows to learn{find out}, which button has been pressed.


ASP .NET supports 3 kinds of events.


* Events which occur in a browser of the client and are processed by a code on Javascript.

* Events of loading of page.

* Events of elements of management.


For example, to process click on the button, we redefine event Click.



protected void Button1_Click (object sender, EventArgs e)

      {

}


Events can be defined{determined} through the tab of events to a property sheet. The second argument of all obrabotchikov events has type EventArgs or any, inherited from him{it}.


For example, we want to create the form for filling the resume. The author can have beforehand unknown to us quantity{amount} of the previous places of job. We shall add on the form the button, by pressing on which in the form one element of input of the text is added.



<% Page Language = " C * " %>

  <script runat = "server">

  static int num=0;

  static TextBox [] tb=new TextBox [10];

  void AddExperience (Object sender, EventArgs e)

  {

      if (num <10) // That there was no mistake of the reference{manipulation} to a nonexistent element of a file

      {

          TextBox newBox = new TextBox ();

          newBox. ID = "box" + num;

          tb [num] = newBox;

          num ++;

}

      for (int i=0; i <10; i ++) // Addition on the form kontrolov from masiva.

      {

          if (tb [i]! = null)

          {

          places. Controls. Add (tb [i]);

          Label lb=new Label ();

          lb. Text = " <br> <br> ";

          places. Controls. Add (lb);

}

          else break;

}

}

  </script>

  <html xmlns = « http: // www.w3.org/1999/xhtml »>

  <head>

  </head>

  <body>

      <asp:Label ID = "Label1" runat = "server" text = " Enter your last place of job "/>

      <form runat = "server" id = "Experience">

      <asp:Panel id = "places" runat = "server">

          <asp:TextBox id = "first" runat = "server"/>

          <br/>

          <br/>

          </asp:Panel>

          <asp:Button id = "Add" Text = "Still" OnCommand = "AddExperience" CommandName = "Add" runat = "server"/>

      </form>

  </body>

  </html>


Here we have a file from 10 elements such as TextBox. The new element is created at the moment of pressing button " Still{Even} ». About 10 new elements are possible to add. As before, they are placed in the container, it is necessary, that they were deduced{removed} up to the button.


By means of property OnClientClick it is possible to set the client script on JavaScript. His{its} value can be the built - in function, or described in a body of page. The client code is carried out up to the server code set in property OnClick.

Image


The element of management asp:image corresponds{meets} tegu img language HTML. It{he} can be used for dynamic addition on page of new images. We shall return to our travel agency. We have decided, that a code the client chooses cities, on page the map of corresponding city automatically should be deduced{removed}. We leave it as exercise. Maps of cities can be found through a Yandex.



<asp:Image> has svojstvaAlternateText, ImageUrl, ImageAlign

AlternateText Corresponds{Meets} to attribute ALT tega IMG. It is displayed, if show of pictures is switched - off or the picture cannot be found

ImageUrl Corresponds{Meets} to attribute SRC tega IMG

ImageAlign Corresponds{Meets} to attribute ALIGN tega IMG


As always, properties can be changed from the program. For example, changing value ImageUrl, it is possible to organize viewing of set of pictures as slide - show. Create a directory images and place in him{it} some pictures image1, image2 and so on.


Let's write new page.



<% Page Language = " C * " %>

  <html xmlns = « http: // www.w3.org/1999/xhtml »>

  <head>

  <script language = " C * " runat = "server">

  public static int count=1;

  void NextImage (Object sender, EventArgs e)

  {

  count ++;

  if (count == 10) count=1; // cyclic viewing

  Image1. ImageUrl = "images/image" +count + ".jpg ";

}

  </script>

  </head>

  <body>

     <form runat = "server">

        <h3> Image Example </h3>

        <asp:Image id = "Image1" runat = "server"

             ImageAlign = "top"

             AlternateText = " the Picture no "

             height = "300"

             ImageUrl = "images/image1.jpg"/>

        <hr>

  <br> <br>

        <asp:Button id = "Next"

             Text = "Next"

             OnClick = "NextImage"

             runat = "server"/>

  <br> <br>

     </form>

  </body>

  </html>



ImageButton


The element of management ImageButton represents a combination of elements Image and Button. It{he} can be used for creation of the images sensitive to a clique of a mousy. Click is event at which approach some is carried out kod.



<asp:ImageButton id = "imgButton" OnCommand = "SubmitPartl" runat = "server"/>


ImageButton allows to reach{achieve} the effect similar to a map of the image. Event Click allows to learn{find out} coordinates of click of the mouse and to react according to region in which the mouse has been pressed. Obrabotchik the argument such as ImageClickEventArgs - naslednikaSystem. EventArgs should accept events. He{it} has additional fields X and Y - coordinates a clique of a mousy.



protected void ImageButton1_Click (object sender,

  System. Web. UI.WebControls. ImageClickEventArgs e)

  {

  // Processing event

}


Let's return again to tourist agency. Director has called you (programmer) and has asked to make the following: on page with the image of a map of Cairo it is necessary to make so that when the user pressed a mousy in any place, the map of area which he has pressed opened. In total there are 9 areas. All of a picture have the size 300 on 300 pikselov. Areas identical, are located in table 3 on 3.


You write:



<% Page Language = " C * " %>

  <script runat = "server">

      void Magnify (Object sender, ImageClickEventArgs e)

      {

      int x=e. X;

      int y=e. Y;

        int row=y/100; // a line on which have cluck

        int col=x/100; // a column on which have cluck

        int count=row*3+col+1; // number{room} of a picture

      plan. ImageUrl = "Cairo/map-" + count + ".jpg ";

      plan. Enabled = false; // it is necessary, that other area did not open.

      instruction. Text = " Press Back to see all city ";

}

  </script>

  <html xmlns = « http: // www.w3.org/1999/xhtml »>

  <head>

  </head>

  <body>

      <form runat = "server">

          <h3> the Map of Cairo

          </h3>

          <br/>

          <br/>

          <asp:Label id = "instruction" runat = "server"> Press the mouse any area to increase a picture. </asp:Label>

          <br/>

          <br/>

          <asp:ImageButton id = "plan" onclick = "Magnify" runat = "server" width = "300" height = "300" ImageUrl = "Cairo/map.gif"> </asp:ImageButton>

          <br/>

          <br/>

      </form>

  </body>

  </html>


And more simple use creation of beautiful non-standard buttons. Property CommandName is used the same as and in sluchaet with the usual button.



<form id = "form1" runat = "server">

      <div>

          <asp:ImageButton ID = "ImageButton1" runat = "server" CommandName = "create" ImageUrl = "~/Images/1button-create.gif" OnCommand = "ImageButton_Click" OnClientClick ='alert ("clicked") ' ToolTip = " Create very nice account "/>

          <asp:ImageButton ID = "ImageButton2" runat = "server" CommandName = "add" ImageUrl = "~/Images/1button-add.gif" OnCommand = "ImageButton_Click"/>

          <asp:ImageButton ID = "ImageButton3" runat = "server" ImageUrl = "~/Images/1button-cancel.gif" CommandName = "cancel" OnCommand = "ImageButton_Click"/> <br/>

          <asp:Label ID = "Message" runat = "server"> </asp:Label> </div>

      </form>


Picture of this example is in delivery Visual Studio 2005 Microsoft Visual Studio 8Common7IDEProjectTemplatesWebCSharp1033. Copy them in folder Images of your project and add them in the project.


This function insert into a file of the separated code.



protected void ImageButton_Click (Object sender, CommandEventArgs e)

      {

          switch (e. CommandName)

          {

              case "create":

                  // Insert code to create.

                  Message. Text = "Creating";

                  break;

              case "add":

                  // Insert code to add.

                  Message. Text = "Adding";

                  break;

              case "cancel":

                  // Insert code to cancel.

                  Message. Text = "canceling";

                  break;

}

}


At the first button property ToolTip is established. See page in Internet Explorer. The window with the help will appear at prompting on this button. And Opera deduces the help for all buttons. Only for in what it is established ToolTip, he is deduced on the first line. In the second - the address.



At the button use of property OnClientClick also is shown. It sets the client script which will be executed by pressing the button without the reference{manipulation} to the server. Here it is function alert language Javascript - a call of a window with the notice.



HyperLink and LinkButton


HyperLink - a hyperlink usual or with a picture. They allow to move on a site or to allow the link to other sites.



<asp:HyperLink ID = "HyperLink1" runat = "server" NavigateUrl = "~/Customer.aspx"> HyperLink </asp:HyperLink>


The sign ~ designates the root of the current site.


LinkButton is a button which looks as a hyperlink. Pressing it{her} results to perezagruzke pages. In property PostBackUrl it is possible to set the address of page on which the search will be redirected.

BulletedList


This element of management allows to reproduce the numbered and unnumbered marked lists and adds to this many new opportunities. It is a new element ASP.NET 2.0 and too can be adhered to the data.


The type of the list is defined{determined} by property BulletStyle. The list can be numbered by figures, letters or latin numbers, or is marked with markers of different forms.



<asp:BulletedList ID = "BulletedList1" runat = "server" BulletStyle = "Numbered">

      <asp:ListItem> CHeburashka </asp:ListItem>

      <asp:ListItem> the Crocodile of the Gene </asp:ListItem>

      <asp:ListItem> SHapokljak </asp:ListItem>

      </asp:BulletedList>


Looks on page as


1.1. CHeburashka

2.2. The crocodile of the Gene

3.3. SHapokljak


And his{its} HTML-code



<ol id = "BulletedList1" style = " list-style-type:decimal; ">

              <li> CHeburashka </li> <li> the Crocodile of the Gene </li> <li> SHapokljak </li>


If property BulletStyle to change on "Circle" will be sgenenirovan the unnumbered list



<ul id = "BulletedList1" style = " list-style-type:circle; ">

              <li> CHeburashka </li> <li> the Crocodile of the Gene </li> <li> SHapokljak </li>


At value CustomImage it is necessary to set a picture in property BulletImageUrl. Elements of the list can be hyperlinks and buttons - hyperlinks.



<asp:BulletedList ID = "BulletedListLinks" runat = "server" BulletStyle = "Circle" DisplayMode = "HyperLink">

          <asp:ListItem Value = " http: // chebur.polyn.kiae.su "> CHeburashka </asp:ListItem>

          <asp:ListItem Value = " http: // gena.crocodile.net "> the Crocodile of the Gene </asp:ListItem>

          <asp:ListItem Value = " http: // chapeauclack.com "> SHapokljak </asp:ListItem>

          </asp:BulletedList>


Hyperlinks should be written down in attribute ListItem Value.


Type LinkButton transforms items{points} of the list into hyperlinks which cause perezagruzku pages. Obrabotchik pressing buttons are accepted with argument which contains the information on an index of the pressed item{point}.



<asp:BulletedList ID = "BulletedListLinks" runat = "server" BulletStyle = "Circle" DisplayMode = "LinkButton" OnClick = "BulletedListLinks_Click">

          <asp:ListItem Value = "Red"> Red </asp:ListItem>

          <asp:ListItem Value = "Blue"> Dark blue </asp:ListItem>

          <asp:ListItem Value = "Green"> Green </asp:ListItem>

          </asp:BulletedList>

      protected void BulletedListLinks_Click (object sender, BulletedListEventArgs e)

      {

          switch (e. Index)

          {

              case 0: BulletedListLinks. BackColor = System. Drawing. Color. LightCoral;

                  break;

              case 1: BulletedListLinks. BackColor = System. Drawing. Color. Aqua;

                  break;

              case 2: BulletedListLinks. BackColor = System. Drawing. Color. LightGreen;

                  break;

}

}

}



Literal


If not it is required to change value of the text programmno, it is possible to use an element of management Literal. In that case the text will be is deduced « as is », without tegov <span>. This class is inherited not from WebControl, and from Control, therefore it{he} can be put outside of the form. Accordingly he{it} does not have the properties responsible for appearance and style. It is not terrible, style can be determined in the container in which he is switched on - div or Panel. But there is interesting property Mode. We shall try on an example:



<asp:Literal ID = "Literal1" Runat = "server" Mode = "Encode"

  Text = " <b> Here is some text </b> "> </asp:Literal>


Mode = "Encode" codes the text so that in a browser this HTML-code has been seen, replacing special symbols of a marking by CER-sequences.



*lt; b*gt; Label*lt;/b*gt;


It is useful, if it happens it is necessary to deduce{remove} a code, and not only HTML. On some sites where there are tutorials on With ++ in plain text, in examples of a code there is a line



*include


Without a name of an included file. It "has been eaten" with a browser which counts all in <> for teg even if does not distinguish it{him} though the text has been made in <PRE> </PRE>. Well, this problem can be avoided.



Table


Tables in HTML are very much distributed, as they allow to position elements on page. The server element of management is set tegami <asp:Table ID = "Table1" runat = "server"> </asp:Table>. Power ASP .NET is shown at dynamic creation of the table. This element of management in ASP .NET is used less often as element DataGrid allows to receive the same results, having besides binding to the data.


Property Rows nablics is the container of lines - elements TableRow, and they, in turn have svojtsvo Cell - a collection of elements TableCell. Itself TableCell - the container of any elements of management. They cannot be inserted into the table differently, than in one of elements TableCell. The text in a cell can be written down through property Cell. Text or an insert of element Literal.


Lines of the table can be also such as TableHeaderRow and TableFooterRow. Such lines are always displayed on mobile devices with the small screen even if the table big and for its{her} viewing is necessary scrolling. Cells of the table can be such as TableHeaderCell - successor TableCell. The text in them is displayed by the selected semiboldface font and centrirovan.


In ASP .NET 2.0 at an element the opportunity to set headings with the help of property Caption has appeared. The site of heading is defined{determined} by property CaptionAlign. At value Bottom he will be is under the table. At other values the heading is where it is necessary to him - above, Left and Right simply level at left or a right edge.


This example illustrates creation of the table in a program mode. Game « Find number » trains attention and memory. In the beginning the player enters the size of the table n. The program generates the square table where numbers from 1 up to n^2 are written on buttons and are hashed in the casual order.




protected void Page_Load (object sender, EventArgs e)

      {

          int tableSize = 5;

          if (! Page. IsPostBack)

          {

              int [,] numbers = new int [tableSize, tableSize];

              for (int i = 0; i <tableSize; i ++)

                  for (int j = 0; j <tableSize; j ++)

                  {

                      numbers [i, j] = i * tableSize + j+1;

}

              int current = tableSize * tableSize;

              Random r = new Random ();

              for (int i = 0; i <tableSize * tableSize / 2; i ++)

              {

                  // swap i and number

                  int number = r. Next (1, current);

                  int t = numbers [number / tableSize, number % number];

                  numbers [number / tableSize, number % number] = numbers [i / tableSize, i % tableSize];

                  numbers [i / tableSize, i % tableSize] = t;

                  current-;

}

              Table Table1 = new Table ();

              Table1. CellSpacing = 0;

              Table1. CellSpacing = 0;

              Table1. BorderWidth = 2;

              Table1. GridLines = GridLines. Both;

              for (int i = 0; i <tableSize; i ++)

              {

                  TableRow row = new TableRow ();

                  for (int j = 0; j <tableSize; j ++)

                  {

                      TableCell cell = new TableCell ();

                      Button button = new Button ();

                      button. Text = numbers [i, j] .ToString ();

                      button. OnClientClick = " return false ";

                      button. Width = 26;

                      button. Height = 26;

                      cell. Controls. Add (button);

                      cell. Height = 26;

                      cell. Width = 26;

                      row. Cells. Add (cell);

}

                  Table1. Rows. Add (row);

}

              form1. Controls. Add (Table1);

}

}

}



Calendar


This class has no analogues in html. Having defined{determined} a unique element of management, it is possible to create and put at disposal of visitors a high-grade calendar where they can scroll months, choose day or week. Appearance of this element of management can be the diversified. And all this is realized by means html. Earlier it was possible only with help ActiveX - kontrolov which need to be loaded from the server, to register in system and to check on safety.


Calendar has set of properties.

CellPadding "Stuffing" (distance between borders of a cell{cage} and its{her} contents

CellSpacing distance between cells{cages}

DayNameFormat the Way of a spelling of names of days of week. Can accept values FirstLetter, FirstTwoLetters, Full, Short

FirstDayOfWeek For the task of the first day of week, Default - the installations accepted in system

NextPrevFormat Show of names of the last and next months. FullMonth - the full name, ShortMonth - the first 3 letters., CustomText - any text determined by the programmer

SelectionMode the Way of a choice of date. Are accessible Day, DayWeek, DayWeekMonth and None

Whether ShowDayHeader To show names of days of week (yes by default)

Whether ShowGridLines To show a grid (no by default)

Whether ShowTitle To show heading (no by default)

TitleFormat MonthYear, Month

TodaysDate What date will be chosen current. By default - date on the server.

VisibleDate Month which will be shown in a calendar


Calendar supports various calendar systems - not only habitual Gregorian, but also julianskij, iudejskij, Muslim, buddistskij. It can be made, changing the cultural information of page.


Choice the client of a date started of travel on a site turististicheskogo agencies.



<% Page Language = " C * " %>

  <script runat = "server">

      void Page_Load (Object sender, EventArgs e)

      {

          if (Page. IsPostBack)

          {

              TextToUser. Text = " you will go " +

                  calVoyage. SelectedDate. ToLongDateString ();

}

}

      void calSelectChange (Object sender, EventArgs e)

      {

          if (calVoyage. SelectedDate> DateTime. Today)

          {

              TextToUser. Text = " you really want to go to travel " + calVoyage. SelectedDate. ToShortDateString () + "?";

              Button ok = new Button ();

              ok. Width = 100;

              ok. Text = "Yes";

              form1. Controls. Add (ok);

}

          else

             TextToUser. Text = " Choose the future date ";

}

  </script>

  <html xmlns = " http: // www.w3.org/1999/xhtml ">

  <head>

      <title> the Example of a calendar </title>

  </head>

  <body>

      <form runat = "server" id = "form1">

          <asp:Calendar ID = "calVoyage" runat = "server" BackColor = "lightgreen" CellPadding = "3"

              CellSpacing = "3" NextPrevFormat = "FullMonth" SelectionMode = "DayWeekMonth" OnSelectionChanged = "calSelectChange"/>

          <asp:Label ID = "TextToUser" runat = "server" Font-Bold = "True" Font-Underline = "False"/> <br>

      </form>

  </body>

  </html>


If we shall see at a HTML-code of page which is generated by this program we shall see rather volumetric text, including function on JavaScript and the big table which each cell refers to this function. Would be uneasy to write all this.


Properties SelectMonthText and SelectWeekText set symbols HTML, by default *gt; and *gt; *gt; this sequence is displayed in symbols>.


To event of a choice of date OnSelectionChanged it is connected obrabotchik calSelectChange. In him we all over again check, whether the chosen date of the future in relation to today is. If yes, the new button for podtverzhenija the chosen date is created.


Such line in Page_Load



this. Culture=new CultureInfo ("th-TH") .ToString ();


Will make a calendar such what is accepted in Thailand. Do not forget to switch on space of names of globalization.



<% @Import Namespace = " System. Globalization " %>


Property SelectionMode equal DayWeekMonth, allows to choose both concrete day, and week or month. How to receive the chosen range of dates? SelectedDate returns only the first day of a range. For this purpose there is collection Calendar1. SelectedDates.



TextToUser. Text = " you probudete on travel ";

              for (int i = 0; i <calVoyage. SelectedDates. Count; i ++)

              {

                  TextToUser. Text + = calVoyage. SelectedDates [i] .ToShortDateString () +

                  "<br>";

}


And will write even more elegantly so:



foreach (DateTime i in calVoyage. SelectedDates)

              {

                  TextToUser. Text + = i. ToShortDateString () + "<br>";

}


It is possible to forbid for the user to choose the last date. For this purpose it is possible to use property IsSelectable.



if (e. Day. Date <DateTime. Now) {

  e. Day. IsSelectable = false;

}



Autoformatting of a calendar


At a calendar not one style, and a little. Different styles are applied to heading, to the days off and week-days, days of not current months and by today. It is possible to set styles as with the help of external classes, and it is direct. All this simply amazes. It is possible to put a calendar for thousand different sites and everywhere he will look differently.


In Visual Studio 2005 it is possible to change appearance of a calendar with the help of the predetermined patterns. At many elements of management in a mode of design is « clever jarlychki » (smart tags). In them there is a link to dialogue of autoformatting of a calendar. Thus already made changes in properties will be saved.


Appearance of a calendar it is possible to change in obrabotchike events DayRender. At argument obrabotchika DayRenderEventArgs two properties: Cell with type TableCell - successor WebControl, and Day such as CalendarDay. In Cell it is possible to add new elements, but only such which do not start events.



protected void calVoyage_DayRender (object sender, DayRenderEventArgs e)

      {

          if (e. Day. Date. Day == 8 ** e. Day. Date. Month == 3)

          {

              e. Cell. BorderColor = System. Drawing. Color. Red;

              e. Cell. BorderWidth = 4;

              e. Cell. Controls. Add (new LiteralControl (" <br> Without women to live it is impossible "));

}

}


In this example day, and if it on March, 8 around of him{it} the red framework is drawn is checked. And element LiteralControl with the text corresponding to the moment is still added.



Sending given to other page


In ASP.NET 1.1 sending of the data between pages was not resolved. In ASP.NET 2.0 elements of management have property PostBackUrl where it is possible to specify, to what page system should pass the Web-form if departure of the data on the server is initiated by this element of management.


Through property PreviousPage of page it is possible to find out, what page was a source postbehka our page.


On the first page the calendar is drawn.



<% Page Language = " C * " %>

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

  <script runat = "server">

  </script>

  <html xmlns = " http: // www.w3.org/1999/xhtml ">

  <head runat = "server">

   <title> the First page </title>

  </head>

  <body>

  <form id = "form1" runat = "server">

  Your name: <br/>

  <asp:Textbox ID = "TextBox1" Runat = "server">

  </asp:Textbox>

  <p>

  Desirable date of a start? <br/>

  <asp:Calendar ID = "Calendar1" Runat = "server"> </asp:Calendar> </p>

  <br/>

  <asp:Button ID = "Button2" Runat = "server" Text = " Submit page to Page2.aspx "

  PostBackUrl = "Page2.aspx"/>

  <p>

  <asp:Label ID = "Label1" Runat = "server"> </asp:Label> </p>

  </form>

  </body>

  </html>


And on the second values of the first form are read:



<% Page Language = " C * " %>

  <! 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 Page_Load (object sender, System. EventArgs e)

  {

      if (PreviousPage! = null)

      {

          TextBox pp_Textbox1;

          Calendar pp_Calendar1;

          pp_Textbox1 = (TextBox) PreviousPage. FindControl ("Textbox1");

          pp_Calendar1 = (Calendar) PreviousPage. FindControl ("Calendar1");

          Label1. Text = "Hello", + pp_Textbox1. Text + "! <br/> " +

            " You have chosen: " + pp_Calendar1. SelectedDate. ToShortDateString ();

}

}}

  </script>

  <html xmlns = " http: // www.w3.org/1999/xhtml ">

  <head runat = "server">

      <title> the Second page </title>

  </head>

  <body>

      <form id = "form1" runat = "server">

      <div>

      <asp:Label ID = "Label1" Runat = "server"> </asp:Label>

      </div>

      </form>

  </body>

  </html>



The conclusion


We have considered only some elements of management and were convinced, that they give rich opportunities. In the following lecture we shall consider other important class of elements of management - validatory.