I'm trying to make a web page with a login screen. I have 2 textboxes that are used to type the username and the password. I'm using an AJAX Update Panel, so in case that username doesn't exist (I'm using SQL Server), it makes visible a label that shows the error. The problem is that when the user types in the username textbox, AutoPostBack fires and when you move to the password textbox, you have to click twice in order to start typing in the password textbox.
C# code:
protected void txtUsername_TextChanged(object sender, EventArgs e)
{
string msgError = businessLogin.checkUsername(txtUsername.Text);
if (msgError != "")
{
lblWrongUser.Text = msgError;
lblWrongUser.Visible = true;
btnLogin.Enabled = false;
}
else
{
lblWrongUser.Visible = false;
btnLogin.Enabled = true;
}
}
ASP.Net:
<asp:TextBox ID="txtUsername" clientidmode="Static" runat="server" OnTextChanged="txtUsername_TextChanged" AutoPostBack="True"></asp:TextBox>
Related
I have two pages:
Drop Down List that User picks a Customer Name from and a load button (Panel 1)
Page with Customer Order Information (Panel 2)
On the 2nd page, I have a check box that will show Customer Details in a DetailsView if it is checked.
My issue is when I go to click on the check box, it brings me back to my First Page with the DownDrop List, which is Panel 1. I have to click the load button to see the second panel again and once it's clicked again, it shows the DetailsView which the check box checked.
I have tried everything, this is what my code looks like:
protected void Page_Load(object sender, EventArgs e)
{
// always show first panel when page loads
pnlFirstPage.Visible = true;
pnlSecondPage.Visible = false;
}
protected void btnLoadOrders_Click(object sender, EventArgs e)
{
// hide the first page and continue to second page
pnlSecondPage.Visible = true;
pnlFirstPage.Visible = false;
// if statement to show details view of customer details
if (cbCustomerDetails.Checked == true)
{
dvCustomerDetails.Visible = true;
dvCustomerDetails.DataBind();
pnlFirstPage.Visible = false;
pnlSecondPage.Visible = true;
}
Ok, so the issue/problem is that keep in mind that EVERY post-back will trigger the page load event.
In other words, any button any dropdown/combo box, or ANY code event that triggers a post-back will fire the page load even EACH time, and THEN your button/event code stub runs.
Thus, for really what amounts to the last 200+ web form pages I built?
EVERY page for setup/loading controls, data etc. will have a if not postback code stub on the page load command.
So, say this markup in panel1
<asp:Panel ID="Panel1" runat="server">
<h3>Select Hotel</h3>
<asp:DropDownList ID="cboHotels" runat="server"
DataValueField="ID"
DataTextField="HotelName" Width="198px">
</asp:DropDownList>
<button runat="server" id="btnRun" title="Search"
style="margin-left: 15px"
type="button" class="btn"
onserverclick="btnRun_ServerClick">
<span class="glyphicon glyphicon-home"></span>View
<br />
</button>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" >
<h3>Hotel Infomration</h3>
<div id="EditRecord" runat="server"
style="float:left;display: normal;border:solid 2px;padding:12px;border-radius:12px">
<h2>Edit Hotel</h2>
<div style="float:left" class="iForm">
<label>HotelName</label>
<asp:TextBox ID="txtHotel" runat="server" f="HOtelName" width="280" /> <br />
bla bla bla for panel2.
so, now my code on startup can be to load up the combo box/dropdown list, and set Panel 1 visible, and panel 2 visible = false.
We have this code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Panel1.Visible = true;
Panel2.Visible = false;
LoadData(); // load our combo box
}
}
void LoadData()
{
SqlCommand cmdSQL =
new SqlCommand("SELECT ID, HotelName FROM tblHotelsA ORDER BY HotelName");
cboHotels.DataSource = MyRstP(cmdSQL);
cboHotels.DataBind();
cboHotels.Items.Insert(0, new ListItem("Select", ""));
}
DataTable MyRstP(SqlCommand cmdSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (cmdSQL)
{
cmdSQL.Connection = conn;
conn.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
And the button to view the details (in panel2), then can look like this:
protected void btnRun_ServerClick(object sender, EventArgs e)
{
if (cboHotels.SelectedIndex > 0)
{
string strSQL = "SELECT * FROM tblHotelsA WHERE ID = #ID";
SqlCommand cmdSQL = new SqlCommand(strSQL);
cmdSQL.Parameters.Add("#ID", SqlDbType.Int).Value = cboHotels.SelectedItem.Value;
DataTable rstData = MyRstP(cmdSQL);
General.FLoader(EditRecord, rstData.Rows[0]);
Panel1.Visible = false;
Panel2.Visible = true;
}
}
And now the results are this:
So, just keep in mind that the REAL first page load has to be inside of that if !IsPostBack stub.
You can think of that code stub the REAL or "first" page load. Thus, loading up a gridview, loading up a combo box, and say setting panel1 visible, and panel2 hidden all typical code that one would place in the real page startup code.
It quite much means you can't really build a working page without taking into account that page-load fires each and every time a event occurs on the page.
In fact, if you adopt a up-date panel, then the above will holds true.
I am trying to implement validations on textbox like if null then display a message to fill the empty field and to check the length of the text entered. I have written the code for it inside TextBox_TextChanged event but it's not working. The event is not getting fired and the user can is able to signup without a username, that is my problem at the moment. Do I have to trigger the event manually? Here is a glimpse of what I am doing:
protected void FirstN_TextBox_TextChanged(object sender, EventArgs e)
{
String firstNameEntered = FirstN_TextBox.Text;
if (firstNameEntered != null)
{
if (firstNameEntered.Length <= 20)
{
MessageBox.Show("Inside text box");
}
else
{
}
}
else
{
FirstN_TextBox.Focus();
MessageBox.Show("Please fill the marked field");
}
change it like this:
From:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged"></asp:TextBox>
To:
<asp:TextBox ID="FirstN_TextBox" placeholder="First name" runat="server" Width="225px" OnTextChanged="FirstN_TextBox_TextChanged" AutoPostBack ="true"></asp:TextBox>
I have a username TextBox and a Label which should update to (V or X) when the TextBox text is changed. The label is updated only if, for example I press a button which automatically refreshes the page.
Here is the code:
<asp:TextBox ID="username" runat="server" OnTextChanged="checkUsername" Width="80%"></asp:TextBox>
<asp:Label ID="usernameCheck" runat="server" CssClass="checkL"></asp:Label>
And the aspx.cs
protected void checkUsername(object sender, EventArgs e)
{
if (username.Text.Length < 3 || username.Text.Length > 15)
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
if (myBl.checkUsername(Convert.ToString(username)))
{
//---Label = X (in red)
usernameCheck.Text = "\u2715";
}
else
{
//---Label = V (in green)
usernameCheck.Text = "\u2713";
}
}
}
Thanks for any help.
You need to add AutoPostBack="true" to your TextBox. This will cause it to post back and for that server side event to fire.
There are much better ways of doing what you are trying to accomplish though, most of which do not require a full page postback. I would try to make an AJAX call using the javascript change event, and using something like a callback method.
I have small page, it looks follows:
Some header
Label with text only for logged users
Hyperlink
Logout button
Here is the behind code:
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.Name == "")
{
Label1.Visible = false;
Controls.Remove(Label1);
}
else
{
Label1.Visible = true;
}
}
Question is: When non logged user loads page, the label is not shown. But how to move the hyperlink and logOutButton and thus hide the white space on place, where the label is invisible ?
Thanks
You have to use the css display:none here
You have to create an Authentication Panel it's a asp:Panel extended and it will able you control "Logged and non Logged user without any headache".
example:
public class AuthenticatedPanel : Panel
{
public string Action { get; set; }
public AuthenticatedPanel()
{
this.Load += new EventHandler(AuthenticatedPanel_Load);
}
void AuthenticatedPanel_Load(object sender, EventArgs e)
{
//your logic to check wether is user is legit or not
// and then
this.Visible = false;
}
}
//then using
<asp:AuthenticatedPanel ID="pnl" runat="server">
your content here
</asp:AuthenticatedPanel>
In summary, per the information above and some research, here's what worked for me. The white space IS removed by using button.Visible = false in codebehind but the "br" tags are remaining, so wrap them inside individual panels and hide the panel, not the button, i.e. panel.Visible = false and it will remove all the white space.
Here's an example, on the .aspx page, create a panel with a control inside(notice the "br" tag inside it):
<asp:Panel ID="panel" runat="server">
<asp:Button ID="button" runat="server"/>
<br />
</asp:Panel>
next, on codeBehind, hide the panel instead of the button:
panel.Visible = false;
What am I doing wrong since the content in the < EditItemTemplate > is not displayed when I click the Edit button?
<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging">
<ItemTemplate>
//display content here
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
This text should be displayed when I click the Edit button
<asp:LinkButton runat="server" ID="UpDateButton" CausesValidation="false" CommandName="Update" Text="Lagre" />
</EditItemTemplate>
</asp:FormView>
Update
This is my code-behind:
namespace development.templates
{
public partial class HotelDetails : TemplatePage
{
static Hotel hotel;
protected DataRow drHotel;
DataTable dtCriteria;
DataTable dtHotel;
private HotelCriteria hotelCriteria;
protected void Page_Load(object sender, EventArgs e)
{
int hotelID = Convert.ToInt32(Request.QueryString["hotelid"].ToString());
if (!IsPostBack)
{
if (hotelID != 0)
{
// Create Hotel instance based on hoteID.
hotel = new Hotel(hotelID);
drHotel = hotel.hotelData.Rows[0];
dtHotel = hotel.getHotelsByCity(drHotel["city"].ToString());
// Hotel scrore is calculated from a score which is derived from certain criterias.
hotelCriteria = new HotelCriteria(hotelID);
dtCriteria = hotelCriteria.getHotelCriteria();
//Set datasource for hotel list in right sidebar.
hotelListByCity.DataSource = correctList(dtHotel, hotelID);
hotelListByCity.DataBind();
// Set datasource for current hotel
fwHotelDetails.DataSource = hotel.hotelData;
fwHotelDetails.DataBind();
}
}
}
protected void fwHotelDetails_DataBound(object sender, EventArgs e)
{
//Find the criteria list and set the datasource
Repeater rep = (Repeater)fwHotelDetails.FindControl("repCriteriaScore");
rep.DataSource = this.dtCriteria;
rep.DataBind();
// Controll is user is logged in. If logged in, then user may add, edit or delete hotel record.
System.Security.Principal.IPrincipal user = Context.User;
if ((user != null) && user.Identity.IsAuthenticated){
Panel panel = (Panel)fwHotelDetails.FindControl("administrationPanel");
panel.Visible = true;
}
}
protected void fwHotelDetails_ModeChanging(object sender, FormViewModeEventArgs e)
{
switch (e.NewMode)
{
case FormViewMode.Edit:
MessageLabel.Text = "Edit mode";
fwHotelDetails.ChangeMode(FormViewMode.Edit);
break;
case FormViewMode.ReadOnly:
MessageLabel.Text = "Read mode";
break;
case FormViewMode.Insert:
MessageLabel.Text = "Insert mode";
break;
}
}
}
}
The EditItemTemplate also won't show up if the record is empty. In other words, if you have a gridview that you select a detail record from that is feeding the formview, and their is no detail for the selected grid item, then the form won't show up at all. I check to see if the detail record is null, and if so, I set the formview or detailsview to "insert" mode. so they can enter a new record.
In your function fwHotelDetails_ModeChanging, add this:
fwHotelDetails.ChangeMode(FormViewMode.Edit)
i.e.
Protected Sub fwHotelDetails_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles fwHotelDetails.ModeChanging
fwHotelDetails.ChangeMode(FormViewMode.Edit)
End Sub
Ok have you tried putting a breakpoint on fwHotelDetails_ModeChanging and then debugging the App? Does the breakpoint get hit when you click the edit button.
At least this will tell you where your problem lies. That is [1] the events are not hooked up correctly or [2] there is something going wrong with ChangeMode.
I realise this isnt a solution but if you tell me whether the breakpoint hits i can help you further.