1

我有一个带有按钮的 ASP.net 页面,该按钮可在单击时创建模式窗口:

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;")

我没有使用 jQuery。这是启动一个全新的 .aspx 页面,而不是当前页面中的新层。

模态窗口中的数据是绑定到数据表的数据网格。它可以包含 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>

后面的代码:

Dim dvHeader as DataView
Dim dtHeader as DataTable

dvHeader = dtHeader.DefaultView
grdHeader.DataSource = dvHeader
grdHeader.DataBind()

我希望模态窗口的大小适合正在显示的数据,没有滚动条。我确信我可以通过 Javascript 做到这一点,但 Javascript 真的不是我的强项。

任何帮助将非常感激!

4

1 回答 1

1

在弹出窗口的主体中添加一个 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)

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.

于 2013-03-04T21:21:34.857 回答