I have an ASP.net page with a button that creates a modal window onclick:
我有一个ASP.net页面,其中包含一个按钮,可以在onclick上创建一个模态窗口:
Dim sURL As String = System.Configuration.ConfigurationManager.AppSettings("APP_Path") & "Detail.aspx"
btnDone.Attributes.Add("onclick", "javascript:window.showModalDialog('" & sURL & "',null,'status:no;dialogWidth:auto;dialogHeight:auto;dialogHide:true;help:no;scroll:yes;center:yes');return false;")
I am not using jQuery. This is launching an entirely new .aspx page, not a new layer in the current page.
我不是在使用jQuery。这是一个全新的.aspx页面,而不是当前页面中的新图层。
The data in the modal window is a datagrid bound to a datatable. It can contain 5 lines to 50 lines - there is no way to know until runtime, when the datatable is created and bound.
模态窗口中的数据是绑定到数据表的数据网格。它可以包含5行到50行 - 在运行时,创建和绑定数据表时无法知道。
<asp:DataGrid ID="grdHeader" runat="server" Width="100%" CssClass="grdGrid"
Font-Size="12px" Allowpaging="false" GridLines="None" AutoGenerateColumns="true"
Font-Names="Verdana" CellPadding="0" ShowHeader="false"></asp:DataGrid>
Code behind:
代码背后:
Dim dvHeader as DataView
Dim dtHeader as DataTable
dvHeader = dtHeader.DefaultView
grdHeader.DataSource = dvHeader
grdHeader.DataBind()
I would like for the modal window to size to the data being displayed, without scroll bars. I'm sure I can do this via Javascript, but Javascript is really not my strong suit.
我希望模态窗口可以调整显示的数据大小,而不需要滚动条。我确信我可以通过Javascript来做到这一点,但Javascript真的不是我的强项。
Any help would be much appreciated!
任何帮助将非常感激!
1 个解决方案
#1
1
Add an onload event to the body of the popup window and have it call the following function:
将onload事件添加到弹出窗口的主体并让它调用以下函数:
function resizeWindow()
{
var containerElement = document.getElementById('<%=grdHeader.ClientID%>');
window.resizeTo(containerElement.offsetWidth, containerElement.offsetHeight);
}
Unfortunately offsetHeight doesn't take into account the height of elements in the browser (such as bookmark bars, url bars, etc.) so you do need to add some concrete amount to make sure all of the data is displayed. I think this might be the only way you can do what you hope to do, though, short of estimating the height of the window based on the number of rows in your data source (i.e. 25 rows x 25 pixels per row = 625 pixels)
不幸的是,offsetHeight没有考虑浏览器中元素的高度(例如书签栏,网址栏等),因此您需要添加一些具体数量以确保显示所有数据。我认为这可能是你可以做你想做的唯一方法,但是,根据数据源中的行数估计窗口的高度(即每行25行×25像素= 625像素)
Edit: Just a note, you are using showModalDialog to essentially open a new window. The above function will not work unless you use window.open(...) to open the window, which would be the best option anyway since you aren't actually making a true modal dialog.
编辑:只是一个注释,你使用showModalDialog基本上打开一个新窗口。除非你使用window.open(...)来打开窗口,否则上面的函数将无效,因为你实际上并没有真正建立模态对话框,这将是最好的选择。
#1
1
Add an onload event to the body of the popup window and have it call the following function:
将onload事件添加到弹出窗口的主体并让它调用以下函数:
function resizeWindow()
{
var containerElement = document.getElementById('<%=grdHeader.ClientID%>');
window.resizeTo(containerElement.offsetWidth, containerElement.offsetHeight);
}
Unfortunately offsetHeight doesn't take into account the height of elements in the browser (such as bookmark bars, url bars, etc.) so you do need to add some concrete amount to make sure all of the data is displayed. I think this might be the only way you can do what you hope to do, though, short of estimating the height of the window based on the number of rows in your data source (i.e. 25 rows x 25 pixels per row = 625 pixels)
不幸的是,offsetHeight没有考虑浏览器中元素的高度(例如书签栏,网址栏等),因此您需要添加一些具体数量以确保显示所有数据。我认为这可能是你可以做你想做的唯一方法,但是,根据数据源中的行数估计窗口的高度(即每行25行×25像素= 625像素)
Edit: Just a note, you are using showModalDialog to essentially open a new window. The above function will not work unless you use window.open(...) to open the window, which would be the best option anyway since you aren't actually making a true modal dialog.
编辑:只是一个注释,你使用showModalDialog基本上打开一个新窗口。除非你使用window.open(...)来打开窗口,否则上面的函数将无效,因为你实际上并没有真正建立模态对话框,这将是最好的选择。