关于datagrid的使用以及动态修改,以及使用存储过程的介绍
2024-07-21 02:24:48
供稿:网友
网站运营seo文章大全提供全面的站长运营经验及seo技术!1.关于datagrid,datalist等数据绑定控件
由于vs.net是codebehind的, 而quickstart 上的例子是代码与页面混合的,所以许多朋友有些疑惑。特别是有关template中如何触发事件的问题。
是这样,许多server control都有一个commandname属性
就是在.aspx中设置你那个edit的commandname,在.cs中用e.commandname 得到
如
///aspx页面
<asp:datalist id=userslist runat="server" >
<template name="itemtemplate">
<asp:imagebutton id=imagebutton1 runat="server" imageurl="../images/edit.gif" alternatetext="编辑条目" commandname="edit"></asp:imagebutton>
<asp:imagebutton id=imagebutton2 runat="server" imageurl="../images/delete.gif" alternatetext="删除条目" commandname="delete"></asp:imagebutton>
<asp:label id=lblrole runat="server" text="<%# container.dataitem%>" cssclass="normal"></asp:label>
</template>
<template name="edititemtemplate">
<span class="normal">email (cookie auth) or domain/username (windows auth)</span><br />
<asp:textbox id=username width="200" cssclass="normaltextbox" text="<%# container.dataitem %>" runat="server" />
<asp:linkbutton text="apply" commandname="apply" cssclass="commandbutton" runat="server" id=applybutton/>
</template>
</asp:datalist>
///cs文件
protected void userslist_itemcommand (object sender, datalistcommandeventargs e)
{
if (e.commandname == "edit") {
// set editable list item index if "edit" button clicked next to the item
userslist.edititemindex = e.item.itemindex;
// repopulate the datalist control
binddata();
}
else if (e.commandname == "apply") {
// obtain the current portal settings
portalconfigurationdata portalconfig = portalconfiguration.loadsettings(portalconfiguration.configfilepath);
// update user entry
portalconfig.securityroles[roleindex].users[e.item.itemindex] = ((textbox) e.item.findcontrol("username")).text;
// persist settings back to disk
portalconfiguration.persistsettings(portalconfig);
// disable editable list item access
userslist.edititemindex = -1;
// repopulate the datalist control
binddata();
}
else if (e.commandname == "delete") {
// obtain the current portal settings
portalconfigurationdata portalconfig = portalconfiguration.loadsettings(portalconfiguration.configfilepath);
// delete selected user
string[] users = new string[portalconfig.securityroles][roleindex].users.length-1];
for (int i=0, j=0; i < portalconfig.securityroles[roleindex].users.length; i++) {
if (i != e.item.itemindex) {
users[j] = portalconfig.securityroles[roleindex].users[i];
j++;
}
}
// update users
portalconfig.securityroles[roleindex].users = users;
// persist settings back to disk
portalconfiguration.persistsettings(portalconfig);
// ensure that item is not editable
userslist.edititemindex = -1;
// repopulate list
binddata();
}
}
2.关于在asp.net中调用存储过程
例子如下:
sqlconnection myconnection = new sqlconnection(portalconfiguration.settings.databaseconnectionstring);
sqlcommand mycommand = new sqlcommand("addmessage", myconnection);
// mark the command as a sproc
mycommand.commandtype = commandtype.storedprocedure;
// add parameters to sproc
sqlparameter parameteritemid = new sqlparameter("@itemid", sqldatatype.int, 4);
parameteritemid.direction = parameterdirection.output;
mycommand.parameters.add(parameteritemid);
sqlparameter parametertitle = new sqlparameter("@title", sqldatatype.nvarchar, 100);
parametertitle.value = title;
mycommand.parameters.add(parametertitle);
sqlparameter parameterbody = new sqlparameter("@body", sqldatatype.nvarchar, 3000);
parameterbody.value = body;
mycommand.parameters.add(parameterbody);
sqlparameter parameterparentid = new sqlparameter("@parentid", sqldatatype.int, 4);
parameterparentid.value = parentid;
mycommand.parameters.add(parameterparentid);
sqlparameter parameterusername = new sqlparameter("@username", sqldatatype.nvarchar, 100);
parameterusername.value = username;
mycommand.parameters.add(parameterusername);
sqlparameter parameterportalid = new sqlparameter("@portalid", sqldatatype.int, 4);
parameterportalid.value = portalid;
mycommand.parameters.add(parameterportalid);
sqlparameter parametermoduleid = new sqlparameter("@moduleid", sqldatatype.int, 4);
parametermoduleid.value = moduleid;
mycommand.parameters.add(parametermoduleid);