GridView学习总结

greatverve 2008-09-19 08:15:16
1.不设置模板,直接绑定数据,对于GridView是可以的.但是无法控制显示,或者更复杂的功能.
2.模板研究
<asp:GridView ID="GVTemplate" runat="server"
AutoGenerateColumns="False" //使用模板一般不自动生成列.
AllowPaging="True" //分页
AllowSorting="True" //排序
AutoGenerateDeleteButton="True" //生成删除按钮
AutoGenerateEditButton="True" //生成编辑按钮
OnPageIndexChanging="GridView1_PageIndexChanging" //页码改变事件.
OnRowCancelingEdit="GridView1_RowCancelingEdit" //取消编辑按钮事件.
OnRowDeleting="GridView1_RowDeleting" //删除事件
OnRowEditing="GridView1_RowEditing" //编辑事件
DataKeyNames="UserID" //设置主键字段,很重要,删除的时候会用到.
PageSize="3"> //每页显示条数
<Columns> //列模板
<asp:BoundField DataField="Id" /> //绑定string/nvarchar类型,一般用BoundField
<asp:CheckBoxField DataField="Checkout" /> //绑定bool/bit

<asp:TemplateField> //模板列,可以绑定复杂的数据,或者更多控件.
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings //设置分页功能
FirstPageText="首页"
LastPageText="尾页"
NextPageText="下一页"
PreviousPageText="上一页"
Mode="NumericFirstLast" //分页模式
PageButtonCount="3" //分页用户界面中要显示的页数.
Position="Top" /> //分页条的位置
</asp:GridView>
...全文
302 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
greatverve 2008-09-19
  • 打赏
  • 举报
回复

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
SQLConstructor.BLL.User user = new SQLConstructor.BLL.User();
this.GVTest.DataSource = user.getAllUser();
this.GVTest.DataBind();
GridView1.DataSource = user.getAllUser();
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;//页码取得的方法.再次绑定数据时,GridView根据新页码自动筛选数据.
this.Bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;//编辑行索引的取得方法,通过事件参数.
this.Bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;//取消
this.Bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int UserID = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
Response.Write(UserID.ToString());
//执行删除代码
//pro.deleteproduct(UserID);
this.Bind();
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('删除成功!')</script>", false);
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string userid = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取主键
GridViewRow myrow = GridView1.Rows[e.RowIndex]; //取得GridView的行
Label lb = (Label)myrow.FindControl("Label1");
Response.Write(lb.Text);
//string id = ((TextBox)myrow.Cells[1].Controls[0]).Text;
/*Cell[0]代表第一列,如果第一列是模板列.则里面的控件可能不至一个.
* Cell[0].Controls[1]代表第二个控件.
*/
//string name =((TextBox)myrow.Cells[1].Controls[0]).Text;//获取选中行单元格数据
//string pwd = ((LiteralControl)myrow.Cells[2].Controls[2]).Text;
//pwd += ((Label)myrow.Cells[2].Controls[1]).Text;
//pwd += "这里的pwd的结束";
//name += "";// myrow.Cells[2].Controls[0].GetType().ToString();
//Response.Write("id" + name + pwd);
////更新代码
//foreach (Control c in myrow.Cells[0].Controls)
//{
// Response.Write(c.GetType().ToString()+"<br/>");
//}

GridView1.EditIndex = -1;
this.Bind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧