ASP.Net/Language Basics/String
String concatenation (C#)
<%@ Page Language="C#" %>
<script runat="server">
void page_Load()
{
string str;
str = "Spring" + "Sale";
lblOut1.Text = str;
string str2;
DateTime datSaleSpring = Convert.ToDateTime("3/3/2005");
str2 = "Spring" + " " + "Sale begins " + Convert.ToString(datSaleSpring);
lblOut2.Text = str2;
lblSaleNote3.Text = "Spring" + " " + "Sale begins " + Convert.ToString(datSaleSpring);
string str4;
str4 = "Sale starts on 3/1/2005";
str4 = str4 + " and ends on " + txtSaleEnd.Text;
lblSaleNote4.Text = str4;
string str5;
str5 = "Sale starts on 3/1/2005 and ends on ";
str5 += txtSaleEnd.Text;
lblSaleNote5.Text = str5;
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<hr>Example 1<br>
<asp:Label runat="server" ID="lblOut1"/><br/>
<hr>Example 2<br>
<asp:Label runat="server" ID="lblOut2"/><br/>
<hr>Example 3<br>
<asp:Label runat="server" ID="lblSaleNote3"/><br/>
<hr>Example 4<br>
Sale End Date = <asp:TextBox runat="server" ID="txtSaleEnd"/>
<asp:Button runat="server" Text="Button"></asp:Button><br/>
<asp:Label runat="server" ID="lblSaleNote4"/><br/>
<hr>Example 5<br>
<asp:Label runat="server" ID="lblSaleNote5"/><br/>
</form>
</body>
</html>
String format (C#)
<%@ page language="C#" %>
<script runat="server">
void BT_Submit_Click(object sender, System.EventArgs e)
{
this.LT_Name.Text = string.Format("Your name is: {0}", this.TB_Name.Text);
}
</script>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form runat="server">
<p>Please enter your name:
<br /><br />
<asp:textbox id="TB_Name" runat="server"/>
<asp:button id="BT_Submit" runat="server" text="OK" onclick="BT_Submit_Click" />
</p>
<p><asp:literal id="LT_Name" runat="server"/></p>
</form>
</body>
</html>
Sub a string value (VB.net)
<%@ Page Language="VB" %>
<script runat="server">
Sub Button1_Click(sender As Object, e As EventArgs)
Dim fullString, subString As String
Dim startPos, length As Integer
startPos = CInt(TextBox2.Text)
length = CInt(TextBox3.Text)
fullString = TextBox1.Text
subString = fullString.SubString(startPos, length)
Label1.Text = subString
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine" Width="259px" Height="112px"></asp:TextBox>
</p>
<p>
start:
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</p>
<p>
length:
<asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Show Substring"></asp:Button>
</p>
</form>
</body>
</html>