Soon after I started Ajax, I wrote a little thing to keep a learning diary.
The message board picture is as follows:
Sample address: http://www.8dao.net/miniguest/
First, the Access database is used to facilitate migration.
The database is very simple. The Guest table has the ID, Name, and Content fields.
To implement Ajax, the use of asp.net ajax 1.0 bate is very convenient and can be downloaded to the http://ajax.asp.net.
After installation, open VS2005 and create an ASP.net AJAX Enabled Web Site project.
Add a WEB user control MiniGB. ascx to it. The control code is as follows:
<% @ Control Language = "C #" ClassName = "MiniGB" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<% @ Import Namespace = "System. Data" %> <script runat = "server">
Private OleDbConnection conn = new OleDbConnection ("provider = microsoft. jet. oledb.4.0; data source = "+ System. web. httpContext. current. server. mapPath ("guest. mdb "));
Private string dname = "Tourist ";
Public string Dname
{
Get
{
Return dname;
}
Set
{
Dname = value;
}
}
Protected void button#click (object sender, EventArgs e)
{
If (TextBox1.Text. Trim ()! = "")
{
OleDbCommand comm = new OleDbCommand ("insert into guest (name, content) values ('" + dname + "', '" + TextBox1.Text. trim () + "')", conn );
Comm. ExecuteNonQuery ();
Listupdate ();
TextBox1.Text = "";
}
}
Private void listupdate ()
{
OleDbDataAdapter da = new OleDbDataAdapter ("select top 5 * from guest order by id desc", conn );
DataSet ds = new DataSet ();
Da. Fill (ds );
DataList1.DataSource = ds;
DataList1.DataBind ();
}
Protected void Page_Load (object sender, EventArgs e)
{
Conn. Open ();
Listupdate ();
}
</Script>
<Style>
. Gtitle
{
Width: 200px;
Background-color: #464646;
Color: # FFFFFF;
Font-size: 14px;
Height: 20px;
Padding: 3px 0 0 10px;
Font-weight: bold;
Border-style: solid;
Border-top-width: 1px;
Border-bottom-width: 0px;
Border-left-width: 1px;
Border-right-width: 1px;
Border-color: # BBBBBB;
}
. Gbody
{
Width: 200px;
Border-style: solid;
Border-top-width: 0px;
Border-bottom-width: 0px;
Border-left-width: 1px;
Border-right-width: 1px;
Border-color: # BBBBBB;
Padding: 3px 5px 3px 5px;
}
. Gend
{
Width: 200px;
Border-style: solid;
Border-top-width: 0px;
Border-bottom-width: 1px;
Border-left-width: 1px;
Border-right-width: 1px;
Border-color: # BBBBBB;
Padding: 3px 5px 3px 5px ;}
. Gname
{
Width: pixel PX;
Font-size: 12px;
Color: blue ;}
. Gcontent
{
Width: pixel PX;
Word-break: break-all;
Color: #444444;
Font-size: 12px;
Padding: 3px 0 3px 0;
Border-style: dotted;
Border-top-width: 0px;
Border-bottom-width: 1px;
Border-left-width: 0px;
Border-right-width: 0px;
Border-color: # BBBBBB;
}
</Style> <asp: ScriptManagerProxy ID = "ScriptManagerProxy1" runat = "server">
</Asp: ScriptManagerProxy>
<Asp: UpdatePanel ID = "UpdatePanel1" runat = "server">
<ContentTemplate>
<Div class = "Gtitle"> <asp: Label ID = "Label1" runat = "server" Text = "message board"> </asp: Label> </div>
<Div class = "Gbody"> <asp: DataList ID = "DataList1" runat = "server">
<ItemTemplate>
<Div class = "Gname"> <% # DataBinder. eval (Container. dataItem, "name") %> Description: </div> <div class = "Gcontent"> <% # DataBinder. eval (Container. dataItem, "content") %> </div>
</ItemTemplate>
</Asp: DataList> </div>
<Div class = "Gend"> <asp: TextBox ID = "TextBox1" runat = "server" Width = "150px"/>
<Asp: Button ID = "Button1" runat = "server" Text = "message" OnClick = "button#click"/> </div>
</ContentTemplate>
</Asp: UpdatePanel>
In this way, you can complete the control, and then call the control on the page. The page that calls the control must have a ScriptManager control.
This control can be modified as the article comment control, plus: more..., display comment by Article ID.
Ajax is used as a learning log for beginners. I also hope to give you more advice.