DataGrid事件的用法(二)【鸡蛋】

来源:互联网 发布:房产中介软件fang 编辑:程序博客网 时间:2024/05/01 20:38



DataGrid的ItemCreated和ItemDataBound事件:

1.DataGrid.ItemCreated当创建DataGrid 控件中的项时(不论是在往返行程中还是在将数据绑定到控件时),都会引发 ItemCreated 事件。ItemCreated 事件通常用于控制 DataGrid 控件中行的内容和外观。

2.DataGrid.ItemDataBound当项被数据绑定到 DataGrid 控件后,将引发 ItemDataBound 事件。此事件为您提供了在客户端显示数据项之前访问该数据项的最后机会。当引发此事件后,该数据项将被设为空,并且不再可用。(ItemDataBound嘛,只要执行了DataBind方法,就会马上激发这个事件。)


(如果页面是第一次访问(Page.IsPostBack = false),那在第一次执行DataBind的时候,会先激发ItemCreated事件,首先会用ItemCreated来建立Header行,然后用ItemDataBound来绑定Header行,再用ItemCreated来建立第一行,再调用ItemDataBound来绑定第一行;也就是说ItemCreated和ItemDataBound两个事件是不断交替执行的:先执行ItemCreated再执行ItemDataBound;页面返回时,也会执行ItemCreated事件,在Page_Load之前,但是这时候就不会再执行ItemDataBound事件了,所以,如果你想在DataGrid里动态添加什么控件,就需要在ItemCreated事件中,而不是在ItemDataBound事件中

模拟两个事件的代码如下:

html:

<asp:TemplateColumn HeaderText="Priority">    <ItemTemplate><asp:DropDownList id="ddlPriority" runat="server" Width="100px"></asp:DropDownList><asp:Label id="lblVip_no" runat="server" style="display:none;"></asp:Label>    </ItemTemplate></asp:TemplateColumn>

'获取城市数据集方法 Dim dsPriority As Dataset=getPriority()
'修改城市信息的方法方法Private Sub UpdatePriority(ByVal Sender As Object, ByVal e As System.EventArgs)Dim ddlPri As DropDownList = CType(Sender, DropDownList) '获取dropdownlist的实例Dim ID, city As StringIf Not ddlPri Is Nothing Then    Dim tr As TableRow = CType(ddlPri.Parent.Parent, TableRow) '获取当前行    ID = CType(tr.FindControl("lbID"), Label).Text '找到当前列的id    'vip_no = tr.Cells(8).Text.Trim    If ddlPri.SelectedIndex <= 0 Thencity= ""    Elsecity= ddlPri.SelectedValue '获取city    End If    Dim objUser As New Users    objUser.UpdateUserinfo(ID, city) '执行数据库插入    bind_data() '再次执行数据绑定    objUser.Dispose()    objUser = NothingEnd IfEnd Sub
'Datagrid的 ItemCreated 事件,在Datagrid被创建每一列时激发Private Sub dgList_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgUser.ItemCreated'下面可用 If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then  end if来判断Select Case e.Item.ItemType    Case ListItemType.Item, ListItemType.AlternatingItem'Dim pri As String = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "priority")).ToUpperIf Not dsPriority Is Nothing AndAlso dsPriority.Tables.Count > 0 Then    Dim ddlPri_tmp As DropDownList = CType(e.Item.FindControl("ddlPriority"), DropDownList) '找到Datagrid里的dropdownlist控件,并创建它的实例    'ddlPri_tmp.Attributes.Add("onchange", "javascript:return confirm('Are you sure to update?');") '为新创建的实例追加脚本事件    '为新建的dropdownlist实例绑定数据源    With ddlPri_tmp.DataSource = dsPriority.Tables(0).DefaultView.DataTextField = "emp_desc".DataValueField = "emp_id".DataBind()'根据Datagrid的priority字段值 来设定dropdownlist实例的默认选中值For k As Integer = 0 To ddlPri_tmp.Items.Count - 1    If ddlPri_tmp.Items(k).Value.ToUpper.CompareTo(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "priority")).ToUpper) = 0 Then  '-- use priority_camd instead of priority temperorily ddlPri_tmp.SelectedIndex = kExit For    End IfNext    End With    'ddlPri_tmp.Items.Insert(0, New ListItem("-", "-"))    '通过使用AddressOf 将下拉列表的SelectedIndexChanged事件指向UpdatePriority函数地址【有可能造成不能调试哦】    AddHandler ddlPri_tmp.SelectedIndexChanged, AddressOf UpdatePriority   End IfEnd SelectEnd Sub
'Datagrid的 ItemDataBound 事件,在Datagrid被创建每一列时激发Private Sub dgList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgUser.ItemDataBoundSelect Case e.Item.ItemType    Case ListItemType.AlternatingItem, ListItemType.ItemCType(e.Item.FindControl("lblVip_no"), Label).Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "vip_no"))'找到Datagrid里的dropdownlist控件,并创建它的实例Dim ddlPri_tmp As DropDownList = CType(e.Item.FindControl("ddlPriority"), DropDownList)'根据Datagrid的priority字段值 来设定dropdownlist实例的默认选中值For k As Integer = 0 To ddlPri_tmp.Items.Count - 1    If ddlPri_tmp.Items(k).Value.ToUpper.CompareTo(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "priority")).ToUpper) = 0 Then  '-- use priority_camd instead of priority temperorily ddlPri_tmp.SelectedIndex = kExit For    End IfNextddlPri_tmp.Attributes.Add("onchange", "javascript:UpdateConfirm(this,'" & ddlPri_tmp.SelectedValue.Trim & "');")End SelectEnd Sub





原创粉丝点击