views:

275

answers:

2

Hi,

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. These links take you a page with a gridview populated dynamically, as each department has a different number of teams, e.g. Finance has one team, IT has four. Each cell has a webuser control inserted into it. 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. (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.

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? (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)

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
A: 

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. 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? Also... why the heck are you using FindControl if you already have a handle to your control? Cast to the appropriate type when you declare/load the control.

Bryan
A: 

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.

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.

Stuart