Web application example

Default.aspx

<%@ Page Language=”C#” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

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

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Procesare String</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div style=”border:solid 1px #ccc;padding:10px;width:500px;height:500px;position:relative; background-color:#eee”>
<!– String Label –>
<asp:Label ID=”labelString” runat=”server” Style=”z-index:102;font-family:Arial; font-size:small;” Text=”String:”></asp:Label>
<br />

<!– Text Input –>
<asp:TextBox ID=”textBox” runat=”server” Style=”z-index:105;font-family:Arial; font-size:small;” Width=”256px”></asp:TextBox>

<!– Validare Text Input–>
<asp:CustomValidator ID=”ValidateText” runat=”server” ControlToValidate=”textBox” ErrorMessage=”Nu este text!” OnServerValidate=”ValidateText_ServerValidate” Style=”z-index:108;”>
</asp:CustomValidator>
<br /><br />

<!– Tip Procesare –>
<asp:Label ID=”labelTipProcesare” runat=”server” Style=”z-index:104;font-family:Arial; font-size:small;” Text=”Tip Procesare”></asp:Label>
<br />
<!– DropDown List –>
<asp:DropDownList ID=”dropDownList” runat=”server” Style=”z-index: 109;font-family:Arial; font-size:small;” Width=”256px”>
<asp:ListItem Value=”0″>Litere mici</asp:ListItem>
<asp:ListItem Value=”1″>Litere mari</asp:ListItem>
<asp:ListItem Value=”2″>Valoare numerica</asp:ListItem>
</asp:DropDownList>

<!– Nr. Prim –>
<asp:Label ID=”labelNrPrim” runat=”server” Style=”z-index: 103;font-family:Arial; font-size:small;” Text=”Nr.Prim:”></asp:Label>
<!– Nr. Prim Input –>
<asp:TextBox ID=”nrBox” runat=”server” Style=”z-index:106;font-family:Arial; font-size:small;” Width=”118px”></asp:TextBox>

<!– Validare Nr. Prim –>
<asp:CustomValidator ID=”ValidateNrPrim” runat=”server” ControlToValidate=”nrBox” ErrorMessage=”Nu este nr. prim!” OnServerValidate=”ValidateNrPrim_ServerValidate” Style=”z-index:107;”>
</asp:CustomValidator><br /><br />

<!– Proceseaza –>
<asp:Button ID=”butonProceseaza” runat=”server” Style=”z-index:100;font-family:Arial; font-size:small;” Text=”Proceseaza” Width=”107px” OnClick=”Proceseaza_Click” />
<br /><br />
<!– ListBox cu Select –>
<asp:ListBox ID=”listBox” runat=”server” Height=”135px” SelectionMode=”Multiple” Style=”z-index:110;” Width=”300px”>
</asp:ListBox>
<br /><br />
<!– Sterge –>
<asp:Button ID=”buttonSterge” runat=”server” Style=”z-index:111;font-family:Arial; font-size:small;” Text=”Sterge” Width=”107px” OnClick=”Sterge_Click” />
</div>
</form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
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;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ValidateNrPrim_ServerValidate(object source, ServerValidateEventArgs args)
{
int nr = Int32.Parse(nrBox.Text);
args.IsValid = true;
for(int i=2;i<=nr/2;i++)
if (nr % i == 0)
{
args.IsValid = false;
break;
}
}

protected void ValidateText_ServerValidate(object source, ServerValidateEventArgs args)
{
/*double nr;
string text = textBox.Text.Trim();
bool b = double.TryParse(text, out nr);
if (b)
args.IsValid = false;
else
args.IsValid = true;
*/
string text  = textBox.Text.Trim();
args.IsValid = true;
for (int i = 0; i < text.Length; i++)
if (char.IsDigit(text[i]) == true)
args.IsValid = false;
}

protected void Proceseaza_Click(object sender, EventArgs e)
{
switch (dropDownList.SelectedValue)
{
case “0″:
listBox.Items.Add(textBox.Text.ToLower());
break;
case “1″:
listBox.Items.Add(textBox.Text.ToUpper());
break;
case “2″:
int prim = Int32.Parse(nrBox.Text);
string s = textBox.Text;
int rez=0;
for (int i=0;i<s.Length – 1;i++)
rez = prim *((int)s[i])+(int)s[i+1];
listBox.Items.Add(rez.ToString());
break;
}
}
protected void Sterge_Click(object sender, EventArgs e)
{
ListItemCollection collection = new ListItemCollection();
foreach (ListItem item in listBox.Items)
{
if (item.Selected)
collection.Add(item);
}
foreach (ListItem item in collection)
{
listBox.Items.Remove(item);
}
}
}

Leave a Reply