您的位置:首页 > 编程语言 > ASP

ASP.NET设计GridView控件的使用方法

2013-06-20 18:34 447 查看
页面GridView控件设计代码:

<asp:GridView ID="Commentslist" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="UserID"
OnPageIndexChanging="Commentslist_PageIndexChanging"
OnRowDeleting="Commentslist_RowDeleting"
OnSelectedIndexChanging="Commentslist_Looking" PageSize="5">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="用户ID">
<HeaderStyle Height="30px" Width="100px" />
<ItemStyle Height="30px" Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="用户名字">
<HeaderStyle Height="30px" Width="100px" />
<ItemStyle Height="30px" Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="CommentID" HeaderText="编号ID">
<HeaderStyle Height="30px" Width="60px" />
<ItemStyle Height="30px" Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="CommentInfor" HeaderText="内容">
<HeaderStyle Height="30px" Width="250px" />
<ItemStyle Height="30px" Width="250px" />
</asp:BoundField>
<asp:CommandField HeaderText="查看" SelectText="查看" ShowSelectButton="True">
<HeaderStyle Height="30px" Width="50px" />
<ItemStyle Height="30px" Width="50px" />
</asp:CommandField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True">
<HeaderStyle Height="30px" Width="50px" />
<ItemStyle Height="30px" Width="50px" />
</asp:CommandField>
</Columns>
</asp:GridView>


后台代码实现:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
this.BindToGirdView();
}

private void BindToGirdView()
{
string Userid = Session["userid"].ToString();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["db"].ToString();
string sqlStr = "select * from Comment where UserID=" + Userid + "or UserID in (select FriendsID from Friends where UserID=" + Userid + ")";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConnection);
DataSet ds = new DataSet();
sqlConnection.Open();
sda.Fill(ds, "Comments");
sqlConnection.Close();

if (ds.Tables["Comments"].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
Commentslist.DataSource = ds.Tables[0].DefaultView;
Commentslist.DataBind();
Commentslist.Rows[0].Cells.Clear();
}
else
this.Commentslist.DataSource = ds.Tables["Comments"].DefaultView;
this.Commentslist.DataBind();
}

protected void Commentslist_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Commentslist.PageIndex = e.NewPageIndex;
this.BindToGirdView();
}

protected void Commentslist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string Userid = Session["userid"].ToString();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["db"].ToString();
string sqlStr = "select * from Comment where UserID=" + Userid + "or UserID in (select FriendsID from Friends where UserID=" + Userid + ")";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, sqlConnection);
DataSet ds = new DataSet();
sqlConnection.Open();
sda.Fill(ds, "Comments");
sqlConnection.Close();
if (ds.Tables["Comments"].Rows.Count == 0)
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message",
"<script language='javascript' defer>alert('列表为空,删除失败!');</script>");
else
{
int CommentID = Convert.ToInt32(this.Commentslist.Rows[e.RowIndex].Cells[2].Text);
int result1 = BLL.DeleteCommentData(CommentID);
//int result2 = BLL.DeleteAnswerCommentData(CommentID);
//if (result1 > 0 && result2 > 0)
if (result1 > 0)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message",
"<script language='javascript' defer>alert('删除成功!');</script>");
this.BindToGirdView();
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message",
"<script language='javascript' defer>alert('删除失败!');</script>");
this.BindToGirdView();
}
}
}

protected void Commentslist_Looking(object sender, GridViewSelectEventArgs e)
{
Session["commentid"] = Convert.ToInt32(this.Commentslist.Rows[e.NewSelectedIndex].Cells[2].Text);
Response.Redirect("AnswerCommentlist.aspx");
}


BindToGirdView()函数中的if语句可以令该表中的数据为空的时候显示出表头,如果没有这个语句,当数据为空时表格则不会显示出来。

“this.Commentslist.Rows[e.NewSelectedIndex].Cells[2].Text”语句可以用来获取表格中的数据,Rows[e.NewSelectedIndex]表示当前选择的行,Cells[]用来标识获取哪一列中的数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: