關(guān)于datagrid的使用以及動(dòng)態(tài)修改,以及使用存儲(chǔ)過程的介紹
發(fā)布時(shí)間:2008-07-14 閱讀數(shù): 次 來源:網(wǎng)樂原科技
1.關(guān)于Datagrid,Datalist等數(shù)據(jù)綁定控件
由于vs.net是Codebehind的, 而quickstart 上的例子是代碼與頁面混合的,所以許多朋友有些疑惑。特別是有關(guān)template中如何觸發(fā)事件的問題。
是這樣,許多server control都有一個(gè)commandname屬性
就是在.aspx中設(shè)置你那個(gè)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 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></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[i];
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.關(guān)于在asp.net中調(diào)用存儲(chǔ)過程
例子如下:
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);