Solved

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

Posted on 2010-11-12
5
935 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
  • 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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