【ASP.NET】控件应用总结(更新中)

文档目录
1. DataGrid
2. DropDownList
3. GridView
4. Repeater
5. TreeView
6. IFRAME
 
 
 
 
Datagrid
 
A. 删除提示
 
第一种方法: 直接在页面加上javascript代码
第二种方法://缺点:整个单元格单击都会删除 private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { e.Item.Cells[3].Attributes.Add(" confirm('您真的要删除此行吗?');"); }
第三种方法: 删除
" HeaderText=" 删除" CommandName="Delete"> 如果是是模版列中的button,则在form_load或其他事件里添加 Button1.Attributes.Add(" confirm('确定删除');");
 
 
 
 
DropDownList
 
A. 绑定
DropDownList1.DataSource =user.bindRoleName();
DropDownList1.DataTextField = "RoleName";
DropDownList1.DataValueField = "RoleName";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem( "- 请选择-","0"));
 
 
 
GridView
 
A. RowDataBound 事件
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
         {
              ((HyperLink)e.Row.Cells[3].Controls[0]).Attributes.Add(" "javascript:window.open('RolePermission.aspx?ID=" + DataBinder.Eval(e.Row.DataItem, "UserID").ToString() + "&Name=" + DataBinder.Eval(e.Row.DataItem, "UserName").ToString() + "&up=role', 'Install', 'left=700,top=180,height=60,width=80'); ");
              ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add(" "javascript:return confirm(' 你确认要删除:\"" + e.Row.Cells[1].Text + "\"?')");
         }
     }
 
}              
 
B. GridView 索引超出范围。必须为非负值并小于集合大小。参数名: index解决方案
指定: DataKeysNames="stid"
 
C. <PagerStyle CssClass="cssPager" /> 页码样式
(1)< style type="text/css">                 
         .cssPager span { font-weight:bold; color:Red; padding-left:3px; padding-right:3px;}  
         </style>
(2)< style type="text/css">
.page span{
background :#FFFFFF none repeat scroll 0 0;
border :1px solid #B2BFCF;
display :block;
float :left;
height :18px;
line-height :18px;
margin-right :4px;
padding :0 5px;
}
</ style >
 
D .没数据显示表头
public void NoRecordBuild(GridView gridView, DataSet ds)
    {
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gridView.DataSource = ds;
            gridView.DataBind();
            int columnCount = gridView.Rows[0].Cells.Count;
            gridView.Rows[0].Cells.Clear();
            gridView.Rows[0].Cells.Add(new TableCell());
            gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
            gridView.Rows[0].Cells[0].Text = "No Records Found.";
        }
        else
        {
            gridView.DataSource = ds;
            gridView.DataBind();
        }
    }
 
 
 
Repeater
 
A. ItemDataBound 用法
protected void Repeater4_ItemDataBound(object sender, RepeaterItemEventArgs e)
     {
         LinkButton lkb = (LinkButton)e.Item.FindControl("LinkButton4");
         lkb.Attributes.Add(" "return confirm(' 确认要删除吗');");
         DataRowView gv = (DataRowView)e.Item.DataItem;
         Literal Literal2 = (Literal)e.Item.FindControl("Literal2");
         Literal2.Text = gv["Area"].ToString();
     }
B. 笔记:asp:repeater 两列显示
<table>
<asp:repeater id="myRepeater" runat="server">
<ItemTemplate>
   <tr>
    <td>
    
第一列
    </td>
</ItemTemplate>
<AlternatingItemTemplate>
   <td>
   
第二列
   </td>
   </tr>
</AlternatingItemTemplate>
</asp:repeater>
</table>
 
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (i % 2 == 0)
    {
        e.Item.Controls.Add(new LiteralControl("</tr><tr>"));
    }
    i++;//
每一条记录绑定的时候就会加1
}
 
 
TreeView
 
A. 树型加载
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTree(TreeView1.Nodes, "0");
            }
            foreach (TreeNode tn in TreeView1.Nodes)
            {
                readNotes(tn);
 
            } 
 
        }
 
B.绑定
        private void BindTree(TreeNodeCollection nds, string parentId)
        {
           
            TreeNode tn = null;
            DataTable dt = GetTreeTable();
            foreach (DataRow dr in dt.Select("[ParentID]='" + parentId + "'"))
            {
                tn = new TreeNode(dr["ChnName"].ToString(), dr["ChnID"].ToString());
                //tn.ShowCheckBox = true;
                tn.Target = "mainFrame";
                tn.NavigateUrl = "../DocBk/docBkMainRight.aspx?LeafID=" + dr["ChnID"].ToString();
                nds.Add(tn);
 
                BindTree(tn.ChildNodes, dr["ChnID"].ToString());
                tn.SelectAction = TreeNodeSelectAction.Expand;
                tn.CollapseAll();
                // 扩展DTS
                if (parentId == "0")
                {
                    tn.Expand();
                }
            }
        }
        public DataTable GetTreeTable()
        {
            DataTable dt = new DataTable();
            dt = Component.SystemUtility.GetCatalogs();
            HttpContext.Current.Cache.Insert("Treeview", dt);
            return dt;
        }
C.读取
        private void readNotes(TreeNode tn)
        {
            foreach (TreeNode tnChild in tn.ChildNodes)
            {
                readNotes(tnChild);
            }
        }
D.扩展TreeView
        private void parentExpand(TreeNode tnChild)
        {
            if (tnChild.Parent.Depth != 0)
            {
                tnChild.Parent.Expand();
                parentExpand(tnChild.Parent);
            }
        }
        
         //get TreeView Nodes
        StringBuilder sb = new StringBuilder();
        foreach (TreeNode tn in SmartTreeView1.CheckedNodes)
        {
            sb.Append(tn.Value + ",");
 
        }
       
<Ifram />应用
A Src赋值方法:
this .frameR.Attributes.Add("src", "CatalogAddFrame.aspx?n=" + pID);

你可能感兴趣的:(GridView,asp.net,控件,treeview,dropdownlist)