GridView行索引的應用

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

           

        }

 

}

發佈了43 篇原創文章 · 獲贊 3 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章