wunth
wunth

Reputation: 634

Updating control values via aspx page's button in time to be rendered

I'm updating an existing asp.net application and on a page with a login button if there are any errors the error is currently just inserted into the body of the page via Response.Write.

I'm trying to create a control which allows the message header and body to be updated and then shown in an overlay.

Separate elements are working fine but I'm just having trouble updating the message header and body in the code behind the page as triggered by a button. I think the properties are being updated too late for them to display in the page. It seems I can update the variables in time if done during page load but not when initiated by a button

ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Prompt.ascx.cs" Inherits="Intranet.Controls.Prompt" %>

<h3><asp:literal runat="server" id="pTitle" Text="Alert" /></h3>
<div class="promptContent">
    <asp:Literal ID="promptContent" runat="server" text="" />
</div>
<asp:Button ID="closePrompt" runat="server" Text="OK" OnClick="closePrompt_Click" />

ascx.cs

public partial class Prompt : System.Web.UI.UserControl
{
    private String _promptMessage = string.Empty;

    public string promptMessage
    {
        get { return _promptMessage; }
        set { _promptMessage = value; }
    } 


    protected void Page_Load(object sender, EventArgs e)
    {
            promptContent.Text = _promptMessage;
        }
    }

    protected void closePrompt_Click(object sender, EventArgs e)
    {
        Page.Master.FindControl("showGenericPrompt").Visible = false;
    }
}

aspx page

<Intranet:Prompt ID="SQLPlaygroundPrompt" runat="server" />

aspx.cs

    protected new void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        //updatePrompt("if I change it here we're golden (it works)");
    }

    private void updatePrompt(String messageToDisplay)
    {
        SQLPlaygroundPrompt.promptMessage = messageToDisplay;
    }

    protected void btn_Login_OnClick(object sender, EventArgs e)
    {
        updatePrompt("a test");
        Page.Master.FindControl("showGenericPrompt").Visible = true;
    }

Please let me know how to update the promptMessage value in the btn_Login_OnClick event

Upvotes: 0

Views: 37

Answers (1)

haitham sha
haitham sha

Reputation: 90

i suggest put prompt message in master page instead usercontrol , in the master page add div with

<div id="ResultDiv" runat="server" visible="false"></div>

in masterpage.cs make a property that return div value

public HtmlGenericControl ResultDivProp
    {
        get {return ResultDiv; }
    }

and in login page get the div value with this code

response.write(this.Master.ResultDivProp)

you must put this code in login page.aspx

<%@ MasterType VirtualPath="~/yourMasterPage.Master" %>

Upvotes: 1

Related Questions