简体   繁体   English

在父级gridview内的webuser控件中嵌套gridview / formview

[英]Nesting gridview/formview in webuser control inside a parent gridview

I'm developing an ASP.net 2 website for our HR department, where the front page has a matrix of all our departments against pay grades, with links in each cell to all the jobs for that department for that grade. 我正在为人力资源部门开发一个ASP.net 2网站,该网站的首页上有一个矩阵,其中包含我们所有部门的薪资等级矩阵,并在每个单元格中链接到该等级该部门的所有职位。 These links take you a page with a gridview populated dynamically, as each department has a different number of teams, eg Finance has one team, IT has four. 这些链接带您进入一个页面,该页面具有动态填充的gridview,因为每个部门都有不同数量的团队,例如,财务有一个团队,IT有四个团队。 Each cell has a webuser control inserted into it. 每个单元都有一个webuser控件插入其中。 The user control has a sql datasource, pulling out all the job titles and the primary key, popuating a formview, with a linkbutton whose text value is bound to the job title. 用户控件具有一个sql数据源,可拉出所有作业标题和主键,并使用链接按钮将其文本值绑定到该作业标题,该主键将弹出一个formview。 (I'm using a usercontrol as this page will also be used to show the results of a search of all roles in a range of grades for a department, and will have a varying number of rows). (我使用的是用户控件,因为此页面还将用于显示部门各个级别的所有角色的搜索结果,并且行数会有所变化)。

I've got everything to display nicely, but when I click on the linkbutton, instead of running the code I've put in the Click event, the page posts back without firing any events. 我可以很好地显示所有内容,但是当我单击linkbutton时,该页面回发而不触发任何事件,而不是运行我放置在Click事件中的代码。

Having looked around, it looks like I have to put an addhandler line in somewhere, but I'm not sure where, could anyone give me some pointers please? 环顾四周后,好像我必须在某处放置一条addhandler行,但是我不确定在哪里,有人可以给我一些指针吗? (fairly numpty please, I'm not too experience in ASP yet and am winging it. I'm also using VB but C# isn't a problem) (请放心,我还不太熟悉ASP并正在使用它。我也在使用VB,但C#没问题)

This is how I'm inserting the controls into the parent grid, have I missed anything obvious? 这就是我将控件插入父网格的方式,我错过了任何明显的事情吗?

        For row As Int16 = 0 To dgvRoleGrid.Rows.Count - 1
            tempwuc = New UserControl
            tempwuc = LoadControl("wucRoleList.ascx")
            tempwuc.ID = "wucRoleList" & col.ToString
            tempwuc.EnableViewState = True
            dgvRoleGrid.Rows(row).Cells(col).Controls.Add(tempwuc)
            CType(dgvRoleGrid.Rows(row).FindControl(tempwuc.ID), wucRoleList).specialtyid = specid
            CType(dgvRoleGrid.Rows(row).FindControl(tempwuc.ID), wucRoleList).bandid = dgvRoleGrid.DataKeys(row)(0)
            CType(dgvRoleGrid.Rows(row).FindControl(tempwuc.ID), wucRoleList).familyid = Session("familyid")


        Next

Hard to say without having a look at more of your code, but the most common reason for not receiving an event from a control in a repeater is that you are re-binding the data instead of relying on ViewState. 在不看更多代码的情况下很难说,但是不从转发器中的控件接收事件的最常见原因是您要重新绑定数据而不是依赖于ViewState。 Does your code look something like this: 您的代码是否如下所示:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)  // <--- YOU REALLY NEED THIS!
        {
            DoDataBinding();
        }

    }
}

Are you missing the IsPostBack check? 您是否缺少IsPostBack检查? Also... why the heck are you using FindControl if you already have a handle to your control? 另外...如果您已经拥有控件的句柄,为什么还要使用FindControl? Cast to the appropriate type when you declare/load the control. 声明/加载控件时,强制转换为适当的类型。

The reason I'm doing it this way is I'm fairly new to all this, and winging it with whatever seems to work; 我这样做的原因是,我对这一切还比较陌生,并且将其与任何可行的方法一起使用; with the deadlines I'm facing, I don't really have the time to find the most elegant solution, as long as it's stable and works, it'll do. 在我面临的截止日期之前,我真的没有时间找到最优雅的解决方案,只要它稳定且有效,它就会做到。

There is an ispostback check which doesn't rebind the datasource on postback. 有一个ispostback检查,它不会在回发时重新绑定数据源。

I've got round the issue by using treeviews, using the databound event to amend the postback URL to include the value as parameter, but I'd still like to work out how to get this working properly. 我已经通过使用树视图解决了这个问题,使用数据绑定事件来修改回发URL以将值作为参数包括在内,但是我仍然想弄清楚如何使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM