Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now