仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1080|回复: 8
打印 上一主题 下一主题

[学习教程] ASP编程:在DataGrid里增加DropDownLit控件

[复制链接]
精灵巫婆 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 23:09:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
SQLServer是基于服务器端的中型的数据库,可以适合大容量数据的应用,在功能上管理上也要比Access要强得多。在处理海量数据的效率,后台开发的灵活性,可扩展性等方面强大。datagrid|控件?

UsingDropDownListcontrolinDataGrid
ByEricZheng

WhenIwasdevelopingawebapplicationcoupledaysago,Ifoundsomeinterestingthingsaboutthedatagrid,Iwanttosharethemwithothervs.netprogrammers,soIwrotethisarticle.ThisarticlewilldemonstratehowtouseDropDownListcontrolindatagrid.



TheessentialpartoftheDropDown.aspxfileisthefollowing:



Insecondline,wesetthedatasourceofthedropdownlistcontroltoafunctionGetCategory(),thisfunctionfetchestheCategoryrecordsfromdatabaseandreturnsadatatable.Inthelastline,wesettheSelectedIndextoafuncitonGetCategoryID,thisfunctiontakesthecurrentCategorynameasitsargument,andreturnsthelocaiton(aninteger)oftheCategoryname,thisenablestheDorpDownListcontroltodisplaythecorrectCategorynameforthecurrentrecord.

ThefollowingistheC#code:

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.OleDb;
usingSystem.Configuration;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;

namespaceManagement
{

publicclassDropDown:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.DataGridProductGrid;
protectedDataTable_category;

//newadatabaseclasstogetrecords,ProductDbisapublicclass
//containingseveralfunctions
protectedProductDbpdb=newProductDb();

publicDropDown()
{
Page.Init+=newSystem.EventHandler(Page_Init);
}

privatevoidPage_Load(objectsender,System.EventArgse)
{
if(!IsPostBack)
{
BindProduct();

}
}

privatevoidPage_Init(objectsender,EventArgse)
{
InitializeComponent();
}

voidBindProduct()
{
//pdb.GetProduct()returnsadatatabletoProductsdatagrid
ProductGrid.DataSource=pdb.GetProduct();
ProductGrid.DataBind();
}

protectedvoidProduct_Edit(objectsender,DataGridCommandEventArgse)
{

BindCategory();
((DataGrid)sender).EditItemIndex=e.Item.ItemIndex;
BindProduct();

}

protectedvoidProduct_Cancel(objectsender,DataGridCommandEventArgse)
{
ProductGrid.EditItemIndex=-1;
BindProduct();

}
protectedvoidProduct_Update(objectsender,DataGridCommandEventArgse)
{
//getthecurrnetproductname
stringpname=e.Item.Cell[1].Controls[0].Text;
//getthecurrentproductprice
stringprice=e.Item.Cell[2].Controls[0].Text;
//getthecurrentcategoryid
DropDownListddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
stringcategoryid=ddl.SelectedItem.Value;
//getthecurrentproductid
stringpid=e.Item.Cell[4].Controls[0].Text;

//callpdbsupdatefunction
pdb.update(pid,pname,price,categoryid);

ProductGrid.EditItemIndex=-1;
BindProduct();


}
voidBindCategory()
{
//pdb.FetchCategory()returnsadatatable
_category=pdb.FetchCategory();

}

protectedDataTableGetCategory()
{
return_category;
}

protectedintGetCategoryID(stringcname)
{
for(inti=0;i<_category.DefaultView.Count;i++)
{
if(_category.DefaultView[i]["categoryname"].ToString()==cname)
{
returni;
}
}
return0;

}



#regionWebFormDesignergeneratedcode
///
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);

}
#endregion


}
}

ThekeypointsofthisC#fileare:

1.InProduct_Edit()function,youhavetocallBindCategory()tosetthe_categorydatatablefirst,andthensettheEditItemIndexforthedatagrid,andatlast,callBindProduct()function.TheDropDownListcontrolwillnotdisplayanytingifyoureversethisorder.Beca</p>ASP是依赖组件的,能访问数据库的组件好多就有好多种,再有就是你微软的工具可是什么都要收钱的啊!
简单生活 该用户已被删除
沙发
发表于 2015-1-17 11:20:27 | 只看该作者
运用ASP可将VBscript、javascript等脚本语言嵌入到HTML中,便可快速完成网站的应用程序,无需编译,可在服务器端直接执行。容易编写,使用普通的文本编辑器编写,如记事本就可以完成。由脚本在服务器上而不是客户端运行,ASP所使用的脚本语言都在服务端上运行。
若天明 该用户已被删除
板凳
发表于 2015-1-20 17:27:17 | 只看该作者
用户端的浏览器不需要提供任何别的支持,这样大提高了用户与服务器之间的交互的速度。
爱飞 该用户已被删除
地板
发表于 2015-1-29 13:24:15 | 只看该作者
哪些内置对象是可以跳过的,或者哪些属性和方法是用不到的?
分手快乐 该用户已被删除
5#
发表于 2015-1-30 19:28:20 | 只看该作者
学习ASP其实应该上升到如何学习程序设计这种境界,其实学习程序设计又是接受一种编程思想。比如ASP如何学习,你也许在以前的学习中碰到过。以下我仔细给你说几点:
莫相离 该用户已被删除
6#
发表于 2015-2-6 15:17:19 | 只看该作者
运用ASP可将VBscript、javascript等脚本语言嵌入到HTML中,便可快速完成网站的应用程序,无需编译,可在服务器端直接执行。容易编写,使用普通的文本编辑器编写,如记事本就可以完成。由脚本在服务器上而不是客户端运行,ASP所使用的脚本语言都在服务端上运行。
活着的死人 该用户已被删除
7#
发表于 2015-2-16 20:12:01 | 只看该作者
跟学别的语言一样,先掌握变量,流程控制语句(就是ifwhileselect)等,函数/过程,数组
8#
发表于 2015-3-5 09:28:15 | 只看该作者
下面简单介绍一下我学习ASP的方法,希望对想学习ASP的朋友有所帮助...
第二个灵魂 该用户已被删除
9#
发表于 2015-3-12 02:42:27 | 只看该作者
Application:这个存储服务端的数据,如果不清除,会直到web应用程序结束才清除(例如重启站点)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-6 05:45

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表