Grid view in Modal PopUp Extender?

I am developing a mapping application in ASP.net C #.

I have a textbox and a button that looks for a database in the zip and returns the results as a grid in my aspx page ...

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

}    

      

}

This works great. Now I want to set the Data Grid to Modal PopUp whereby the user clicks the search button and the result table is returned modally. I tried setting it up with a fake ControlID button like this, but no luck ...

<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>

      

Any ideas? of course I am doing something obvious wrong. Greetings.

+3


source to share


1 answer


  • In a script where you set modal popups TargetControlId

    for a dummy field, you must explicitly call ModalPopupExtender1.Show();

    to display the modal file.
  • Where is BtnOK? Should it be Button2?

Council. Using the buttons to the fictitious field is a bit overkill, use something simple like <span>

instead

Below is a working example in case you get stuck, but I believe the call ModalPopupExtender1.Show();

should sort you:

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>

      



Code behind:

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();
    }
}

      

The class I used for testing:

public class PostalCode
{
    public string Code { get; set; }
    public string Province { get; set; }
} 

      

+1


source







All Articles