关于datagrid的使用以及动态修改,以及使用存储过程的
1.关于Datagrid,Datalist等数据绑定控件
由于vs.net是Codebehind的, 而quickstart 上的例子是代码与页面混合的,所以许多朋友有些疑惑。特别是有关template中如何触发事件的问题。
是这样,许多server control都有一个commandname属性
就是在.aspx中设置你那个edit的commandname,在.cs中用e.CommandName 得到
如
///aspx页面
<asp:DataList id=usersList runat="server" ></P><P><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></P><P><template name="EditItemTemplate">
<span class="Normal">email (cookie auth) or DOMAINusername (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></P><P></asp:DataList></P><P></P><P>///cs文件
protected void UsersList_ItemCommand (Object sender, DataListCommandEventArgs e)
{
if (e.CommandName == "edit") {</P><P> // Set editable list item index if "edit" button clicked next to the item
usersList.EditItemIndex = e.Item.ItemIndex;</P><P> // Repopulate the datalist control
BindData();
}
else if (e.CommandName == "apply") {</P><P> // Obtain the current portal settings
PortalConfigurationData portalConfig = PortalConfiguration.LoadSettings(PortalConfiguration.ConfigFilePath);</P><P> // Update user entry
portalConfig.SecurityRoles[roleIndex].Users[e.Item.ItemIndex] = ((TextBox) e.Item.FindControl("userName")).Text;</P><P> // Persist Settings back to disk
PortalConfiguration.PersistSettings(portalConfig);</P><P> // Disable editable list item access
usersList.EditItemIndex = -1;</P><P> // Repopulate the datalist control
BindData();
}
else if (e.CommandName == "delete") {</P><P> // Obtain the current portal settings
PortalConfigurationData portalConfig = PortalConfiguration.LoadSettings(PortalConfiguration.ConfigFilePath);</P><P> // Delete selected User
String[] users = new String[portalConfig.SecurityRoles][roleIndex].Users.Length-1];</P><P> for (int i=0, j=0; i < portalConfig.SecurityRoles[roleIndex].Users.Length; i++) {</P><P> if (i != e.Item.ItemIndex) {</P><P> users[j] = portalConfig.SecurityRoles[roleIndex].Users;
j++;
}
}</P><P> // Update Users
portalConfig.SecurityRoles[roleIndex].Users = users;</P><P> // Persist Settings back to disk
PortalConfiguration.PersistSettings(portalConfig);</P><P> // Ensure that item is not editable
usersList.EditItemIndex = -1;</P><P> // Repopulate list
BindData();
}
}
2.关于在asp.net中调用存储过程
例子如下:
SQLConnection myConnection = new SQLConnection(PortalConfiguration.Settings.DatabaseConnectionString);
SQLCommand myCommand = new SQLCommand("AddMessage", myConnection);</P><P> // Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;</P><P> // Add Parameters to SPROC
SQLParameter parameterItemID = new SQLParameter("@ItemID", SQLDataType.Int, 4);
parameterItemID.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterItemID);</P><P> SQLParameter parameterTitle = new SQLParameter("@Title", SQLDataType.NVarChar, 100);
parameterTitle.Value = title;
myCommand.Parameters.Add(parameterTitle);</P><P> SQLParameter parameterBody = new SQLParameter("@Body", SQLDataType.NVarChar, 3000);
parameterBody.Value = body;
myCommand.Parameters.Add(parameterBody);</P><P> SQLParameter parameterParentID = new SQLParameter("@ParentID", SQLDataType.Int, 4);
parameterParentID.Value = parentId;
myCommand.Parameters.Add(parameterParentID);</P><P> SQLParameter parameterUserName = new SQLParameter("@UserName", SQLDataType.NVarChar, 100);
parameterUserName.Value = userName;
myCommand.Parameters.Add(parameterUserName);</P><P> SQLParameter parameterPortalID = new SQLParameter("@PortalID", SQLDataType.Int, 4);
parameterPortalID.Value = portalId;
myCommand.Parameters.Add(parameterPortalID);</P><P> SQLParameter parameterModuleID = new SQLParameter("@ModuleID", SQLDataType.Int, 4);
parameterModuleID.Value = moduleId;
myCommand.Parameters.Add(parameterModuleID);