3

我正在用 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);
    }

}    

}

这工作正常。我现在想在模态弹出窗口中设置数据网格,用户单击搜索按钮并以模态方式返回结果表。我试过用一个假的 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>

有任何想法吗?确定我在做一些明显错误的事情。干杯。

4

1 回答 1

1
  1. 在将模式弹出窗口设置TargetControlId为虚拟字段的情况下,您必须显式调用ModalPopupExtender1.Show();以显示模式。
  2. BtnOK 在哪里?不应该是Button2吗?

提示 - 为虚拟字段使用按钮有点矫枉过正,请使用简单的东西,例如<span>a

如果您遇到困难,下面是一个工作示例,但我相信致电ModalPopupExtender1.Show();应该可以解决您的问题:

ASPX:

<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="sm" runat="server">
</asp:ToolkitScriptManager>
<span ID="dummy" runat="server" />
Post code:&nbsp;<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; }
} 
于 2013-01-25T21:33:15.553 回答