asp.net 控件常用代码 及 其他

来源:互联网 发布:js实现秒杀倒计时 编辑:程序博客网 时间:2024/04/23 21:00

控件:

一,选项控件:

l Dropdownlist:

数据绑定:

方法1:

this.ddlFatherIndustry.DataSource=dt;

this.ddlFatherIndustry.DataTextField="名称";

this.ddlFatherIndustry.DataValueField="fatherid";

this.ddlFatherIndustry.DataBind();

方法2:

ddlUnite.Items.Clear();//清楚所有选项

this.ddlUnite.Items.Add(new ListItem("请选择部门","-1"));

DataTable objTable=new DataTable();

objManage=new Lilosoft.PA.EAccount.Manage();

ret=objManage.GetAllDept(out objTable,out strErr);

foreach(DataRow row in objTable.Rows)

{

this.ddlUnite.Items.Add(new ListItem(row["name"].ToString(),row["id"].ToString()));

}

得到选中的值:ddlPersonnel.SelectedItem.Value

加入两个页面有相同的Dropdownlist,一个页面选中一个值到另一个页面,那么另一个页面的ddl要初始本身包含的选中项:

if( Request["Dept"] != null )

{

string dept = Request["Dept"].ToString();

ddlDept.SelectedIndex=ddlDept.Items.IndexOf(ddlDept.Items.FindByValue(dept));

}

超连接:

Hyperlink3.NavigateUrl = "PersonnelCompare.aspx?Cycle=" + ddlCycle.SelectedItem.Value + "&Dept=" + ddlDept.SelectedItem.Value + "&Personnel=" + ddlPersonnel.SelectedItem.Value ;

l RadioButtonList

clip_image002

1, 得到选定项的值:属性:RadioButtonList1.SelectedValue

2, 设置选中项:RadioButtonList1.Items[0].Selected = true;

二,DataGrid

DataGrid: e.Item.ItemIndex 下标索引Header是 -1,每行数据Item是从 0 开始的。

数据绑定时(事件:DGList_ItemDataBound,即绑定每行后执行的事件)

if(e.Item.ItemIndex==-1)return;

int id=int.Parse(e.Item.Cells[0].Text);

e.Item.Cells[1].Text=objOrganize.GetDeptNameByID(id,out strErr);

#region 2006-7-18 加入连接

string TempDept = e.Item.Cells[1].Text;

e.Item.Cells[4].Text = ""+ e.Item.Cells[4].Text +"";

事件出发(事件:dgList_ItemCommand,即在DataGrid中出发事件)

//根据CommandName的名字来判断做什么。//

// 触发删除事件

if(e.CommandName == "delete")

{

string id = e.Item.Cells[0].Text;

DataTable td1 = new DataTable();

objIndicatorMgmt.ListPersonByContentID(id,out td1,out strErr);

if(td1.Rows.Count > 0)

{

Response.Write("");

return;

}

objIndicatorMgmt.DelContentById(id,out strErr);

dgList.EditItemIndex = -1;

DGDataBind();

}

改变DataGrid每行数据,但不改变表头

if(e.Item.ItemType !=ListItemType.Header)

{

e.Item.Cells[6].Text=(e.Item.Cells[6].Text=="False"?"否":"是");

e.Item.Cells[7].Text=(e.Item.Cells[7].Text=="False"?"否":"是");

}

三,HyperLink1:超链接控件导航设置

HyperLink1.NavigateUrl = "ResultSearch_Chart.aspx?period="+ddlCycle.SelectedValue;

其它常用:

GridView中某一列隐藏,并能取到值。

.hidden { display:none;}

在gridview中某一列上 ItemStyle-CssClass="hidden" HeaderStyle-CssClass="hidden"

光爆效果:DataGrid1_ItemDataBound事件

if (e.Item.ItemType !=ListItemType.Header)

{ //给每行加js光爆效果

e.Item.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#EEEEFF'");

e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=color");

}

小窗口、页面打开、弹出、关闭:

Response.Write("");

返回JS:

1,关闭窗口:Response.Write("");

Response.Write("");

3, 弹出对话框并跳转(利用工具类JavaScriptOB: JavaScriptOB.AlertAndRedirect("您没有审核权限!", "JBu_List.aspx");

4, 显示父页面,本页面关闭,父页面刷新

Response.Write("");

5, 弹出子页面:

//在.cs页面加

btnSupplyAdd.Attributes.Add("onclick", "javascript:OpenClientInfo('Client_Supply_Dealer.aspx?clientcode=" + clientcode + "','','630px')");

6,删除确认:

OnClientClick="return confirm('确定删除?')"

6, 点击按钮直接跳转:onClick="javascript:location.href='SmsGroup?oprater=List'"

隐藏域:

可以隐藏,但在j2ee中,控件disabled=true后,得不到这个html控件的值

原创粉丝点击