我正在ASP.net C#中开发一个地图应用程序。
我有一个文本框和按钮,用于在邮政编码上搜索数据库并将结果返回到我的aspx页面上的网格视图...
public partial class _Default : System.Web.UI.Page
{
// SDE connection string to extract postcode from ADDRESS (sde) table.
private SqlConnection m_sqlConn;
protected void Page_Load(object sender, EventArgs e)
{
}
private void ShowMsg(string strMessage)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (txtPostCode.Text.Length > 0)
{
m_sqlConn = new SqlConnection();
m_sqlConn.ConnectionString = "Data Source=Server1;Initial Catalog=sde;User
ID=Tom;Password = Password1";
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = m_sqlConn;
sqlCmd.CommandType = System.Data.CommandType.Text;
sqlCmd.CommandText = "SELECT * FROM ADDRESS"
+ " WHERE Postcode = '" + txtPostCode.Text + "'";
m_sqlConn.Open();
SqlDataReader reader = sqlCmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
}
else
{
ShowMsg("Error - No Postal Addresses Returned");
}
}
catch (Exception ex)
{
ShowMsg("Error - " + ex.Message);
}
}
private bool CloseDB()
{
try
{
m_sqlConn.Close();
return (true);
}
catch (Exception ex)
{
return (false);
}
}
}
这很好用。我现在想要在Modal PopUp中设置数据网格,用户单击搜索按钮并以模态方式返回结果表。我试图用假的ControlID按钮设置它,但没有运气......
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button id="BtnFake" runat="server" Style="display: none"/>
<table id="ModalGrid">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button id="Button2" runat="server" Text="OK" />
</table>
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="BtnFake" PopupControlID="ModalGrid" DropShadow="false"
BackgroundCssClass="ModalBackground"
CancelControlID="BtnOK" BehaviorID="ModalGrid" RepositionMode="RepositionOnWindowScroll">
</cc1:ModalPopupExtender>
有什么想法吗?确定我做了一些明显的错误。欢呼声。
答案 0 :(得分:1)
TargetControlId
设置为虚拟字段的情况下,您必须显式调用ModalPopupExtender1.Show();
以显示模式。提示 - 使用虚拟字段的按钮有点过分,使用像<span>
这样简单的东西
如果您遇到困难,下面是一个有效的示例,但我相信致电ModalPopupExtender1.Show();
会让您感到困惑:
<强> ASPX:强>
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="sm" runat="server">
</asp:ToolkitScriptManager>
<span ID="dummy" runat="server" />
Post code: <asp:TextBox ID="txtPostcode" runat="server" /><br />
<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="Search" />
<asp:Panel runat="server" ID="modalPanel" CssClass="modalPanel">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</asp:Panel>
<asp:ModalPopupExtender id="ModalPopupExtender1" runat="server" TargetControlID="dummy"
PopupControlID="modalPanel" BackgroundCssClass="ModalBackground"
CancelControlID="btnCancel" BehaviorID="ModalGrid">
</asp:ModalPopupExtender>
</form>
代码背后:
protected void Search(object sender, EventArgs e)
{
List<PostalCode> codes = new List<PostalCode>()
{
new PostalCode{ Code="000",Province="District 0" },
new PostalCode{ Code="111",Province="District 1" }
};
string code = txtPostcode.Text;
if (codes.Where(c => c.Code == code).Any())
{
GridView1.DataSource = codes.Where(c => c.Code == code);
GridView1.DataBind();
ModalPopupExtender1.Show();
}
}
我用过的课程测试:
public class PostalCode
{
public string Code { get; set; }
public string Province { get; set; }
}