ASP.NET Tutorial/HTML Controls/Text input

Материал из .Net Framework эксперт
Версия от 12:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Read input from text field, do calculation then output to the save page (C#)

File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="CurrencyConverter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Currency Converter</title>
  </head>
  <body>
    <form ID="Form1" method="post" runat="server">
        <div>
        Convert:
        <input type="text" ID="US" runat="server"/>U.S. dollars to
        <select ID="Currency" runat="server" />
        <br /><br />
        <input type="submit" value="OK" ID="Convert" runat="server" OnServerClick="Convert_ServerClick" />
        <input type="submit" value="Show Graph" ID="ShowGraph" runat="server" OnServerClick="ShowGraph_ServerClick" />
        <br /><br />
        <img ID="Graph" alt="Currency Graph" scr="" runat="server" />
        <br /><br />
        <div style="font-weight: bold" ID="Result" runat="server"></div>
      </div>
    </form>
  </body>
</html>

File: Default.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class CurrencyConverter : System.Web.UI.Page
{
  protected void Page_Load(Object sender, EventArgs e)
  {
    if (this.IsPostBack == false)
    {
      Currency.Items.Add(new ListItem("Euros", "0.5"));
      Currency.Items.Add(new ListItem("Japanese Yen", "100"));
      Currency.Items.Add(new ListItem("Canadian Dollars", "2"));
    }
    Graph.Visible = false;
  }
    protected void Convert_ServerClick(object sender, EventArgs e)
    {
        decimal amount;
        bool success = Decimal.TryParse(US.Value, out amount);
        if (success)
        {
            ListItem item = Currency.Items[Currency.SelectedIndex];
            decimal newAmount = amount * Decimal.Parse(item.Value);
            Result.InnerText = amount.ToString() + " U.S. dollars = ";
            Result.InnerText += newAmount.ToString() + " " + item.Text;
        }
        else
        {
            Result.InnerText = "The number you typed in was not in the correct format. ";
            Result.InnerText += "Use only numbers.";
        }
    }
    protected void ShowGraph_ServerClick(object sender, EventArgs e)
    {
        Graph.Src = "Pic" + Currency.SelectedIndex.ToString() + ".png";
        Graph.Visible = true;
    }
}


Set value to html:textbox in page load event (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
   sub Page_Load(Sender as object, e as EventArgs)
      tbMyText.Value = "This is the page load event!"
   end sub
</script>
<html><body>
   <form runat="server">
      <input type="text" size="25" 
         id="tbMyText" runat="server" />
   </form>
</body></html>


TextBox server changed event

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ChangeEvents" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Change Events</title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
        <select runat="server" 
                id="List1" 
                size="5" multiple 
                Name="List1" 
                onserverchange="List1_ServerChange">
        <option>Option 1</option>
        <option>Option 2</option>
    </select>
    <br/>
    <input type="text" 
           runat="server" 
           ID="Textbox1" 
           Size="10"
           Name="Textbox1" 
           OnServerChange="Ctrl_ServerChange"><br/>
    <input type="checkbox" 
           runat="server" 
           ID="Checkbox1"
           Name="Checkbox1" 
           OnServerChange="Ctrl_ServerChange">Option text<br/>
        &nbsp;
    <input type="submit" 
           runat="server" 
           ID="Submit1" 
           Name="cmdSubmit"
           value="Submit Query" 
           onserverclick="Submit1_ServerClick">
       </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ChangeEvents : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      List1.Items.Add("Option 3");
      List1.Items.Add("Option 4");
      List1.Items.Add("Option 5");
    }
  }
  protected void Ctrl_ServerChange(object sender, System.EventArgs e)
  {
    Response.Write("<li>ServerChange detected for " + sender + "</li>");
  }
  protected void List1_ServerChange(object sender, EventArgs e)
  {
    Response.Write("<li>ServerChange detected for List1. " + "The selected items are:</li><br/>");
    foreach (ListItem li in List1.Items)
    {
      if (li.Selected)
        Response.Write("&nbsp;&nbsp;- " + li.Value + "<br/>");
    }
  }
  protected void Submit1_ServerClick(object sender, EventArgs e)
  {
    Response.Write("<li>ServerClick detected for Submit1.</li>");
  }
}