vuyiswamb 17 Posting Whiz

I see a lot of people have been asking this question, some was because of the if postback check , but mine is different. i am using AjaxModalExtender to show my popup on the server side. this is my markup

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="PopTester.TestPage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">

 .modalBackground
{     background-color: Yellow;
      filter: alpha(opacity=60);
      opacity: 0.6;
}

   </style>

 
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager>

    <asp:Panel ID="Panel1" runat="server">

    <asp:Button ID="Button1" runat="server"

    Text="CreateModal" OnClick="Button1_Click" />

    </asp:Panel>


         <asp:Panel ID="ModalPanel" runat="server" Style="display: none" HorizontalAlign="Center" BackColor="Green">
             <div>
     <asp:UpdatePanel ID="Update" runat="server">
     <ContentTemplate>
        <asp:GridView ID="GridView1"  AutoGenerateColumns="false" runat="server" Height="176px" Width="453px">
        <Columns>
        <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
        <asp:CheckBox ID="chkId" runat="server" />
        <asp:Label ID="lblId" runat="server" Text='<%#Eval("ID")%>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Name">
        <ItemTemplate>
        <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

        
        <asp:TemplateField HeaderText="Lastname">
        <ItemTemplate>
        <asp:Label ID="lblLastname" runat="server" Text='<%#Eval("Lastname") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>

        </asp:GridView>

             <asp:Button ID="btnPickrecord" runat="server" Text="Pick Record" 
             onclick="btnPickrecord_Click "   />
    <asp:Button ID="btnload" runat="server" onclick="btnload_Click"  
        Text="Load Data" />
                </ContentTemplate>
     </asp:UpdatePanel>
     
        </div>
        <asp:Button ID="btnCancel" runat="server" Text="Close Me" 
                 onclick="btnCancel_Click" />
    </asp:Panel>
        <asp:TextBox ID="txtreference" runat="server" AutoPostBack="True"></asp:TextBox>
        <p>
            &nbsp;</p>
    </form>
</body>
</html>

as you can see i have a gridview that will be on the modal and its working fine, now after the grid has returned some results, i will select one record via the checkbox on the Grid and click the button "btnPickrecord" and this will fire my server side code

protected void btnPickrecord_Click(object sender, EventArgs e)
        { 
                 foreach (GridViewRow dataItem in GridView1.Rows)
                {

                    CheckBox chkselect = (CheckBox)dataItem.FindControl("chkId");
                     
                    if (dataItem.RowType == DataControlRowType.DataRow)
                    {
                          if (chkselect.Checked)
                          {
                              Label lblReferenceNumberID = (Label)dataItem.FindControl("lblId");

                              txtreference.Text = lblReferenceNumberID.Text;
                          }
                    }
                 } 
        }

the breakpoints got a hit and i stepped through it and i the value get assigned to the textbox <b> txtreference.Text</b> , but when i close the modal the textbox is empty , or while the modal is open i can see the value does not reflect in the parent page. Here is the Code for invoking the modal

protected void Button1_Click(object sender, EventArgs e)
        { 
            AjaxControlToolkit.ModalPopupExtender modalPop = new AjaxControlToolkit.ModalPopupExtender();

            modalPop.ID = "popUp";

            modalPop.PopupControlID = "ModalPanel";

            modalPop.TargetControlID = "Button1";

            modalPop.DropShadow = true;

            modalPop.BackgroundCssClass = "modalBackground";

            modalPop.CancelControlID = "btnCancel";

            this.Panel1.Controls.Add(modalPop); 

            modalPop.Show();
        }

Thanks

Dani AI

Generated

A quick diagnosis for : the server handler is hitting and the textbox value is being assigned on the server, but the browser never shows the update because the button inside the modal is triggering an asynchronous (partial) postback. Only the UpdatePanel that participates in the async postback gets re-rendered; controls outside it (the parent textbox in this case) are not refreshed on the client. That makes the server-side assignment visible on the next full postback but not immediately in the page DOM.

Two simple ways to fix it:

  • Put the parent textbox into an UpdatePanel (or wrap the modal and parent textbox in the same UpdatePanel) so the partial postback updates the textbox too.
<asp:UpdatePanel ID="OuterUpdate" runat="server">
  <ContentTemplate>
    <asp:TextBox ID="txtreference" runat="server" />
  </ContentTemplate>
</asp:UpdatePanel>

If using a separate UpdatePanel, call its Update() from the btnPickrecord_Click handler after setting the Text.

  • Or push the value to the client with a small script registered from server code (use encoding to avoid JS injection):
string js = "document.getElementById('" + txtreference.ClientID + "').value = '"
  + HttpUtility.JavaScriptStringEncode(value) + "';";
ScriptManager.RegisterStartupScript(this, GetType(), "setRef", js, true);

Other important notes: create the ModalPopupExtender declaratively where possible (or, if creating it dynamically, recreate it in Page_Init on every postback so its client script and IDs remain consistent). Avoid rebinding the GridView on every postback without an IsPostBack guard, because rebinding will wipe checkbox selections. For reference on the UpdatePanel behavior and script registration see UpdatePanel (System.Web.UI) and ScriptManager.RegisterStartupScript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.