No KeyDown event on my VB.NET Webform - asp.net

I'm new to VB.NET webform development, but an old VB/Access developer.
I've used Keydown, Keypress events before in my normal development but cannot find this event with this new web development project I'm starting.
I'm assuming it's something to do with the fact it's a web-form. However when I search I can't find others with this issue so thought I'd ask it here. Below is a screen-shot of the events I have on the text control on the web-form where I'm trying to put the keypress event.
(I wanted to attach my picture showing you the events in the list but I don't have 10 reputation points so won't let me include it).
Is this event not available for web-form development? Essentially what I want to do is have the page check that there is text is both the txtUsername and txtPassword controls before enabling the "Log In" command button.
All I have in the drop-down list for the control is:
(Declarations)
DataBinding
Disposed
Init
Load
PreRender
TextChanged
Unload

Consider using a RequiredFieldValidator:
<asp:TextBox id="Foo" runat="server"/>
<asp:RequiredFieldValidator id="Bar"
ControlToValidate="Foo"
Display="Static"
ErrorMessage="*"
runat="server"/>
And in your submit button's click handler:
If Page.IsValid Then
...
Else
...
End If
You will probably also want to use the HTML5 required attribute:
<asp:TextBox id="Foo" runat="server" required="required" />
You might also consider using aria-required:
<asp:TextBox id="Foo" runat="server" required="required" aria-required="true" />

Related

Prevent webform postback onclick button

I have a webform with an asp button that when clicked, needs to perform a function in c#. The function does not affect anything on the page so I do not need a refresh of the page.Is there any way to prevent an asp button from doing a postback? I am not familiar at all with JavaScript so I need to perform this function in c#. Surely there is a way. I have researched but nothing works. CausesValidation="false"' does not work. UseSubmitBehavior=false does not work. And neither does setting the OnClientClick to return false. Keep in mind I am not using JavaScript. Anyone know how?
Add a ScriptManager in your Page first,
Then add an update panel which will have all the controls. This will prevent the entire page being posted back.
here goes an example
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="pnlHelloWorld" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblHelloWorld" Text="Click the button!" />
<asp:Button runat="server" ID="btnHelloWorld" OnClick="btnHelloWorld_Click" Text="Update label!" />
</ContentTemplate>
</asp:UpdatePanel>
If you do not want to monkey with the ScriptManager or the server side stuff, you can always use an HTML control as in:
<input type="button" id="myButton" value="Do Something" onclick="doSomeFunction(); return false;" />
OR
<button id="myButton" onclick="doSomeFunction(); return false;">Do Something</button>
I did discover that attaching the click event to a button element via jQuery will result in a postback (at least that's what happened to me), so I switched to the tried and true input element.

Hide validations on form post back in aspx page

I have put server side validations for each text box using <asp:RequiredFieldValidator/>. I have called ClearFields() method on page load that will clear all fields on the form when the button is clicked. The problem is that when the form gets posted and the fields are cleared, the validation message appears again. How to hide the validation messages on form post back. I am sorry, but its been years I have not coded in aspx and I can't find any solution online.
This is the textbox code:
<asp:TextBox runat="server" CssClass="form-control" placeholder="Your Name *" ID="name"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="name" ErrorMessage="Name seems empty" CssClass="help-block text-danger"></asp:RequiredFieldValidator>
This is the button code:
<asp:Button runat="server" class="btn btn-xl" Text="Send Message" ID="submit" OnClick="submit_Click" CausesValidation="false"/>
I guess you are getting the validation message due to button used for clearing the fields. Set CausesValidation property of button to false:-
<asp:Button ID="ClearButton" runat="server" CausesValidation="false" Text="Clear" />
Also, please note asp:RequiredFieldValidator works on client side i.e on the browser it is not a server side validation.
Update:
Since you are clearing your fields on click of button, you can clear in submit_Click method itself after sending the email instead of page load. Ideally you should have a separate button to clear the form though.

Asp Focus on input jumping bug

I've got a web form using asp.net. In this form I have a lot of inputs.
For one of the drop downs whenever you press it, the focus jumps to the next text box.
This is in a update panel, because there is some server side work required for filtering, hiding, etc.
If the User chooses Australia from visaType_filter then it hides visaType_dd and shows visaType_tb. If they choose NZ its the other way around.
Now my question:
Is there a bug or something that makes focus jump off of a drop down when you click on it to go to the next input (or control)?
Code:
<fieldset>
<asp:UpdatePanel ID="visaTypeUpdatePanel" runat="server">
<ContentTemplate>
<label>Visa Type Number</label>
<label>
<asp:DropDownList ID="visaType_filter" runat="server" Width="40%" OnSelectedIndexChanged="visaType_filter_SelectedIndexChanged" AutoPostBack="true"/>
<asp:TextBox ID="visaType_tb" runat="server" Width="40%" OnTextChanged="visaType_tb_blur" AutoPostBack="true"/>
<asp:DropDownList ID="visaType_dd" runat="server" Width="40%"/>
<asp:Literal ID="visaType_literal" runat="server" />
</label>
</ContentTemplate>
</asp:UpdatePanel>
</fieldset>
<fieldset>
I resolved my problem by using jQuery and Ajax in place of UpdatePanels.
AFAIK, when UpdatePanel gets triggered, the focus does not get maintained. So ideally, you shouldn't be getting any focus at all.
Regardless of reason, you can work-around the issue using ScriptManager.SetFocus method to maintain focus on the drop-down.
You can also have client side solutions for maintaining focus - they essentially work by hooking into AJAX requests to remember the focused control before update panel is triggered and then restoring it back when update panel post-back is completed: see this link for one such solution.

validation problem in asp.net

I am using Page.Isvalid function in my web page.I have two buttons in my web page namely "save" and "generate".when i click the save button the validation summary will be invoked in which all the validations in this page will be shown.
Now I dont want to show a particular validation message for the "Save" button, but the same validation message should be shown specifically to "generate" button in the same page.
But i am using Page.Isvalid in the "save" button click which is displaying all the validation messages in the page.
Any help would be deeply appreciated.Thanks
You can use ValidationGroup(s) to do this.
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="R1" runat="server"
ErrorMessage="*"
ValidationGroup="GenerateOnly"
ControlToValidate="TextBox1" />
<asp:Button ID="Generate" runat="server" Text="Generate"
ValidationGroup="GenerateOnly" />
<asp:Button ID="Save" runat="server" Text="Save" />
</div>
</form>
This example triggers the validator if Generate is clicked, but not when Save is clicked, and also works when calling Page.IsValid in the buttons onclick function.
not entirely sure what you mean but here goes.
If you want both the save and generate buttons to generate validation messages then why not move the Page.Isvalid along with the validation code into another method. Call this method from both the save and generate methods.
If I've got the wrong end of the stick please let me know a bit more.
Cheers Tigger
Ya buddy if you have to call the same function for two different buttons it is not possible without the concept of overloading. Otherwise you have to create two methods.
But I'am not sure as i am a begginer

What is the minimum amount of HTML an UpdatePanel requires before it falls over?

If for instance, I was to give a response back to an ASP.Net Update Panel page, but use Response.Write and then end it before anything was rendered, what is the minimum I would need to write in the Response.Write?
If you take the following UpdatePanel ...
<asp:UpdatePanel runat="server" id="updatePanel" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:TextBox runat="server" ID ="textbox1"/>
<asp:Button runat="server" ID="go" OnClick="OnGo" text="Go"/>
</ContentTemplate>
</asp:UpdatePanel>
... then click the button and view the response in Firebug, you'll get something like this (I've truncated the viewstate and event validation to make it a bit more readable):
265|updatePanel|updatePanel|
<input type="text" name="textbox1" value="content" id="input1" />
<input type="submit" name="go" value="Go" id="go" />|
52|hiddenField|__VIEWSTATE|/wXPZwUK...|
64|hiddenField|__EVENTVALIDATION|/wEWB...|
0|asyncPostBackControlIDs|||0|postBackControlIDs|||
12|updatePanelIDs||tupdatePanel|0|childUpdatePanelIDs|||
11|panelsToRefreshIDs||updatePanel|2|asyncPostBackTimeout||90|20|
formAction||Upagename.aspx|13|pageTitle||Untitled Page|
All that extra data is stuff that the ScriptManager needs to rebuild the contents of the UpdatePanel: control ids, the page name, viewstate, etc.. This is what you'd have to Response.Write manually for the ScriptManager to be able to do its job.
For details, see "ScriptManager Enables AJAX In Your Web Apps". At the end of the section titled "Putting the AJAX in ASP.NET AJAX", the author explains what's going on:
Finally, the client framework gets the
asynchronous response from the server
and parses out data. The ScriptManager
control has packed into the response
all the control IDs and new markup so
the client framework can simply
perform scripting operations on the
browser's document object model to
update the page content.

Resources