我还是学生,有好多还不明白,求教。。。
这是前台代码:
<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" EnableEventValidation="false" CodeFile="ManageUser.aspx.cs" Inherits="Admin_ManageUser" %>
<!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">
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="UserEmail"
DataNavigateUrlFormatString="mailto:{0}" DataTextField="UserEmail" />
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="UserID" onrowcommand="GridView2_RowCommand"
AllowSorting="True" BackColor="White" BorderColor="White" BorderStyle="Ridge"
BorderWidth="2px" CellPadding="3" CellSpacing="1" Font-Names="Andalus"
Font-Size="Medium" GridLines="None" HorizontalAlign="Center">
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<Columns>
<asp:TemplateField HeaderText="用户名" SortExpression="UserName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="邮件地址">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("UserEmail", "Mailto:{0}") %>'
Text='<%# Eval("UserEmail") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="性别">
<ItemTemplate >
<%# Eval ("UserGender").ToString()=="1"?"男":"女" %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate >
<asp:Button ID ="Button1" runat ="server" CausesValidation="false" CommandName="Del" Text="删除" OnClientClick="return confirm('确定删除吗?')" CommandArgument ='<%#Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="获取行中的非主键数据" >
<ItemTemplate >
<asp:LinkButton ID ="lb1" runat ="server" CommandName="GetData1" Text="方法1" CommandArgument ='<%#Eval("UserName") %>'/>
<asp:LinkButton ID ="lb2" runat ="server" CommandName="GetData2" Text="方法2" CommandArgument ='<%# Container.DataItemIndex %>' />
<asp:LinkButton ID ="lb3" runat ="server" CommandName="GetData3" Text="方法3" CommandArgument ='<%#Container.DataItemIndex %>'/>
<input type ="hidden" runat ="server" id ="hid_UserName" value='<%#Eval("UserName")%>'/>
<input type ="hidden" runat ="server" id ="hid_UserEmail" value='<%#Eval("UserEmail")%>'/>
<asp:LinkButton ID ="lb4" runat ="server" CommandName="GetData4" Text="方法4" CommandArgument ='<%#Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserName" HeaderText="用户名" SortExpression="UserName" DataFormatString="< i> {0}/>"/>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web .UI .MobileControls;
public partial class Admin_ManageUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
SetBind();
}
private void SetBind()
{
DataSet ds = new DataSet();
string sConnectionString = @"server=TSONG;database=Forum;Trusted_Connection=True";
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("select * from tbUser", conn);
da.Fill(ds);
}
GridView2.DataSource = ds;
GridView2.DataBind();
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Del")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
string sUserID = GridView2.DataKeys[iIndex].Value.ToString();
string sConnectionString = @"server=TSONG;database=Forum;Trusted_Connection=True";
string sSql = "Delete from tbUser where UserID=@UserID";
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sSql, conn))
{
cmd.Parameters.AddWithValue("@UserID", sUserID);
ClientScript.RegisterStartupScript(Page.GetType(), "", string.Format("<script> alert('删除了{0}条记录');</script>)", cmd.ExecuteNonQuery()));
SetBind();
}
}
}
if (e.CommandName == "GetData1")
{
Response.Write(string.Format("您所单击的用户为:{0}", e.CommandArgument));
}
if (e.CommandName == "GetData2")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
GridViewRow gvr = GridView2.Rows[iIndex];
string sUserName = gvr.Cells[0].Text;
string sUserEmail = (gvr.Cells[1].FindControl("HyperLink1") as HyperLink).Text;
Response.Write(string.Format("您所单击的用户为:{0}<br>您所单击用户邮件地址为:{1}", sUserName, sUserEmail));
}
if (e.CommandName == "GetData3")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
GridViewRow gvr = GridView2.Rows[iIndex];
string sUserName = (gvr.FindControl ("hid_UserName") as HtmlInputHidden ).Value;
string sUserEmail = (gvr.FindControl("hid_UserEmail") as HtmlInputHidden).Value ;
Response.Write(string.Format("您所单击的用户为:{0}<br>您所单击用户邮件地址为:{1}", sUserName, sUserEmail));
}
if (e.CommandName == "GetData4")
{
int iIndex = Convert.ToInt16(e.CommandArgument);
string sUserID = GridView2.DataKeys[iIndex].Value.ToString();
string sConnectionString = @"server=TSONG;database=Forum;Trusted_Connection=True";
string sSql = "select * from tbUser where UserID=@UserID";
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sSql, conn))
{
cmd.Parameters.AddWithValue("@UserID", sUserID);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
if (dr.Read())
{
string sUserName = dr["UserName"].ToString();
string sUserEmail = dr["UserEmail"].ToString();
Response .Write (string .Format ("您所单击的用户:{0}<br>您所单击用户邮件地址为:{1}", sUserName, sUserEmail));
}
}
}
}
}
protected string FormatGenderData(string s)
{
string[] arrs = new string[] { "女", "男" };
return arrs[Convert.ToInt16(s)].ToString();
}
}
现在在学格式化Gridview,调试结果是这样子的: