前台代码:
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="465px">
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="T_UserName" HeaderText="用户名" />
<asp:BoundField DataField="T_Password" HeaderText="密码" />
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%#Eval("Id") %>' οnclick="lnkEdit_Click">编辑</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("Id") %>' οnclick="lnkDelete_Click">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
后台:
public String oracleStr = ConfigurationManager.ConnectionStrings["oracleCon"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sql = "select * from T_Users order by id";
loadData(sql);
}
}
private void loadData(string sql)
{
OracleConnection oracleCon = new OracleConnection(oracleStr);
oracleCon.Open();
OracleCommand cmd = oracleCon.CreateCommand();
cmd.CommandText = sql;
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
oracleCon.Dispose();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void lnkEdit_Click(object sender, EventArgs e)
{
LinkButton lnk = new LinkButton();
ViewState["Id"] = lnk.ID;
Response.Redirect("OracleAddData.aspx?id="+ViewState["Id"]);
}
protected void lnkDelete_Click(object sender, EventArgs e)
{
LinkButton lnk = new LinkButton();
ViewState["Id"] = lnk.ID;
string sql = "delete from T_Users where Id=" + ViewState["Id"].ToString() + "order by id";
loadData(sql);
}
跳转页前台:
<div >
用户名: <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br /><br /><br />
密 码: <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br /><br /><br />
<asp:Button ID="btnSave" runat="server" Text="保存" οnclick="btnSave_Click" />
</div>
后台:
public String oracleStr = ConfigurationManager.ConnectionStrings["oracleCon"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
OracleConnection oracleCon = new OracleConnection(oracleStr);
oracleCon.Open();
OracleCommand cmd = oracleCon.CreateCommand();
string sql = "insert into T_Users (Id,T_UserName,T_Password) values(se.nextval,:name,:pwd)";
cmd.Parameters.AddWithValue(":name", txtUserName.Text);
cmd.Parameters.AddWithValue(":pwd", txtPassword.Text);
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
cmd.Dispose();
oracleCon.Dispose();
Response.Redirect("GridViewOracle.aspx");
}