since I am new to ASP .NET I wonder whether this is normal behavior or something strange. I am trying to convert C# WinForms app to web app and it is behaving not as I expected.
In desktop app I get data after user press enter on textbox. Data are loaded I can modify it and then, when I am done changing data I can press one of two buttons and data are saved to database or form is cleared. Something like this.
private int idOrder;
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(load_data)
{
label1.Text = "some loaded data";
...
else{ MessageBox.Show("Something went wrong. No data loaded");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//save to database;
}
But When I try to do the same in ASP.NET Web form, whenever I press enter on textbox all controls are fired (as it is written in source code line after line) when page is refreshed. Could you please advice we what should I do to replicate same behavior as in winforms e.g. user input - enter - load data - modify - press button when I want. Thank you.
//Is the view state thing I am looking for?
Here is a snippet to get you started. Keep in mind that posting a HTML form when pressing enter is default behavior for websites. So the first thing is preventing that by wrapping the TextBox and Submit Button with a Panel with a DefaultButton assigned to it. Now when the enter is pressed in TextBox1, it will post the form as dummyButton
Next we add an OnTextChanged event to TextBox1 and set AutoPostBack to true. This will fire when the focus of TextBox1 is lost. You could remove this and just use the enter with dummyButton.
The dummyButton is added to ensure the form will PostBack when enter is pressed in TextBox1 to get the desired behaviour.
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:LinkButton ID="dummyButton" runat="server" OnClick="TextBox1_TextChanged"></asp:LinkButton>
</asp:Panel>
And then in code behind.
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//load from database
Label1.Text = TextBox1.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
//save to database
Label1.Text = "Data saved!";
TextBox1.Text = "";
}
You can also prevent PostBack with enter like this. The JavaScript will block all enters. Depending on your page design you could set this in the <body> tag for example.
<div onkeydown="return (event.keyCode!=13);">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
</div>
Related
I have a textbox that users can enter comments into, and then click save. When they click save, some code executes in the background, but their comments stay in the textbox so that they can see them. I want to display some text above the box that says "Comments saved" when they click save, so that they know that their request went through, since nothing else changes on the page after clicking save. However, they can then change the text in the textbox, and it won't save unless they click "save" again. So, I want the "Comments saved" text to disappear if they change the text in the box. However, I cannot set AutoPostBack="true", because it will mess up some other stuff that's happening on the page. Is there a way to make the "Comments Saved" text disappear when the textbox is changed, without reloading the page? Here is the code I have so far:
<div id="savedMsg" runat="server"><p class="msgAlert"> Comments Saved! </p></div>
<div>
<asp:Panel runat="server" DefaultButton="Save">
<asp:TextBox runat="server" ID="TextBox" CssClass="form-control" TextMode="MultiLine"/>
</asp:Panel>
</div>
Here is the code behind:
protected void SaveComments(object sender, EventArgs e)
{
//some code here that is unrelated
savedMsg.Visible = true;
}
Ideally, I would set TextChange="SomeMethod" and include this in my code behind:
protected void SomeMethod(object sender, EventArgs e)
{
savedMsg.Visible = false;
}
but I can't enable AutoPostBack, so this event never happens.
You can hide the message on the onkeydown event of the TextBox:
<asp:TextBox runat="server" ID="TextBox" onkeydown="hideSaveMessage();" CssClass="form-control" TextMode="MultiLine" />
with the Javascript code:
<script type="text/javascript">
function hideSaveMessage() {
var div = document.getElementById('<%= savedMsg.ClientID %>');
div.style.visibility = 'hidden';
}
</script>
You could also use div.style.display = 'none' to hide the message but that causes the TextBox to "jump" to a new position, which is somewhat disturbing.
I used document.getElementById('<%= savedMsg.ClientID %>') to account for any modification applied to the div ID by ASP.NET. The simpler syntax document.getElementById('savedMsg') may work in your case.
My ASP.NET website doesn't see newest Session values. When my form is being sent to the server after clicking the button, the result sent back to browser depends of Session values. I process them in Page_Load method. It doesn't see the last change od Session values, but one before last. It looks like button's event handlers are executed before page's event handles but I'm not sure if it is like that.
I tried to do my code in other methods like Page_PreInit etc, but it's still the same like that. The only thing that works for me is refreshing the page: Response.Redirect(Request.Url.AbsoluteUri); after any change of any Session value, but it resets values of all controls, which I want to be the same as before. Is there a better solution?
Example: when It runs first time, the label's text is "Click the button", but when I click any of the buttons one time, nothing happens. When I click any of the button second time, label's text is the value of first click (even if I click A and then B, value changes to A after clicking B).
form:
<form id="form1" runat="server">
<div>
<p>
<asp:Label ID="Label1" runat="server" />
</p>
<p>
<asp:Button ID="Button1" runat="server" Text="A" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="B" onclick="Button2_Click" />
</p>
<p>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
</div>
</form>
Event handlers:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["Letter"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Letter"] = "A";
}
protected void Button2_Click(object sender, EventArgs e)
{
Session["Letter"] = "B";
}
Global method:
void Session_Start(object sender, EventArgs e)
{
Session["Letter"] = "Click the button";
}
Without your code its very difficult, but is sounds like you are setting a Session collection value in the code of a button's click event( or some other control/event ), and expecting it to be in the SESSION collection during the Page_Load event.
It doesn't work like that - When the page request comes in, Page_Load happens before the control's events.
Instead of Page_Load use Page_PreRender this event occurs before the page is prepared to be sent back to the client.
Your addition of the code confirms the above.
Normally I wouldn't use Session_Start to initialize stuff like this, use Page_Load and IsPostBack property
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
Label1.Text = "Click the button";
}
}
im trying to make a user control to popup when a button is clicked! now i dont want to hide the user control and then show it when the button is clicked,
im NOT trying to do this:
test1.aspx
<uc1:PopUp ID="PopUp1" runat="server" Visible="false" />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
test1.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
if(PopUp1.Visible==false)
PopUp1.Visible = true;
else
PopUp1.Visible = false;
}
i want the page loads without it and if the button is clicked it fires! is it possible?
btw, im open to any other suggestion other than using user control
thank you in advance
I have a user control that allows the user to add/edit a worker. When the user clicks the 'Add Worker' button the user control appears in a DevExpress Popup. All of the following button are in an update panel to prevent Postbacks.
When I edit a user (pencil) everything works fine. To edit a user I enter a last name, click search (magnifying glass) and then click edit (pencil). It's only when I load the page and click add the save/cancel buttons do not work.
I add the control in asp.net
<dx:PopupControlContentControl ID="PopupControlContentControl2" runat="server" SupportsDisabledAttribute="True">
<uc:WorkerAddEdit ID="wae" runat="server" OnOnWAECancelEvent="wae_OnWAECancelEvent" OnOnWAESaveEvent="wae_OnWAESaveEvent" />
</dx:PopupControlContentControl>
Here is the C# code behind the edit (the one that works correctly. The pencil)
protected void btnEditWorker_Click(object sender, EventArgs e)
{
SetupSessions();
wae.WorkerEdit = loadedWorker;
pucAddEditWorker.HeaderText = "Edit Worker";
pucAddEditWorker.ShowOnPageLoad = true;
}
Here is the C# code behind for the add (the round + that doesn't work)
protected void btnAddWorker_Click(object sender, EventArgs e)
{
wae.WorkerEdit = null;
pucAddEditWorker.HeaderText = "Add Worker";
pucAddEditWorker.ShowOnPageLoad = true;
}
Here is the asp.net section of the save and cancel button. This shows both onClick calls
<td><dx:ASPxButton ID="btnSave" runat="server" Text="Save" Theme="MetropolisBlue"
Width="50px" Height="20px" style="float:right;" onclick="btnSave_Click" /></td>
<td><dx:ASPxButton ID="btnCancel" runat="server" Text="Cancel"
Theme="MetropolisBlue" Width="50px" Height="20px" style="float:right;"
onclick="btnCancel_Click" /></td>
Here is the events in the code behind
protected void btnCancel_Click(object sender, EventArgs e)
{
//Do Work Here
}
protected void btnSave_Click(object sender, EventArgs e)
{
// Do Work Here
}
If I put a break point on either the save or cancel click event nothing ever happens. I've been googling for a while now with no luck.
Thanks in advance.
In Design mode of the form, have you tried clicking the control once, and check it on Properties (Events) window? Maybe the Click event does not have any method selected.
Select the btnSave_Click method in the Click label.
I figured out what the problem was. I am using a textbox from DevExpress. Further up in my code I had the textbox as follows:
<dx:ASPxTextBox ID="txtPhoneNumber" runat="server" Width="100px" Theme="MetropolisBlue" >
<MaskSettings Mask="(999) 000-0000" IncludeLiterals="None" />
<ValidationSettings Display="None">
</ValidationSettings>
</dx:ASPxTextBox>
Because I had the 0's in the mask it was trying to validate whatever was in the text box. Because I turned off the validation settings (ValidationSettings Display="None") I never saw the error but it was still validating. I made a change to this:
<dx:ASPxTextBox ID="txtPhoneNumber" runat="server" Width="100px" Theme="MetropolisBlue">
<MaskSettings Mask="(999) 999-9999" IncludeLiterals="None" />
<ValidationSettings Display="None">
</ValidationSettings>
</dx:ASPxTextBox>
and everything worked fine. I just started using DevExpress and it shows! Thanks to everyone for the help!
I have session in a page and would like to reset this session when the user text input changes. For example: account number xxx will have a session, once account number changes to yyy i would like to reset session. I dont have login to this page so cannot dump session upon logout. help is appreciated. thank you in advance.
Try this:
in aspx:
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
in code behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session.Clear();
}
So you'll need an event handler that is fired when the account number changes (or whenever the user text input changes), and then according to this post you'll need to either use Session.Clear() which just removes all values from the object or Session.Abondon() which will destroy the session and trigger the Session_OnEnd event. Whichever you use will depend on what you want to accomplish.
make ajax call on "onkeyup" event of textbox and set new value in that session, you will get new value of textbox and can set it in session.
Use TextChanged event of TextBox and destroy the session using Session.Abandon() or Session.Clear() method
This will help you
If you write down code here, then we can give you exact answer
try this
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
code behind
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session["your_sesion_name"]=TextBox1.Text;//or what ever you want to update value
}
There are a couple of ways to do what you are asking.
Option one:
You could uses the text boxes ontextchanged event
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
in code behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session.Clear();
}
The pit fall here is the OnTextChanged event is only fired once the textbox has lost focus.
Option 2:
Uses JavaScript to capture the onkeyup event and make an ajax call back to the server to update your session information
The pit fall here is that every time the user enters a character into the textbox you would be making the ajax call. Although you could add checks so that the call is only made if the account number is a certain length.
Option 3:
You could add a button to your form for the user to click once they have entered the account number. This would avoid needless calling the server on every key press as in option 2. Then within you buttons click event you could do the work with your session
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
Code behind
protected void btnSubmit_Click(object sender, EventArgs e)
{
// do session work
}