在分层用户控件中处理事件 [英] Handling Events in Hierarchical User Control

查看:80
本文介绍了在分层用户控件中处理事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要有关正在创建的向下钻取"用户控件的帮助.我想我快到了.

我要实现的是工作人员的分层网格;

经理1
Manager2
经理3

单击Manager1显示:

经理1
-LineManager1
-LineManager2
Manager2
经理3

单击LineManager1显示:

经理1
-LineManager1
-员工1
-员工2
-员工3
-LineManager2
Manager2
经理3

我简化了这里返回的信息,但是每一行将包含一些有价值的数据.

我不想使用树形视图,因为数据列很少,而且我不想加载所有人员(会有成千上万个)

我创建了一个包含Gridview的UserControl. Gridview的每一行都包含一个LinkBut​​ton和一个PlaceHolder控件.

我有一个SQL存储过程,该存储过程返回任何给定级别的工作人员列表的数据-这是使用UserControl的"ParentUser"属性运行的.

单击LinkBut​​ton时,我将加载UserControl的新实例并将其添加到gridviewrows占位符:

Hi,

I need some help with a ''drill-down'' user control that I''m creating. I think I''m nearly there.

What I am trying to acheive is a hierarchical grid of staff;

Manager1
Manager2
Manager3

Clicking on Manager1 shows:

Manager1
-LineManager1
-LineManager2
Manager2
Manager3

Clicking on LineManager1 shows:

Manager1
-LineManager1
--Staff1
--Staff2
--Staff3
-LineManager2
Manager2
Manager3

I have simplified the information returned here, but each line will contain a few colums worth of data.

I dont want to use a treeview because of the few columns of data issue and because I don''t want to load all of the staff (there will be thousands)

I have created a UserControl that contains a Gridview. Each Row of the Gridview contains a LinkButton and a PlaceHolder Control.

I have an SQL stored Procedure that returns the data for the list of staff at any given level - this is run using a ''ParentUser'' Property of the UserControl.

When the LinkButton is clicked I load a new instance of the UserControl and add it to the gridviewrows placeholder:

Dim myDL As New UserControls_DLGridItem
Dim myRow As GridViewRow = CType(sender, LinkButton).Parent.Parent

myDL = Page.LoadControl("UserControls/DLGriditem.ascx")
myDL.ParentUserCode = CType(sender, LinkButton).CommandArgument
myDL.StartDate = Me.StartDate
CType(myRow.FindControl("Placeholder1"), PlaceHolder).Controls.Add(myDL)



我已经创建了一个测试页并添加了usercontrol的实例.页面加载时,将设置控件的ParentUser属性.

到目前为止,一切都很好-该页面加载并显示了最高级别的经理.
单击LinkBut​​ton,然后加载下一个级别(LineManagers)OK.

我遇到的问题是,单击其中一个LineManager上的LinkBut​​ton不会触发LinkBut​​ton_Click事件.

最初,在动态创建的控件中单击LinkBut​​ton之后,动态生成的内容已完全消失.

我将此添加到LinkBut​​ton_Onclick事件中(添加到占位符之后):



I have created a test page and added an instance of the usercontrol. When the page loads it sets the ParentUser Property of the control.

So far, so good - the page loads and displays the top level of managers.
Clicking on LinkButton then Loads the next Level (LineManagers) OK.

The problem I have is that Clicking the LinkButton on one of the LineManagers does not fire the LinkButton_Click event.

Initially the dynamically generated content has disappearing completely after clicking on a LinkButton in the dynamically created control.

I added this to the LinkButton_Onclick Event (after I add to the placeholder):

Session.Add(CType(myRow.FindControl("Placeholder1"), PlaceHolder).UniqueID, myDL)



而这个:



And this:

Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender

        For Each rw As GridViewRow In Me.GridView1.Rows
            If rw.RowType = DataControlRowType.DataRow Then
                ''If there is a usercontrol in session then load it!
                Dim myPlaceholder As PlaceHolder = rw.FindControl("PlaceHolder1")

                If Not Session(myPlaceholder.UniqueID) Is Nothing Then
                    myPlaceholder.Controls.Clear()
                    myPlaceholder.Controls.Add(CType(Session(myPlaceholder.UniqueID), UserControls_DLGridItem))
                End If
            End If
        Next

    End Sub




这样可以停止动态生成的控件消失,但仍然不会处理控件上的LinkBut​​ton_Click事件.

换句话说,我可以装载The LineMangers,但不能装载他们的人员!

有人知道我在做什么错吗?

有更简单的方法吗?

谢谢!

Chris




This stops the dynamically generated controls disappearing but still wont process the LinkButton_Click event on the controls.

In other words I can get The LineMangers Loaded, but not their staff!

Anyone have any ideas what I am doing wrong?

Is there an easier way of doing this?

Thanks!

Chris

推荐答案

感谢数字人,

我不想使用树状视图,因为我要返回一个需要格式化列的类似网格的结构.

我想我现在可以正常使用了.

我将gridview1.PreRender中的代码移至页面Load事件,并将其稍作修改为:

Thanks digital man,

I didn''t want to use a treeview because I''m returning a grid-like structure that will need formatted columns.

I think I now have this working properly.

I moved the code in the gridview1.PreRender to the page Load event and amended it slightly to:

For Each rw As GridViewRow In Me.GridView1.Rows
            If rw.RowType = DataControlRowType.DataRow Then
                'If there is a usercontrol in session then load it!
                Dim myPlaceholder As PlaceHolder = rw.FindControl("PlaceHolder1")
                If Not Session(myPlaceholder.UniqueID) Is Nothing Then
                    myPlaceholder.Controls.Clear()
                    Dim myDL As New UserControls_DLGridItem
                    myDL = Page.LoadControl("UserControls/DLGriditem.ascx")
                    myDL.WhichResults = 3
                    myDL.ParentUserCode = CType(Session(myPlaceholder.UniqueID), UserControls_DLGridItem).ParentUserCode
                    myDL.StartDate = CType(Session(myPlaceholder.UniqueID), UserControls_DLGridItem).StartDate
                    myPlaceholder.Controls.Add(myDL)
                 End If
            End If
        Next



即,我正在重新创建子控件(使用Page.LoadControl),而不是简单地从会话中获取它.

谢谢

克里斯



i.e. I''m re-creating the child control (using Page.LoadControl) rather than simply getting it from the session.

Thanks

Chris


这篇关于在分层用户控件中处理事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆