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

ASP.NET--GridView配合DetailsView初使用

2015-10-15 16:54 671 查看
1.GridView与DetailsView中的绑定模板不可以进行编辑的问题

方法:将要编辑的列转换为模板列---TemplateField

<EditItemTemplate></EditItemTemplate>---编辑模板

<InsertItemTemplate></InsertItemTemplate>---新建项模板

<ItemTemplate></ItemTemplate>---显示项模板

注:在对应的模板中就可以使用相应的控件,,使用空间是要记得编辑模板列,让控件绑定相应的属性/列名,如图





2.GridView与DetailsView中要实现增删改等操作没反应

方法:重要属性,给空间指定表中的主键值DataKeyNames=主键列/属性

3.GridView中要将某列设置为超链接状态



方法:用到控件中的HyperlinkField--作用是将某一列设置为超链接状态

DataTextField--要绑定的列名或属性名

DataNavigateUrlField--超链接要传的值,一般绑定主键

DataNavigateUrlFormatString--超链接传值的格式--要连接的地址?id={0} [{0}]--是占位符,,占的是DataNavigateUrlField的值

注:在GridView配合DetailsView使用中 GridView只实现删除的方法,DetailsView实现增、改的方法

GridView跳转到DetailsView是要将设置好的DataNavigateUrlField中的主键值传递给DetailsView,

DetailsView配置数据源时定义来源是QueryString 键的名字--要连接的地址?id={0}中的id

4.DetailsView中的几个事件

ItemInserted--项插入完成后的操作

项目中用到的是插入完成后跳转的别的页面

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
//数据插入完成后跳转页面
Response.Redirect("Teacher.aspx");
}



ItemUpdating事件--在修改之前,将控件空的值传递给修改模板


protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
CheckBox ckm = (CheckBox)DetailsView1.FindControl("rdoMale");
CheckBox ckf = (CheckBox)DetailsView1.FindControl("rdoFamle");
}


5.数据绑定时怎样控制GridView中某一单元格的值

RowDataBound事件--数据绑定完成后触发---三步1.获取2.修改3.显示

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//判断成绩是否已经录入,,没录入显示------未录入成绩

//判断是否是数据行
if (e.Row.RowType==DataControlRowType.DataRow)
{
//是数据行就将行中的对象获取-----获取
v_Score data = e.Row.DataItem as v_Score;
//修改
TextBox txt = e.Row.Cells[5].FindControl("txtScore") as TextBox;
//显示
txt.Text = data.Score == -1 ? "未录入成绩" : data.Score.ToString();
}
}


6.点击按钮实现录入成绩,要循环GridView

在表格中找到某一单元格方法:

GridView1.Rows[i].Cells[5].FindControl("txtScore") as TextBox




//点击录入成绩
protected void btnUpdateScore_Click(object sender, EventArgs e)
{
//循环表格中的每一行,,把主键值,和分数值提取出来(转换成模板之后才能提取)
for (int i = 0; i < GridView1.Rows.Count; i++)
{
TextBox txt = GridView1.Rows[i].Cells[5].FindControl("txtScore") as TextBox;
Label lbl = GridView1.Rows[i].Cells[6].FindControl("lblEID") as Label;
int eid = Convert.ToInt32(lbl.Text);
double score = Convert.ToDouble(txt.Text);
int rel = new ElectiveBll().Input(eid, score);
if (rel>0)
{
lblMSG.Text = "录入成功!";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: