11
Web Controls

Web Controls

  • Upload
    chick

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

Web Controls. Making a WebRequest. using System; using System.Collections.Generic ; using System.Linq ; using System.Text ; using System.Net ; using System.IO;. Creating a WebRequest. namespace WebRequestorApp { class Program { static void Main( string [] args ) { - PowerPoint PPT Presentation

Citation preview

Web Controls

Making a WebRequest

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;

Creating a WebRequest

namespace WebRequestorApp{ class Program { static void Main(string[] args) { WebRequest req = WebRequest.Create("http://www.microsoft.com"); WebResponse resp = req.GetResponse(); StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.ASCII); Console.WriteLine(reader.ReadToEnd()); } }}

Mixing Code With HTML<%@ Page Language="C#" Debug="true" %><html><body><h1>Hello World!!!</h1><%

// This block will execute in the Render_Control methodResponse.Write("Check out the family tree: <br/> <br/>");Response.Write(this.GetType().ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.BaseType.ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.BaseType.BaseType.ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());

%></body></html>

Mixing Code With HTML :Server Side

<%@ Page Language="C#" Debug="true" %><script runat="server">void ShowLineage(){

Response.Write("Check out the family tree: <br/> <br/>");Response.Write(this.GetType().ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.BaseType.ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.BaseType.BaseType.ToString());Response.Write(" which derives from: <br/> ");Response.Write(this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());

}</script><html><body> <h1>Hello World!!!</h1><% ShowLineage();%></body></html>

Dropdown List

<form id="form2" runat="server"><div style="font-family: 'Times New Roman', Times, serif; font-size: 14pt; font-weight: bold">Page in Visual Studio<br />

<asp:Label ID="Label1" runat="server" Text="Type in me:"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><asp:DropDownList ID="DropDownList1" runat="server"><asp:ListItem>Item 1</asp:ListItem><asp:ListItem>Item 2</asp:ListItem><asp:ListItem>Item 3</asp:ListItem><asp:ListItem>Item 4</asp:ListItem><asp:ListItem>Item 5</asp:ListItem><asp:ListItem>Item 6</asp:ListItem></asp:DropDownList><br />

<asp:Button ID="Button1" runat="server" Text="Click Me" /></div></form>

DropDownList Code

protected void Button1_Click(object sender, EventArgs e) { Response.Write("Hello. Here's what you typed into the text box: <br/>"); Response.Write(this.TextBox1.Text); Response.Write("<br/>"); Response.Write("And the selected item is: <br/>"); Response.Write(this.DropDownList1.SelectedItem.Text); }

CheckBoxList

protected void Button1_Click(object sender,System.EventArgs e) { Label1.Text = "You Selected:<br/>"; foreach (ListItem li in CheckBoxList1.Items){ if (li.Selected == true){ Label1.Text += li.Text + "<br/>"; } } }

RadioButtonList

<form id="form2" runat="server"> <div style="text-align: left"> <br /> My level of education is:<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" onselectedindexchanged="RadioButtonList1_SelectedIndexChanged"> <asp:ListItem>Primary school</asp:ListItem> <asp:ListItem>Middle school</asp:ListItem> <asp:ListItem>High School</asp:ListItem> <asp:ListItem>Vocational college</asp:ListItem> <asp:ListItem>Bachelors degree</asp:ListItem> <asp:ListItem>Masters degree</asp:ListItem> <asp:ListItem>PhD or other doctorate</asp:ListItem> </asp:RadioButtonList> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form>

The Code

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = "Your level of education is " + RadioButtonList1.Text; }

The ListBox

Assuming that you created a ListBox lstEmployees:Protected void btnAdd_Click(object sender, EventArgs e) { if (lstEmployees.SelectedIndex > -1) { string _value = lstEmployees.SelectedItem.Value;//Gets the value of items in list. string _text = lstEmployees.SelectedItem.Text;//Gets the Text of items in the list. ListItem item = new ListItem (); //create a list item item.Text = _text; //Assign the values to list item item.Value = _value; lstSelectedEmployees.Items.Add(item); //Add the list item to the selected list of employees lstEmployees.Items.Remove(item); //Remove the details from employee list }