GridView行索引的应用
来源:互联网 发布:python读取二进制文件 编辑:程序博客网 时间:2024/05/17 23:10
asp.net服务控件的ID属性可用于在aspx的后台代码中检索控件,从而获得或编辑控件属性。
但有时候希望能在客户端对服务器控件进行样式,内容的控制,这要完成两部分的工作:
1 能在客户端对服务器控件进行唯一定位(JQuery,javascript控制)。
2 能实现服务器端ID到客户端ID的转换(对于MasterPage和GridView中的模版列会出现服务器控件ID和ClientID不一致)
对GridView要将每一行区分出来,可以通过RowDataBound,在行绑定事件中通过e.Row.RowIndex来定位行数据。用e.Row对行数据进行编辑。RowDataBound事件会在对GridView进行数据绑定的时候调用,它会遍历GridView的每一行。
在后台代码中动态生成服务器控件时,如要加入JS代码,可能需要知道服务器控件的客户端ID,可以通过“服务器控件对象”. ClientID得到。
在js文件得到客户端ID可以在JS debug的时候 document.all 查看控件的客户端ID规则,得到ID的prefix,在js中检索对象的时候在服务器ID前加上这个prefix 即可。
<!--GridView Begin 销售订单明细 -->
<asp:GridView ID="gdvContent" runat="server" AllowPaging="false" AutoGenerateColumns="False" Width="100%" SkinID="gdvReport" OnRowDataBound="gdvContent_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="行号">
<ItemTemplate>
<asp:Label ID="lblLineNo" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="单一料号">
<ItemTemplate>
<asp:Label ID="txtItemNo" runat="server" Text='<%# Bind("dp_item_no") %>' ToolTip='<%# Bind("dp_item_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="生产商acer料号">
<ItemTemplate>
<asp:Label ID="txtProdItemNo" runat="server" Text='<%# Bind("pom080_ace_item") %>' ToolTip='<%# Bind("pom080_ace_item") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="品名">
<ItemTemplate>
<asp:Label ID="brand" runat="server" Text='<%# Bind("m01_v_model_no") %>' ToolTip='<%# Bind("m01_v_model_no") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="供应商代码">
<ItemTemplate>
<asp:Label ID="m01_vdr_code" runat="server" Text='<%# Bind("m01_vdr_code") %>' ToolTip='<%# Bind("m01_vdr_code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="预定数量">
<ItemTemplate>
<asp:Label ID="dp_dis_qty" runat="server" Text='<%# Bind("dp_dis_qty") %>' ToolTip='<%# Bind("dp_dis_qty") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="订单单价">
<ItemTemplate>
<asp:Label ID="dp_req_up" runat="server" Text='<%# Bind("dp_req_up") %>' ToolTip='<%# Bind("dp_req_up") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="客户需求到货日期CRD">
<ItemTemplate>
<asp:Label ID="dp_req_date" runat="server" Text='<%# Bind("dp_req_date","{0:d}") %>' ToolTip='<%# Bind("dp_req_date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="采购单价">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="采购单需求出货日期PRDCRD">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--GridView End -->
对应的后台代码:
protected void gdvContent_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 自动给第一列编号
if (e.Row.RowIndex > -1)
{
int lineNo = e.Row.RowIndex ;
e.Row.Cells[0].Text = Convert.ToString(lineNo+1);
/********* 单一料号 *************************/
HiddenField txtItemNo = new HiddenField();
txtItemNo.ID = "txtItemNo" + lineNo;
txtItemNo.Value = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["dp_item_no"];
e.Row.Cells[1].Controls.Add(txtItemNo);
/********* 生产商acer料号 *************************/
HiddenField txtProdItemNo = new HiddenField();
txtProdItemNo.ID = "txtProdItemNo" + lineNo;
txtProdItemNo.Value = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["dp_producer_item"];//TODO:Wesley
e.Row.Cells[2].Controls.Add(txtProdItemNo);
/********* 预定数量 *************************/
HiddenField txtItemQty = new HiddenField();
txtItemQty.ID = "txtItemQty" + lineNo;
txtItemQty.Value = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["dp_dis_qty"];
e.Row.Cells[5].Controls.Add(txtItemQty);
/********* 客户需求到货日期CRD *************************/
HiddenField txtCRD = new HiddenField();
txtCRD.ID = "txtCRD" + lineNo;
txtCRD.Value = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["dp_req_date"];
e.Row.Cells[7].Controls.Add(txtCRD);
/********* 采购单价 *************************/
TextBox tb1 = new TextBox();
tb1.ID = "txtPoUp" + lineNo;
if (strCurrencyP.Equals("U"))
{
tb1.Text = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["pur_price_u"];
}
else
{
tb1.Text = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["pur_price_n"];
}
tb1.Attributes.Add("onKeyPress", "return KeyCheck(this,event,1);");
e.Row.Cells[8].Controls.Add(tb1);
HiddenField tb2 = new HiddenField();
tb2.ID = "txtUSDUp" + lineNo;
tb2.Value = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["pur_price_u"];
e.Row.Cells[8].Controls.Add(tb2);
HiddenField tb3 = new HiddenField();
tb3.ID = "txtRMBUp" + lineNo;
tb3.Value = "" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["pur_price_n"];
e.Row.Cells[8].Controls.Add(tb3);
/********* 采购单需求出货日期PRD *************************/
TextBox txtPRD = new TextBox();
txtPRD.ID = "txtPRD" + lineNo;
txtPRD.Text = DateTime.Parse("" + ((System.Data.DataRowView)(e.Row.DataItem)).Row["dp_prd_date"]).ToString("yyyy-MM-dd");
e.Row.Cells[9].Controls.Add(txtPRD);
HyperLink hl = new HyperLink();
hl.Attributes.Add("href","javascript://") ;
ImageButton ib = new ImageButton();
ib.OnClientClick = "newCalWindow('',document.form1." + txtPRD.ClientID + ".value,'&Objfield=form1." + txtPRD.ClientID + "');return false;";
ib.Attributes.Add("src","../images/cal.gif");
ib.Attributes.Add("Width", "16");
ib.Attributes.Add("Height", "15");
ib.Attributes.Add("border", "0");
hl.Controls.Add(ib);
e.Row.Cells[9].Controls.Add(hl);
}
}
- GridView行索引的应用
- GridView获取行所在的索引
- 获得GridView行索引方法
- GridView得到关键字及行索引的三种情况
- GridView的RowCommand事件响应得到行索引号
- GridView的RowCommand事件中取得行索引
- GridView的RowCommand事件中取得行索引
- GridView的RowCommand事件中取得行索引 技巧
- GridView 的扩展应用
- gridview的应用点滴
- GridView的应用
- GridView的几个应用
- gridview的一些应用
- GridView组件的应用
- GridView的应用一
- GridView的基本应用
- gridview的datarow应用
- GridView的简单应用
- 我的JQuery动画
- My new home page www.cs.uic.edu/~lzhang3
- winsock error code reference
- 秋季如何养胃
- 秋季如何正确养胃
- GridView行索引的应用
- c# 未将对象引用设置到对象的实例的问题
- 各科目的计算机书籍
- C++模板类的编译问题
- Android学习笔记-1
- C#数组讨论一 System.object [,]与System. object [][]之间的区别
- iframe自适高度
- 用 exim4 和 popa3d 搭建一个简单可用的邮件系统
- IE8下debug模式的两个小问题