Vs.net refreshing
| The code is as follows: |
Copy code |
|
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
<H2>
Use The VS control updatePanel to achieve no refreshing
</H2>
<P>
<Asp: UpdatePanel ID = "UpdatePanel1" RenderMode = "Block" UpdateMode = "Conditional" runat = "server">
<ContentTemplate>
Click the button to obtain the current time from the server <asp: TextBox ID = "txtTime" runat = "server"> </asp: TextBox>
<Asp: Button ID = "btn1" runat = "server" Text = "Button" onclick = "btn1_Click"/>
</ContentTemplate>
<Triggers>
<% -- <Asp: PostBackTrigger ControlID = "btn1"/> -- %>
<Asp: AsyncPostBackTrigger ControlID = "btn1"/>
</Triggers>
</Asp: UpdatePanel>
</P> |
Background code
| The code is as follows: |
Copy code |
Protected void btn1_Click (object sender, EventArgs e)
{
TxtTime. Text = DateTime. Now. ToString ();
} |
Upload images without refreshing
| The code is as follows: |
Copy code |
<% @ 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> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server" UpdateMode = "Conditional">
<ContentTemplate>
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
</Asp: ScriptManager>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "Button"/>
<Asp: FileUpload ID = "File1" runat = "server" Width = "200px"/>
</ContentTemplate>
<Triggers>
<Asp: PostBackTrigger ControlID = "Button1"/>
</Triggers>
</Asp: UpdatePanel>
<Asp: Image id = "image1" ImageUrl = "nopic.gif" Height = "115px" Width = "108px" runat = "server"/>
</Div>
</Form>
</Body>
</Html> |
Background code
| The code is as follows: |
Copy code |
|
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. IO;
Using System. Data. SqlClient;
Using System. Data;
Public partial class _ Default: baseClass
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
HttpPostedFile upFile = File1.PostedFile;
Int iFileLength = upFile. ContentLength;
Try
{
If (iFileLength = 0)
{
MessageBox ("select the file to upload! ");
}
Else
{
Byte [] FileByteArray = new Byte [iFileLength];
Stream StreamObject = upFile. InputStream;
StreamObject. Read (FileByteArray, 0, iFileLength );
SqlConnection conn = new SqlConnection ("server = ..; database = Test; uid = sa; pwd = 1234 ;");
ExecuteBySQLNonQuery ("delete from imageTable ");
SqlCommand cmd = new SqlCommand ("insert into [imageTable] values (@ image)", conn );
Cmd. Parameters. Add ("@ Image", SqlDbType. Binary, iFileLength). Value = FileByteArray;
Conn. Open ();
Cmd. ExecuteNonQuery ();
Conn. Close ();
MessageBox ("The photo has been uploaded successfully! ");
}
Image1.ImageUrl = "displayempphoto. ashx ";
}
Catch (Exception ex)
{
MessageBox (ex. Message );
}
}
}
|