Solved

ASP.NET - Determining the detailsview title column width at runtime

Posted on 2010-11-12
5
939 Views
Last Modified: 2012-05-10
Hello Experts,

I have a DetailsView object which pulls its data from a sqldatasource.  At runtime, based on the data retrieved, the page will hide some rows and show others of the DetailsView object.

Because of the hiding/showing of rows is based on data, the column width of the title column changes at runtime.  I need to be able to determine programatically what the column size is in pixels.

This doesn't work, but I'm thinking something like this:
Dim intWidth as integer = dvResults.Rows(0).Cells(0).Width.Value
OR
Dim intWidth As Integer = dvResults.HeaderStyle.Width.Value

Thanks,
Maurice
0
Comment
Question by:mauricerichard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 34128873
IMO, I'd use a GridView for the details because after databinding, you can do something like...

	Protected Sub gvRowDataBind(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
		If e.Row.RowType = DataControlRowType.DataRow Then
			If e.Row.DataItem("someDataColumn").ToString = "someVal" Then
				gv.HeaderRow.Cells(0).Width = Unit.Pixel(150)
			End If
		End If
	End Sub

Open in new window


Better to set the headrow cell width rather than multiple widths for each row, which can result in misbehaving HTML.  The DetailsView doesn't appear to have sufficient functionality for post-formatting (AFAIK),
0
 
LVL 16

Expert Comment

by:BurnieP
ID: 34131089
You can use DetailsView with OnDataBound event also.

Add the OnDataBound event to your DetailsView declaration.  

<asp:DetailsView ID="dv" runat="server" OnDataBound="dv_OnDataBound">

The Code Behind function will be called at runtime when you DataBind your DetailsView :
Protected Sub dv_OnDataBound(sender As Object, e As EventArgs)
      Dim dView As DetailsView = DirectCast(sender, DetailsView)
      dView.HeaderRow.Cells(0).Width = Unit.Pixel(100)
End Sub

0
 
LVL 1

Author Comment

by:mauricerichard
ID: 34136489
Hello Rouchie & BurnieP,

Thanks very much for the suggestions.  Rouchie, sadly replacing the detailsview with a gridview isn't really an option for me.  But I appreciate the idea, you might be right that the detailsview doesn't have the functionality ... cause I'm having no luck what-so-ever.  BurnieP, that's seems like it has potential.  Although what I'm trying to do is retrieve the runtime column width as opposed to assigning it.

For example, imagine a detailsview column at runtime:
If the longest text field in that column is: "Dog" - the column width would be very small
If the longest text field is " The quick brown fox jumped over the lazy dogs - It'll be much larger

So I need to be able to do something like:
Dim intColumnWidthAtRuntime as Integer = dView.HeaderRow.Cells(0).Width

Will that work?  Or how could we tweak it to work?

Thanks!
Moe
0
 
LVL 25

Accepted Solution

by:
Rouchie earned 500 total points
ID: 34136602
You can't do that from code-behind I'm afraid, because the width is set by the browser based on the cell contents, which then affects the whole column.  Therefore, you'd have to use JQuery or JavaScript to determine the width once the page finished loading and all the content was in there.
0
 
LVL 1

Author Closing Comment

by:mauricerichard
ID: 34148209
Looks like I won't be doing this from code-behind then.  Thanks for the help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question