简体   繁体   中英

asp.net set value to a label after page refresh

i need some help.

i have a user registration page with a label that say success on not and why not.

i need to refresh the all page after the user clicked the button AND show him the label value. How can i do this ?

the code:

<asp:Button ID="CreateUserButton" runat="server" Text="Go" onclick= CreateUserButton_Click/>
<asp:Label ID="ErrorMessage" runat="server" Text="Label"></asp:Label>

c#:

protected void CreateUserButton_Click(object sender, EventArgs e)
try
                    {
                        ErrorMessage.Visible = true;
                        ErrorMessage.Text = "Registered successfully, ";
                        Response.Redirect("~/register.aspx", false);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }

Generate a click event "OnClick" for the button create user and write the code in that event

Please have a look at below Code and Update your code as mentioned below :

ASPX Page

<asp:Button ID="CreatUser" runat="server" Text="Go" OnClick="button_CreatUser" />
<asp:Label ID="ErrorMessage" runat="server" Text="Label"></asp:Label>

Code Behind (.CS) Page

Update:

Update your code behind as mentioned below :

protected void Page_Load(object sender, EventArgs e)
{
    if (null != Session["ErrorMessage"])
    {
        ErrorMessage.Visible = true;
        ErrorMessage.Text = Session["ErrorMessage"].ToString();
    }
}

protected void button_CreatUser(object sender, EventArgs e)
{
    try
    {
        Session["ErrorMessage"] = "Registered successfully, ";
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

You need to bind the click even t and write the code you have in the event handler.

HTML

<asp:Button ID="CreatUser" runat="server" Text="Go"   OnClick="GreetingBtn_Click"  />

Code behind

protected void CreatUser_Click(Object sender,  EventArgs e)
{
     try
     {
        ErrorMessage.Visible = true;
        ErrorMessage.Text = "Registered successfully, ";
     }
     catch (Exception ex)
     {
        throw new Exception(ex.Message);
     }

}

The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button), MSDN .

use session variables/View state to store data and after refreshing display it on form.

http://msdn.microsoft.com/en-us/library/75x4ha6s.ASPX

http://msdn.microsoft.com/en-us/library/ms178581.aspx

Refer the above link will give more clarity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM