GridView行索引的应用

来源:互联网 发布:python读取二进制文件 编辑:程序博客网 时间:2024/05/17 23:10

asp.net服务控件的ID属性可用于在aspx的后台代码中检索控件,从而获得或编辑控件属性。

但有时候希望能在客户端对服务器控件进行样式,内容的控制,这要完成两部分的工作:

1 能在客户端对服务器控件进行唯一定位(JQuery,javascript控制)

2 能实现服务器端ID到客户端ID的转换(对于MasterPageGridView中的模版列会出现服务器控件IDClientID不一致)

 

GridView要将每一行区分出来,可以通过RowDataBound,在行绑定事件中通过e.Row.RowIndex来定位行数据。用e.Row对行数据进行编辑。RowDataBound事件会在对GridView进行数据绑定的时候调用,它会遍历GridView的每一行。

 

在后台代码中动态生成服务器控件时,如要加入JS代码,可能需要知道服务器控件的客户端ID,可以通过“服务器控件对象”. ClientID得到。

js文件得到客户端ID可以在JS debug的时候 document.all 查看控件的客户端ID规则,得到IDprefix,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>

                                                          &nbsp;                                        

                                                </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);

           

        }

 

}

原创粉丝点击