我写的第一个ASP.NET"组件"

类别:Asp 点击:0 评论:0 推荐:

demo.aspx代码:
<%@ Inherits="FeaturedProduct" %>
<script Language="C#" runat="server">
 void btnSetBgcolor_click(Object s,EventArgs e)
 {
  setBackCalor(txtBgcolor.Text);
 }
 void btnSubmit_click(Object s,EventArgs e)
 {
  Add(txtUsername.Text,txtContent.Text);
 }
</script>
<table width="200" cellpadding="10"
  cellspacing="0" bgcolor="<%=BackColor%>">
<tr>
  <td>
  <h3>Blenders on Sale!</h3>
  See our selection of blenders in the
  home appliance deparment.
  </td>
</tr>
</table>
<form runat="server">
输入颜色:
<asp:TextBox id="txtBgcolor" runat="server"/>
<asp:Button id="btnSetBgcolor" onclick="btnSetBgcolor_click" Text="设置颜色" runat="server"/>
<br>
username:
<asp:TextBox id="txtUsername" runat="server"/>
<br>
content:
<asp:TextBox id="txtContent" runat="server"/>
<br>
<asp:Button id="btnSubmit" onclick="btnSubmit_click" Text="提交留言" runat="server"/>
</form> FeaturedProduct.cs代码:
using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
using System.Web.UI.WebControls;

public class FeaturedProduct : System.Web.UI.Page
{
   public string  BackColor = "lightgreen";
   public void setBackCalor(string str)
 {
  BackColor = str;
  }
  public void Add(string username,string content)
 {
   OleDbConnection conConnection = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Server.MapPath("./db1.mdb") );
   OleDbCommand cmdCommand = new OleDbCommand("insert into guestbook (username,content) values('" + username + "','" + content + "')", conConnection);
   conConnection.Open();
   cmdCommand.ExecuteNonQuery();
   conConnection.Close();
 }
}

本文地址:http://com.8s8s.com/it/it8126.htm