2010年11月29日星期一

Some code-snippet in GridView, ListView control

Comparing ListView with GridView,DataList and Repeater
http://weblogs.asp.net/anasghanem/archive/2008/09/06/comparing-listview-with-gridview-datalist-and-repeater.aspx

1. ListViewItem listEditItem = lvJournals.Items[lvJournals.EditIndex];

2. protected void odsJournals_Updating(object Sender, ObjectDataSourceMethodEventArgs E)
{
E.InputParameters["JournalId"] = GetCurrentColumnItemId();
E.InputParameters["ModifiedBy"] = LogonUser.FullName;

3. protected override void CurrentFormObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
if (e.InputParameters[0] != null && e.InputParameters[0].GetType().Equals(typeof(tblRole_NEW)))
{
tblRole_NEW r = (tblRole_NEW)e.InputParameters[0];
r.ApplicationName = SelectedApplicationName;
}
base.CurrentFormObjectDataSource_Inserting(sender, e);
}


4. Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim rw As GridViewRow = GridView1.Rows(e.RowIndex)
Dim dt As Data.DataTable = CType(Session("SavData"), Data.DataTable)

Dim prodid As String = CType(rw.Cells(1).Controls(0), TextBox).Text
Dim prodname As String = CType(rw.Cells(2).Controls(0), TextBox).Text
Dim supid As String = CType(rw.Cells(3).Controls(0), TextBox).Text
Dim catid As String = CType(rw.Cells(4).Controls(0), TextBox).Text

Dim old_prodid As String = dt.Rows(e.RowIndex).Item("productid")
Dim old_prodname As String = dt.Rows(e.RowIndex).Item("ProductName")
Dim old_supid As String = dt.Rows(e.RowIndex).Item("SupplierID")
Dim old_catid As String = dt.Rows(e.RowIndex).Item("CategoryID")


5. protected override void CurrentFormObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (!DisableGridView && CurrentGrid.SelectedDataKey != null && CurrentGrid.SelectedDataKey["RoleName"] != null && CurrentGrid.SelectedDataKey["ApplicationName"] != null)
{
e.InputParameters["applicationName"] = (string)CurrentGrid.SelectedDataKey["ApplicationName"];
e.InputParameters["roleName"] = (string)CurrentGrid.SelectedDataKey["RoleName"];
}
else
e.Cancel = true;
base.CurrentFormObjectDataSource_Selecting(sender, e);
}


6. string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();

asp:gridview id="CustomersGridView" datakeynames="CustomerID" runat="server">
/asp:gridview>

7. SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

8. lbDelete.OnClientClick = "javascript:return confirm('" + AppStrings.ctlEventList_DeleteAlert + "');";

9. asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete" CommandArgument='<%# Container.DataItemIndex %>' Text="Delete">

CurrentGrid_RowCommand(object sender, GridViewCommandEventArgs e)
switch (e.CommandName.ToLower())
{
case "select":
CurrentGrid.SelectedIndex = Convert.ToInt32(e.CommandArgument);
...
}

10.
protected virtual void CurrentGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
...
GridViewRow row = GetGridViewRow(e.CommandSource as Control);
...
}

protected GridViewRow GetGridViewRow(Control cntr)
{
if (cntr != null)
{
if (cntr.GetType().Equals(typeof(GridViewRow)))
return cntr as GridViewRow;
else
return GetGridViewRow(cntr.Parent);
}

return null;
}

11.
CurrentGridObjectDataSource_Updating
CurrentGridObjectDataSource_Updated
CurrentForm_ItemUpdated

CurrentForm_ModeChanging
CurrentForm_ModeChanged

CurrentGridObjectDataSource_Selecting
CurrentForm_ItemCreated

没有评论:

发表评论