一
在DataSource元件要使用Transaction,可以增加一個參考dll(system.transactions.dll),此dll只支援.Net 2.0以上,然後使用TransactionScope來在指定區域內做Transaction
參考資料網址:http://msdn.microsoft.com/zh-tw/library/system.transactions.transactionscope(VS.80).aspx
using System.Transactions;
using (TransactionScope scope = new TransactionScope())
{
SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.DeleteCommand = "delete from table_name ";
SqlDataSource2.Update();
scope.Complete();
}
二
GridView執行onCommand時,要如何取得執行列的此筆Row資料呢?
假設有一個GridView如下:(需先把RowData的Index放置於CommandArgument參數裡,以便取用)
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="mybutton" runat="server" CommandArgument='<%# Container.DataItemIndex %>' OnCommand="Button_Click"
Text="Down" CommandName="buttonit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
然後利用ExtractRowValues這個function把所有Rows的Columns的key及值取出,放到OrderedDictionary物件裡,這個方法是參考DataControlField的ExtractRowValues方法的。
private OrderedDictionary ExtractRowValues(DataControlFieldCollection Columns, GridViewRow Row)
{
OrderedDictionary oFieldValues;
DataControlField oColumn;
OrderedDictionary oDictionary;
oFieldValues = new OrderedDictionary(Columns.Count);
oDictionary = new OrderedDictionary();
for (int i = 0; i <= Columns.Count - 1; i++)
{
oColumn = Columns[i];
if (oColumn.Visible)
{
oDictionary.Clear();
oColumn.ExtractValuesFromCell(oDictionary, (DataControlFieldCell)Row.Cells[i], Row.RowState, true);
foreach (DictionaryEntry oEntry in oDictionary)
{
oFieldValues[oEntry.Key] = oEntry.Value;
}
}
}
return oFieldValues;
}
需using 空間
using System.Collections.Specialized;
就可以於OnComman的function裡直接取用此Row的每一個Field的值了
public void Button_Click(object sender, CommandEventArgs e) {
int rowid = Convert.ToInt32(e.CommandArgument.ToString());
GridViewRow oRow = (GridViewRow)GridView1.Rows[rowId ];
OrderedDictionary oFieldValues = ExtractRowValues(GridView1.Columns, oRow);
String fiel_value = oFieldValues["field_name"].ToString();
}