Reputation: 115
how do i add a certain html code to a page on page load dynamically in asp.net?
eg : i need validate password. if the password is wrong.. on page load there should be a div named "error" appear just on the top in side a div called bodyWrapper
my current way of doing this is writing it inline in aspx file.. but i know this is not the proper way of mixing inline and code behind.
<div id="content">
<%
Session["loginError"] = "loginError";
try
{
if (Session["loginError"].ToString() == "lockout")
{
%>
<div class="errorMessage">
Your Account has been locked out. Please wait
<% Response.Write(Session.Timeout); %>
minutes and try again.
<br />
</div>
<%
}
else if (Session["loginError"].ToString() == "loginError")
{
%>
<div class="errorMessage">The user ID or password you entered does not match our records. Please try again. <br />
You may also securely recover your <a href="#">User ID</a> or reset your <a href="#">Password</a> online.
</div>
<%
}
}
catch
{
//cssClassName = "loginTextInput";
//Response.Redirect("login.aspx");
}
%>
Upvotes: 1
Views: 4510
Reputation: 377
I agree with the suggestion to checkout tutorials for frameworks like ASP.NET MVC, Razor etc. If you are trying to perform manual validation then you're already doing something wrong. There are many, many, many ways to do this with little to zero code. At the very least you could use ASP.NET Field Validators if you're designing a Web Forms app.
Upvotes: 0
Reputation: 71
I'm pretty sure you can do this in the codebehind of your Page_Load event handler. There are several ways, but try this:
In your markup, add a Panel control and initially set its visibility to false:
<asp:Panel ID="ErrorPanel" runat="server" Visible="False"></asp:Panel>
Then in the Page_Load of your pages codebehind, do your login logic and write to this panel. For example:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["loginError"].ToString() == "lockout")
{
LiteralControl errorControl =
new LiteralControl(String.Format("Your account has been locked out. Please wait {0} minutes and try again.", Session.Timeout));
ErrorPanel.Controls.Add(errorControl);
ErrorPanel.Visible = true;
}
// add more login logic for login error
}
catch(Exception) //you may want to capture more specific Exceptions
{
//handle the exception(s)
}
}
Upvotes: 0
Reputation: 13600
You have basically couple of options..
1.) mix logic with markup like you're already doing (not good in my opinion)
2.) add controls dynamically, something like this:
MyPage.aspx
<asp:Panel id="pnlErrorPlaceHolder" runat="server">
</asp:Panel>
MyPage.aspx.cs
if (i_found_some_error)
{
Label lbl = new Label();
lbl.Text = "An error occurred";
pnlErrorPlaceHolder.Controls.Add(lbl);
}
OR
3.) you can take advantage of an attribute called Visible, which helps to decide, whether the control will be a part of the response or not. That would look like something like this:
MyPage.aspx
<asp:Panel id="pnlErrorBox" Visible="false" runat="server">
Some info for the user if there was an error.
</asp:Panel>
MyPage.aspx.cs
if (there_was_an_error)
{
pnlErrorBox.Visible = true;
// and maybe set some additional info
}
Upvotes: 1
Reputation: 12431
Instead of having a , you should have an asp:Panel, and in the code behind toggle the visibility.
<asp:Panel ID="pnlAccountLocked" Visible="false" runat="server" CSSClass="errorMessage">
Your Account has been locked out. Please wait
<% Response.Write(Session.Timeout); %>
minutes and try again.
<br />
</div>
and in the code behind Page Load code:
private void Page_Load(object sender, System.EventArgs e)
{
pnlAccountLocked.Visible =
!(Session["loginError"].ToString() == "lockout");
}
Upvotes: 0
Reputation: 61812
If you simply want to write text to a div, give the div the runat="server"
attribute:
ASPX
<div id="error" runat="server" class="errorMessage"></div>
C#
if(IsPostBack && YouHaveAnError) {
error.InnerHtml = "[YourErrorMessage]";
}
Upvotes: 1