text box clear issues - c#

Ok im having a problem with the text boxes of my page holding values of the login info when i navigate to the start up page via the back button of the browser. i need the text boxes to clear when the page is loaded but when i use the code im using the text boxes stay equal to "" instead of the user input when the login button is clicked. heres my code.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack == true)
{
txtEmailLog.Text = "";
txtPasswordLog.Text = "";
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
PlayerModel plyrModel = new PlayerModel();
PlayerBLO plyrBLO = new PlayerBLO();
List<PlayerModel> models = plyrBLO.GetAllPlayers();
foreach (PlayerModel player in models)
{
if (txtEmailLog.Text == player.PlayerEmail && txtPasswordLog.Text == player.PlayerPassword)
{
Session.Add("Player", player);
Response.Redirect("PlayerMenu.aspx");
}
else
{
lblMessage.Text = "Invalid Login! Retry or Register.";
}
}
}

When ever your page is loaded the following script will run.Include script file jquery-1.4.1.js in your LogIn form it will work even when you click Browser Back Button.
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// clear the text box values onload.
$('#<%=txtEmailLog.ClientID%>').val('');
$('#<%=txtPasswordLog.ClientID%>').val('')
});
</script>

there are other ways, this is one of them
protected void btnLogin_Click(object sender, EventArgs e)
{
// rest of your code
txtEmailLog.Text = "";
txtPasswordLog.Text = "";
}

Related

set the asp:textbox val() in jQuery and get the Text of the same on the server side code

I have successfully set the text of asp:textbox using jQuery val() function, now I want the same value of the textbox on click of asp:button on the server side code.
$("#textboxId").val('some text');
protected void button_Click(object sender, EventArgs e)
{
// getTheText is blank
string getTheText = textboxId.Text.Trim();
}
<script type="text/javascript">
$(document).ready(function () {
$('#<%= TextBox1.ClientID %>').val("my value");
});
</script>
and in code behind on button click use
protected void Button1_Click(object sender, EventArgs e)
{
var value = TextBox1.Text;
}
this will work. it work for me i test it.
I also had the same problem and finally found a solution.
string getheText =Page.Request.Form["textboxId"].ToString().Trim();
But be careful if you use "Content" in master page the id must be like that
string gettheText = Page.Request.Form["ctl00$ContentPlaceHolder1$textboxId"].ToString().Trim();
if your textbox is an aspx server control then you can directly set Text by using
textboxId.Text = "Some Value";

Testing to verify that a button is clicked first before another is clicked "Webfomrs"

I used an if statement saying that if lblTotalAmount is populated then you can be able to click the second button. Because if lbltotalamount is populated then the first button was clicked to populate it. However, with my code below it works by showing the error message if you try to click the second button before the first button but then if i do it in the correct order it will not redirect me to the page i stated below. How can i correctly state this so that it will work?
protected void btnSubmitOrder_Click(object sender, EventArgs e)
{
if (lblTotalAmount == null)
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}
You should be trying to validate on the Text property of the lablel instead
protected void btnSubmitOrder_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(lblTotalAmount.Text))
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}

managing and controlling the post back priority

I have a web page which is contained a Data Filter and a report.The Data Filter is a user control. The report is loaded inside the main page so totally i have two pages. one user control and one web page.
Now i am going to gather the data by clicking a button inside the user control then i can use it to filter the table, but it seems that during the post back it goes first to the Page_Load method of the main, not the user control so the report is constructed before filtering.The BtnPreviewReport_Click must be executed earlier than the page_Load.
What should i do ?
User control
protected void BtnPreviewReport_Click(object sender, EventArgs e)
{
Date = Year.Text + "/" + Month.Text + "/" + Day.Text;
}
Main Page
protected void Page_Load(object sender, EventArgs e)
{
string date = UserControls1.Date;
Response.Write(date);
}
Output : Nothing
I am not sure why the ButtonClick event should be run earlier than page load.
But here's a simple way to solve your question:
private bool isPageLoaded = false;
private bool isButtonClicked = false;
private void ButtonClick()
{
isButtonClicked = true;
doTheFirstThing();
if( isPageLoaded )
{
doTheSecondThing();
}
}
private void PageLoad()
{
isPageLoaded = true;
if( isButtonClicked )
{
doTheSecondThing();
}
// else let the button click handle the SecondThing()
}

Session variable has old value, needs to update with page redirect

I have session variable that dont update with new value. I have two pages, one were you enter the values and klick on the button and you get redirected to page 2 and there you can check your input, if this is wrong you click back-button and you go back to the first page where you can change the input but now when i click the button to validate again the new value does not show up in the session variable but only the old value. I have been readingabout session for the last day but i just cant find the problem, the behind code is below:
Page1
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["value1"] != null)
{
lbl1.Text = (String)this.Session["value1"].ToString();
}
}
public string info { get { return lbl1.Text; } }
protected void inputButton_onclick(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
Session["value1"] = info;
Response.Redirect("~/validpage.aspx");
}
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value1"] != null)
{
lbl2.Text = (String)Session["value1"].ToString();
}
}
protected void BackButton_Click(object sender, EventArgs e)
{
Session["value1"] = lbl2.Text;
Response.Redirect("~/Default.aspx");
}
Maybe i have staired my self blind on this code as to me this should not have this problem it is presenting. Any idea and help will be appreciated.
Every time Page1 loads, lbl1 is set to the contents of the session, unless it's never been set. So when you click the button, the lbl1 is first set back to the content of the session as the page is loaded. You then read this value back & but it back in the session.
try this instead:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
if (this.Session["value1"] != null)
{
lbl1.Text = (String)this.Session["value1"].ToString();
}
}
}
This says only set the value if we're not postback, ie NOT clicking a button on the page.
Also in page2, there's no need to call ToString AND cast to a string. Do either, not both.

How to popup a page on button click?

I want to pop up a new page in the main page of my application . The location must be in center,can't be sizable, it shouldn't be opened as a new tab in the main page , the bar with the options for : closing,minimize/miximize shouldn't be there.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.Attributes.Add("onclick", "window.open('WebForm1.aspx',null,'height=350, width=250,status= no, resizable= no, scrollbars=no, toolbar=no,location=center,menubar=no')");
}
But....
It's sisable,location is not in not in the center of the page.
The page is opened in the main page but as a new tab.
I don't know how to remove the bar with : closing,maximize,minimize
Can someone help me?
Thanks
Try this:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.Attributes.Add("onclick", "centeredPopup('WebForm1.aspx','myWindow','500','300','yes');return false");
}
<script language="javascript">
var popupWindow = null;
function centeredPopup(url,winName,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
popupWindow = window.open(url,winName,settings)
}
</script>
For more details - see this

Categories

Resources