ASP.Net/Language Basics/Type Convert

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

Convert date to string (C#)

<script Language="c#" runat="server">
void Page_Load() {
  string strEarn = "150";
  string strTax = "23.0";
  double dblEarn = Convert.ToInt32(strEarn);
  decimal decTax = Convert.ToDecimal(strTax);
  double dblTotal = dblEarn - ((dblEarn/100) * (double)decTax);
  Display1.Text = dblTotal.ToString();
  DateTime datNow = DateTime.Now;
  Display2.Text = " " + datNow.ToString("d");
}
</script>
<html>
<head>
  <title>Declaring Variables</title>
</head>
<body>
  Your total earnings after tax are $
  <asp:label id="Display1" runat="server" />
  <br />
  The date is
  <asp:label id="Display2" runat="server" />
</body>
</html>



Convert double and date to string (C#)

<script Language="c#" runat="server">
void Page_Load() {
  string strEarn = "150";
  string strTax = "23.0";
  double dblEarn = Convert.ToInt32(strEarn);
  decimal decTax = Convert.ToDecimal(strTax);
  double dblTotal = dblEarn - ((dblEarn/100) * (double)decTax);
  Display1.Text = dblTotal.ToString();
  DateTime datNow = DateTime.Now;
  Display2.Text = " " + datNow.ToString("d");
}
</script>
<html>
<head>
  <title>Declaring Variables</title>
</head>
<body>
  <asp:label id="Display1" runat="server" />
  <br />
  <asp:label id="Display2" runat="server" />
</body>
</html>



Convert int to String (C#)

<%@ Page Language="c#" %>
<script runat="server">
void Page_Load()
{
    const int AbsoluteZero = -273;
    lblOut.Text = Convert.ToString(AbsoluteZero);
}
</script>
<html>
<head>
<title>Creating Variables Example</title>
</head>
<body>
    <asp:Label runat="server" ID="lblOut"/><br/>
</body>
</html>



Convert int value to string (VB.net)

<script runat="Server" language="VB">
Sub Page_Load()
 
    Dim strArrayDetails(2) As String
    Dim intLoop As Integer
    
    strArrayDetails(0) = text1.Text 
    strArrayDetails(1) = text2.Text
    strArrayDetails(2) = text3.Text
    
    Message1.text = CStr(strArrayDetails(0)) 
    Message2.text = CStr(strArrayDetails(1)) 
    Message3.text = CStr(strArrayDetails(2)) 
End Sub
</script>
<html>
  <head>
    <title>Text Box Example</title>
  </head>
  <body>
    <asp:label id="message1" runat="server" />
    <br /> 
    <asp:label id="message2" runat="server" /> 
    <br />
    <asp:label id="message3" runat="server" />
    <br />
    <form runat="server">
      Please enter your name:
      <asp:textbox id="text1" runat="server" />
      <br /><br />
      Please enter your address:
      <asp:textbox id="text2" runat="server" rows=5 textmode="multiline" />
      <br /><br />
      Please enter your chosen password:
      <asp:textbox id="text3" runat="server" textmode="password" />
      <br /><br /> 
      <input type="Submit">
    </form>
  </body>
</html>



Convert Now (current date) to string (C#)

<script language="c#" runat="server">
void Page_Load()
{   
  time.Text=DateTime.Now.Hour.ToString() + ":" + 
             DateTime.Now.Minute.ToString() + ":" +  
             DateTime.Now.Second.ToString();
} 
</script>
<html>
<head><title>The Punctual Web Server</title></head>
<body>
  <h1>Welcome</h1>
  In WebServerLand the time is currently:
<asp:label id="time" runat="server" />
</body>
</html>



Convert selected date from asp:Calendar to long string (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Button1_Click(sender As Object, e As EventArgs)
      Label1.Text = Calendar1.SelectedDate.ToLongDateString()
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Calendar id="Calendar1" runat="server"></asp:Calendar>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
    </form>
</body>
</html>



Convert string to double (VB.net)

<%@ page language="vb" runat="server" %>
<script runat="server">
  Sub Page_Load()
    pi.text = Math.PI
    exp.text = Math.E
  End Sub
  Sub Update(Sender As Object, E As EventArgs)
    Dim dblInput As Double = CDbl(input.text)
    sqrt_input.text = dblInput
    sqrt_result.text = Math.Sqrt(dblInput)
 
    abs_input.text = dblInput
    abs_result.text = Math.Abs(dblInput)
 
    log_input.text = dblInput
    log_result.text = Math.Log10(dblInput)
  End Sub
</script>
<html>
  <body>
    <hr />
    Pi = <asp:label id="pi" runat="server" /><br />
    Exponential Constant = <asp:label id="exp" runat="server" />
    <hr />
    <form runat="server">
    Input = <asp:textbox id="input" runat="server" />
    <asp:button text="Submit" runat="server" onclick="update" />
    </form><hr />
    Square root of <asp:label id="sqrt_input" runat="server"/>
    = <asp:label id="sqrt_result" runat="server" /><br />
    Absolute Value of <asp:label id="abs_input" runat="server"/>
    = <asp:label id="abs_result" runat="server" /><br />
    Logarithm of <asp:label id="log_input" runat="server"/>
    = <asp:label id="log_result" runat="server" /><br />
  </body>
</html>



Convert String to int and decimal (C#)

<script Language="c#" runat="server">
void Page_Load() {
  string strEarn = "150";
  string strTax = "23.0";
  double dblEarn = Convert.ToInt32(strEarn);
  decimal decTax = Convert.ToDecimal(strTax);
  double dblTotal = dblEarn - ((dblEarn/100) * (double)decTax);
  Display1.Text = dblTotal.ToString();
  DateTime datNow = DateTime.Now;
  Display2.Text = " " + datNow.ToString("d");
}
</script>
<html>
<head>
  <title>Declaring Variables</title>
</head>
<body>
  <asp:label id="Display1" runat="server" />
  <br />
  <asp:label id="Display2" runat="server" />
</body>
</html>



Convert value from asp:TextBox to int and compare (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Reset()
    
      Textbox1.Text = ""
      Textbox2.Text = "0"
    
    End Sub
    
    Sub Page_Load(sender As Object, e As EventArgs)
    
      If Not Page.IsPostback
        Reset()
      End If
    
    End Sub
    
    Sub btnSubmit_Click(sender As Object, e As EventArgs)
    
      If cint(textbox2.text) < 20 Then
    
        label1.text = "Hi " & textbox1.text & ", you are " & textbox2.text & ", which is young!"
    
      Else If cint(textbox2.text) < 40 Then
      
        label1.text = "Hi " & textbox1.text & ", you are " & textbox2.text & ", which means you"re middle-aged!"
    
      Else 
    
        label1.text = "Hi " & textbox1.text & ", you"re old!"
    
      End If
    
    End Sub
    
    Sub btnReset_Click(sender As Object, e As EventArgs)
    
      Reset()
    
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Enter your name: 
            <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            Enter you age: 
            <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button>
            &nbsp; 
            <asp:Button id="btnReset" onclick="btnReset_Click" runat="server" Text="Reset"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Convert value in asp:TextBox to bool in try catch block (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub btnConvert_Click(sender As Object, e As EventArgs)
      Try
        lblToInt.Text = cint(txtValueToConvert.Text)
      Catch
        lblToInt.Text = "Could not convert to Integer"
    
      End Try
    
      Try
        lblToDateTime.Text = cdate(txtValueToConvert.Text)
    
      Catch
        lblToDateTime.Text = "Could not convert to Date/Time"
    
      End Try
    
      Try
        lblToBoolean.Text = cbool(txtValueToConvert.Text)
    
      Catch
        lblToBoolean.Text = "Could not convert to Boolean"
    
      End Try
    
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Original input: 
            <asp:TextBox id="txtValueToConvert" runat="server"></asp:TextBox>
            &nbsp;<asp:Button id="btnConvert" onclick="btnConvert_Click" runat="server" Text="Convert!"></asp:Button>
        </p>
        <p>
            Convert to Integer produces: 
            <asp:Label id="lblToInt" runat="server"></asp:Label>
        </p>
        <p>
            Convert to Date/Time produces: 
            <asp:Label id="lblToDateTime" runat="server"></asp:Label>
        </p>
        <p>
            Convert to Boolean produces: 
            <asp:Label id="lblToBoolean" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Convert value in asp:TextBox to date in try catch block (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub btnConvert_Click(sender As Object, e As EventArgs)
      Try
        lblToInt.Text = cint(txtValueToConvert.Text)
      Catch
        lblToInt.Text = "Could not convert to Integer"
    
      End Try
    
      Try
        lblToDateTime.Text = cdate(txtValueToConvert.Text)
    
      Catch
        lblToDateTime.Text = "Could not convert to Date/Time"
    
      End Try
    
      Try
        lblToBoolean.Text = cbool(txtValueToConvert.Text)
    
      Catch
        lblToBoolean.Text = "Could not convert to Boolean"
    
      End Try
    
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Original input: 
            <asp:TextBox id="txtValueToConvert" runat="server"></asp:TextBox>
            &nbsp;<asp:Button id="btnConvert" onclick="btnConvert_Click" runat="server" Text="Convert!"></asp:Button>
        </p>
        <p>
            Convert to Integer produces: 
            <asp:Label id="lblToInt" runat="server"></asp:Label>
        </p>
        <p>
            Convert to Date/Time produces: 
            <asp:Label id="lblToDateTime" runat="server"></asp:Label>
        </p>
        <p>
            Convert to Boolean produces: 
            <asp:Label id="lblToBoolean" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Convert value in asp:TextBox to int in try catch block (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub btnConvert_Click(sender As Object, e As EventArgs)
      Try
        lblToInt.Text = cint(txtValueToConvert.Text)
      Catch
        lblToInt.Text = "Could not convert to Integer"
    
      End Try
    
      Try
        lblToDateTime.Text = cdate(txtValueToConvert.Text)
    
      Catch
        lblToDateTime.Text = "Could not convert to Date/Time"
    
      End Try
    
      Try
        lblToBoolean.Text = cbool(txtValueToConvert.Text)
    
      Catch
        lblToBoolean.Text = "Could not convert to Boolean"
    
      End Try
    
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Original input: 
            <asp:TextBox id="txtValueToConvert" runat="server"></asp:TextBox>
            &nbsp;<asp:Button id="btnConvert" onclick="btnConvert_Click" runat="server" Text="Convert!"></asp:Button>
        </p>
        <p>
            Convert to Integer produces: 
            <asp:Label id="lblToInt" runat="server"></asp:Label>
        </p>
        <p>
            Convert to Date/Time produces: 
            <asp:Label id="lblToDateTime" runat="server"></asp:Label>
        </p>
        <p>
            Convert to Boolean produces: 
            <asp:Label id="lblToBoolean" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Int, String and date variables (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
       string CapitalCityOfUK;
       int NumberOfStates;
       DateTime IndependenceDay;
    
       CapitalCityOfUK = "London";
       NumberOfStates = 50;
       IndependenceDay = Convert.ToDateTime("7/4/1776");
    
       lblCapital.Text = CapitalCityOfUK;
       lblNumStates.Text = Convert.ToString(NumberOfStates);
       lblDateIndependence.Text = Convert.ToString(IndependenceDay);
    }
</script>
<html>
<head>
    <title>Creating Variables Example</title>
</head>
<body>
    The contents of CapitalCityOfUk is: <asp:Label id="lblCapital" runat="server"></asp:Label>
    <br />
    The contents of NumberOfStates is: <asp:Label id="lblNumStates" runat="server"></asp:Label>
    <br />
    The contents of IndependenceDay is: <asp:Label id="lblDateIndependence" runat="server"></asp:Label>
</body>
</html>



Output ASCII Code (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void  Page_Load()
    {
        lblASCII.Text += "<Table><TR><TD>Character&nbsp;&nbsp;&nbsp;</TD><TD>ASCII code</TD></TR>";
        for (byte bytCounter=33;bytCounter<255; bytCounter++) {
            lblASCII.Text +=
            "<TR><TD>" + Convert.ToChar(bytCounter) + "</TD><TD>" + Convert.ToString(bytCounter) + "</TD></TR>";
        }
        lblASCII.Text += "</Table>";
    
    }
</script>
<html>
<head>
    <title>Demonstrate String Comparison</title>
</head>
<body>
    <form runat="server">
        For your reference, here are the ASCII codes:<br />
        <asp:Label id="lblASCII" runat="server"></asp:Label>
    </form>
</body>
</html>



Read string from asp textbox and format it to DateTime (C#)

<%@ Page Language="C#" Debug="true"%>
<script runat="server">
       void Page_Load()
       {
            if (Page.IsPostBack)
            {
                DateTime MyDateTime; 
                
                
                lblDateOut.Text = Convert.ToString(txtDateIn.Text);  //reads the variable
            
            }
        }  
</script>
<html>
<head>
<title>Variables Example - String and Char</title>
</head>
<body>
<form runat="server">
<asp:TextBox runat="server" ID="txtDateIn" ></asp:TextBox><br>
<asp:Label runat="server" ID="lblDateOut" >Label</asp:Label><br>
<asp:Button runat="server" Text="Click me format date time"></asp:Button>
</form>
</body>
</html>