duraz0rz
duraz0rz

Reputation: 397

How can I get my ASP table control to show/hide? (not as simple as it sounds)

Alright, I'm fairly new to ASP.NET, but I think I understand what's going on,. I've been tasked with building reports in them. So far, I've got everything down except for this one issue.

I use a Repeater that's databound to a DataRowCollection and programatically build a table through that. I have something similar to:

Private Sub SubAcctGrid_OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
        Dim currentrow = TryCast(e.Item.DataItem, DataRow)
        Dim acctSummaryTable = New Table With {.ID = "acctSummary" & e.Item.ItemIndex, .CssClass = "minisub_acct_table"}
        Dim drillDownTable = New Table With {.ID = "drillDownTable" & e.Item.ItemIndex, .CssClass = "drilldown_table"}

        ' Build both tables here

        acctSummaryTable.Rows(1).Cells(0).Attributes.Add("onclick", String.Format("displayDetailTable('{0}')", drillDownTable.ID))
        acctSummaryTable.Rows(1).Cells(1).Attributes.Add("onclick", String.Format("displayDetailTable('{0}')", drillDownTable.ID))

        e.Item.Controls.Add(acctSummaryTable)
        e.Item.Controls.Add(drillDownTable)

    End If
End Sub

I have a Javascript function that takes in the element ID and hides or shows the table:

function displayDetailTable(tableID) {
    var table = document.getElementById(tableID)
    if (table.style.display == "none") {
        table.style.display = "table";
    }
    else {
        table.style.display = "none";
    }
}

I can't, for the life of me, pass in the correct element ID, though. From what I understand, ASP.NET transforms the ID I assign into some huge long ID for the sake of uniqueness. For example, I assign a table with the ID 'drillDownTable0' and it spits out 'ctl00_drillDownTable0' in the HTML markup. It worked the first time around, but then the Repeater gets binded to a new row and then I get 'ctl01_drillDownTable0' so on and so forth.

I've tried the ClientID, ID, and UniqueID where I add the attributes to the Cells in the code above and they don't do what I need to.

Is there any way I can get that ID and pass it to the Javascript function? Or I guess a better question is: When are the IDs generated and can I get to them before the page is rendered in HTML?

Upvotes: 0

Views: 528

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460380

You're probably looking for ClientID:

String.Format("displayDetailTable('{0}')", drillDownTable.ClientID) 

Apart from that, i would recommend to create controls dynamically in Repeater's ItemCreated event. You must recreate all dynamical controls on every postback. ItemDataBound is only called on databinding whereas ItemCreated is called on every postback.

Edit: I've only just seen that you've already tried ClientID. But it might be an issue caused by creating the table in ItemdataBound.

Upvotes: 1

Related Questions