GridView使用一些記錄

現在有個項目開始使用ASP.NET 2.0開發,在開發過程中關於GridView的使用的一些零星的記錄
1.TemplateField中的綁定

None.gif<asp:TemplateField>
ExpandedBlockStart.gifContractedBlock.gif                        
<ItemTemplate><%dot.gifEval("OCompany.Name"%></ItemTemplate>
None.gif                    
</asp:TemplateField>

2.通過CheckBox得到選擇行的主健值(GridView支持多主健)
None.gif        private string GetSelectId()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
//EnsureChildControls();
InBlock.gif
            GridView gvlist1 = (GridView)this.Controls[0].FindControl("gvlist");
InBlock.gif
InBlock.gif            
int icount = gvlist1.Rows.Count;
InBlock.gif
InBlock.gif            
string id = string.Empty;
InBlock.gif
InBlock.gif            
for (int i = 0; i < gvlist1.Rows.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CheckBox cb 
= (CheckBox)gvlist1.Rows[i].FindControl("cb_id");
InBlock.gif
InBlock.gif                 
//得到選中行的信息
InBlock.gif
                if (cb.Checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataKey dkr 
= gvlist1.DataKeys[i];
InBlock.gif
InBlock.gif                    id 
= dkr.Value.ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return id;
ExpandedBlockEnd.gif        }

3.動態創建TemplateField
None.gifprivate void TemplateColumn(GridView gvlist, string column, GridListAttribute att)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            TemplateField tf 
= new TemplateField();
InBlock.gif
InBlock.gif            tf.HeaderText 
= att.HeaderText;
InBlock.gif            tf.HeaderStyle.Width 
= Unit.Parse(att.Width);
InBlock.gif
InBlock.gif            ColumnTemplate temp 
= new ColumnTemplate();
InBlock.gif
InBlock.gif            temp.Column 
= att.Column;
InBlock.gif
InBlock.gif            tf.ItemTemplate 
= temp;
InBlock.gif
InBlock.gif            gvlist.Columns.Add(tf);
InBlock.gif     
InBlock.gif            
ExpandedBlockEnd.gif        }

None.gif
ColumnTemplate類
None.gifpublic class ColumnTemplate : ITemplate
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
//第一個模板列 
InBlock.gif
    public void InstantiateIn(Control container)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        LiteralControl l 
= new LiteralControl();
InBlock.gif        l.DataBinding 
+= new EventHandler(this.OnDataBinding);
InBlock.gif        container.Controls.Add(l);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void OnDataBinding(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
InBlock.gif            LiteralControl lt 
= (LiteralControl)sender;//LiteralControl發送綁定請求
InBlock.gif

InBlock.gif            GridViewRow container 
= (GridViewRow)lt.NamingContainer;
InBlock.gif
InBlock.gif            lt.Text 
= DataBinder.Eval(container.DataItem, column).ToString();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章