I have been attempting to fill in a few HTML input boxes from a C# form application. So far I am able to fill in the form and click buttons without issue. Triggering the form validation fields has been a whole different story. When I look at the events in the source, I see more then onfocus that is listed in code below. I was trying to avoid firing the javascript manually and I know this question has been asked, but none of the answers I see are working.I have attempted to simulate clicks, but still doesn't allow the form to be submitted and complains the input box is still empty until I click in it.
Events I see listed in browser debugger. These could be up stream and not correct, regardless I have attempted to fire them.
blur
click
focus
mouseout
mouseover
shown
code from input box that needs to be triggered.
onfocus="if (!this.tc) addLoadEvent(function() {var e = gel('sys2.incident_group'); if (!e.ac) new AJAXReferenceCompleter(gel('sys2.incident_group'), 'sys2.incident_group', '', 'true'); e.ac.onFocus();})" aria-required="true" aria-expanded="false">
var obj = webBrowser1.Document.Window.Frames[0].Document.GetElementsByTagName(tag).GetElementsByName(field);
HtmlElement ele = obj[0];
if (ele.Name == field)
{
ele.Focus();
ele.InvokeMember("blur");
ele.InvokeMember("click");
ele.InvokeMember("mousedown");
ele.InvokeMember("focus");
ele.InvokeMember("mouseout");
ele.InvokeMember("mouseover);
ele.InvokeMember("shown);
}
Update: attempted to use synchronous task and no change occurred. It almost appears lag from when the field's are being populated are keeping everything from working correctly. I can fire off one method filling in the boxes, then use a button for second and I pass the validation. When I see my submit button fire it occurs before the boxes are populated.
Related
I made my own textbox in UWP, now I want the user to add emojis from the Windows emoji-picker window.
The problem:
When I click any of the emojis with my application focused nothing happens. I check the events that could get triggered by that but non of them got. I also tried it with the ASCII-emojis, but they also don't work.
I checked the CoreWindow_CharacterReceived event and the Paste event, but neither gets fired.
The actual question:
Is there any event I can use to get the input of the emoji-picker or is there a different way?
Github-repo:
https://github.com/FrozenAssassine/TextControlBox-UWP
When I click any of the emojis with my application focused nothing happens.
The problem looks your TextBox is not in focus state, you could focus textbox manually after show Emoji pikcer. Then it will input emoji correctlly.
CoreInputView.GetForCurrentView().TryShow(CoreInputViewKind.Emoji);
YourTextBox.Focus(FocusState.Programmatic);
Update
For customing textbox you could refer to official text editor code sample that implement textbox with CoreTextEditContext.
This question already has answers here:
Is making an asp:Button control invisible enough to be sure users won't be able to click it?
(3 answers)
Closed 7 years ago.
This is in ASP.NET web forms, I have a save button on a screen. When I load the page initially, under certain conditions, the save button is not rendered.
button1.visible = false
In my button clicked event, I have this
public void button1_click(Object sender, EventArgs e)
{
SaveData();
}
The only security preventing the user being from being saved is on whether the save button is rendered.
In MVC, it would be trivial to access the save button action method just by making a HTTP POST to the server with my own modified request.
In ASP.NET Web forms, I'm a little bit confused because it relies on the encrypted ViewState being posted back. Do I still need to add this security to the button1_click event too? If so, then can you tell me how a client can fire a postback to the server that would reach the button click event without the button being visible?
That is one of the common mistakes about ViewState - it DOES NOT serve your click events and many other things.
Each click on button (or checkbox if autopostback is enabled) raises an form submitting to server. And all info about what was clicked is included in form postback data as plain text. Then server parses this data and, as your button has IPostBackDataHandler implemented, it raises appropriate events like "button id has been clicked". So, you can actually change request body:
D__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPD...&ctl00%24_pdContent%24txtEmail=qwe%40aeqw.ry&ctl00%24_pdContent%24btnRtnUser=Login&__EVENTVALIDATION=%2FwEWDAL424aUAQLjtYbqCAKu8qTUCQLXm%2BfNAwKk2O%2B4DgK3ypStCAL6q%2BaACgKBnvb9CQLr8ey6CALxoZvDCALFt96ABgLMorjMAwoW3zW69NNlOXygWNnB6luGVWnk
Above you can see content of input with id=ctl00__pdContent_txtEmail and clicked login button with id=ctl00__pdContent_btnRtnUser.
More explanation about server events in WebForms here.
And please read more about ViewState here or even better explanation here.
https://stackoverflow.com/a/24064375/279911
According to this answer on another question, it looks like it is impossible to reach the button click event if the button is not rendered as long as you have the relevant security settings set.
Yes, setting a button's Visible property to false is enough to prevent
its Click and Command events from being raised, as long as you don't
turn off the default ASP.NET security features.
I've got TextBoxes in a C# form. The user enters data, and then when they leave the control (almost always by hitting Tab), I check the data to make sure it's valid. If it is invalid, I want to highlight their text so they can immediately fix it rather than having to click it.
Right now, on Control.Leave, I validate their entry. This works just fine. However, since they hit Tab, right after they dismiss the error message, it goes on to the next object, even though I've got ((TextBox)sender).Focus();
How can I have the above line fire after the form Tabs to the next control.
You may want to look into Control.CausesValidation property
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx
You can validate the control prior to the user leaving focus rather than waiting on Focus moving itself.
And here's MSDN documentation for Control.Validating event, does a good job at laying out the sequence of events when gaining / losing focus of a Control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx
Notice how Control.Validating and Control.Validated are launched prior to Control.LostFocus. You can perform your validation step prior to allowing the user to lose focus of your Textbox.
There's also a pretty good previous answer on stackoverflow.com which outlines how to do this: C# Validating input for textbox on winforms
If you handle the Control.Validating event, setting e.Cancel to true will stop the change of focus from occurring.
Note that this method will also stop buttons from working, so you may need to set Control.CausesValidation to false on certain buttons.
You will also need the following snippet on the main form to allow the close button to work:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
Try using the LostFocus event on the TextBox to Focus it again
I am trying to change the CSS class of a dropdownlist on its onfocus event to change its background colour.
When the user clicks on the the dropdownlist, the CSS class is changed (the backgound colour is changed), but the list of options is not displayed until it is clicked again.
This is happening in IE7. It works fine in Firefox.
The only advice I have found anywhere on this problem suggests also using the onfocusin event:
http://www.eggheadcafe.com/software/aspnet/34297064/open-dropdown-list-only-with-double-mouse-click.aspx
but this did not have any effect.
The onfocus event is attached to the dropdownlist in the code behind:
ddl.Attributes.Add("onfocus", "setCssClass();");
The javascript the sets the class:
ddl.className = "Class1";
Please help!
Thanks to anybody who looked at this, but I have found the answer myself.
The problem was only occuring when the focus was coming from a few particular controls. These controls had onblur events that also set the class of the dropdown.
I had assumed that these onblur events would run before the onfocus/onfocusin, but it seems that this is not the case. The onblur events then seem to close the dropdownlist when applying the class change.
Anyway, I now check which control has the focus on these onblur events, and if it is the dropdown list, I don't run the main logic within the onblur and all now works as expected.
Putting in a hard coded "onfocus" event will force you to have 2 clicks when using Internet Explorer. It's a known quirk with IE. Although this solution isn't perfect. If you have an alert for example inside this anonymous function in the .focus() callback, you still have to click the dropdown twice. You just have to get creative.
As a workaround, just use the jQuery .focus event.
jQuery([dropdownlist object]).focus(function() {
... put code here
});
Background: I am customizing an existing ASP .NET / C# application. It has it's own little "framework" and conventions for developers to follow when extending/customizing its functionality. I am currently extending some of it's administrative functionality, to which the framework provides a contract to enforce implementation of the GetAdministrationInterface() method, which returns System.Web.UI.Control. This method is called during the Page_Load() method of the page hosting the GUI interface.
Problem: I have three buttons in my GUI, each of which have been assigned an Event Handler. My administration GUI loads up perfectly fine, but clicking any of the buttons doesn't do what I expect them to do. However, when I click them a second time, the buttons work.
I placed breakpoints at the beginning of each event handler method and stepped through my code. On the first click, none of the event handlers were triggered. On the second click, they fired.
Any ideas?
Example of Button Definition (within GetAdministrationInterface)
public override Control GetAdministrationInterface()
{
// more code...
Button btn = new Button();
btn.Text = "Click Me!";
btn.Click += new EventHandler(Btn_Click);
// more code...
}
Example of Event Handler Method Definition
void Btn_Click(object sender, EventArgs e)
{
// Do Something
}
Page_Load Method that calls GetAdministrationInterface
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsAsync)
{
List<AdministrationInterface> interfaces = <DATABASE CALL>;
foreach(AdministrationInteface ai in interfaces)
{
placeholderDiv.Controls.Add(ai.GetAdministrationInterface());
}
}
}
Good grief! I knew it was going to be something this stupid. Purely my fault of course and my lack of knowledge in ASP .NET.
After doing a multitude of Google searches and eventually being blocked by Google on suspicion of being a bot running automated scripts, I managed to squeeze in one last search in and stumbled across this article. Already at the point of giving up, I tried my best to read the article without skipping 10 lines at a time or looking for pretty pictures. In the section titled Assigning IDs to Dynamically Created Controls, I read these magical and most joyful words:
If you view the source HTML before you click the not-working button and after you have clicked it, you will notice a small difference. The buttons have different HTML IDs before and after the post-back. I got ctl04 and ctl05 before the post-back and ctl02 and ctl03 after the post-back.
ASP.NET button recognizes events by checking for a value for its ID in the Request.Form collection. (In truth it happens differently and controls do not check Request.Form collection by themselves. Page passes post data to controls by their IDs and to controls that are registered to be notified about post data). ASP.NET does not fire the Click event, because the button's ID has changed between the post-backs. The button you have clicked and the button you see after are different buttons for ASP.NET.
Sure enough, when I viewed the HTML the first time, my button had the ID ctl04$ctl36. After clicking the button, my button had the ID ctl04$ctl33.
So there you have it! All I had to do was set the ID on the buttons and presto! My event handlers are now being called!
Sample Solution:
public override Control GetAdministrationInterface()
{
// more code...
Button btn = new Button();
btn.Text = "Click Me!";
// !!THE BANE OF MY EXISTENCE!!
btn.ID = "The_Bane_of_My_Existence";
// !!THE BANE OF MY EXISTENCE!!
btn.Click += new EventHandler(Btn_Click);
// more code...
}
What a great way to spend two days...
I had the same problem, but the accepted answer here was not causing it. I had a text box and a search button, and clicking the button the first time didn't perform the search. The event handler of the button wasn't being hit. But clicking the button a second time did trigger the event on the server. Here is why:
If you have an <asp:Textbox> with its AutoPostBack set to true, after typing in the text box and then moving to click a button, the text box causes a post-back immediately the moment it loses focus. So the click even of the button doesn't count (the page is already posted-back as a result of the text box's event). That's why when you click the button a second time, it works because the text box is not involved in the second post-back.
Set the AutoPostBackproperty of the <asp:Textbox> to false to fix this issue.
A quick fix is to set an ID to the ASCX control your are loading on a page. For example, if your code is like this:
UserControl SpecsControl = (UserControl)Page.LoadControl("../name.ascx");
SpecsContainer.Controls.Add(SpecsControl);
then you need to add a line (before Controls.Add):
SpecsControl.ID = "Aribtrary_Name";
Then your handler method is fired at the first click.
I was facing the same problem. My button froze after my first click. For me this annoying problem got solved when I disabled the button's EnableViewState attribute.
For me it was the UpdatePanel , my Button and my TextBox were both inside an UpdatePanel , so when I post-back , it caused some weird behavior . It took it outside of the UpdatePanel and that fixed it .
Even i had the same problem. the cause was "localhost:1656/secure/login.aspx?ReturnUrl=%2f".
if the request contain %2f as query string, the first post will not be succeeded even though "%2f" is representing "/".
one way to avoid this by having a condition check in pageload
protected void Page_Load(object sender, EventArgs e)
{
string queryString = Request.QueryString.ToString();
if(queryString == "ReturnUrl=%2f")
{
Response.Redirect("/secure/login.aspx");
}
}
Whilst its hard to know exactly without seeing the full Page_load method it does smell a little bit like the event handlers are not hooking up until the page is reloaded.
eg:
if (IsPostBack) {
// Add handlers here ...
}
I had same problem. And I searched on internet i didnt find a solution. After that i found sample code and I used it. It worked for me. Web site link is below:
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/