How to have a textbox click event in asp.net with c# - c#

if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt2OnClick")
{
txt2_Click();
}
txt2.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt2, "txt2OnClick")); //within page load
private void txt2_Click()
{
ImageMap1.ImageUrl = "guide/2.jpg";
}
This is a perfect piece of code to have a click event on textbox, with asp.net (C#).
But the only problem is, we cant type when we apply this code to a textbox. So what I did was set the focus txt2.Focus(); then I can type, but the textbox is not getting validated (I have added a regular expression validator) . Any help? Even to have a better onClick event for a textbox than this?

I don't see any point in having click event for text box.. Remove your 'perfect piece of code' and txt2 if not required..If you have added regular expression validator correctly, it will take care of validation..
EDIT:
For changing some text or showing some image, you don't need to post back to server..
You can either use javascript Focus event or jquery to do that..
In the link you provided, all those photos and texts are placed on divs and are already loaded on browser and when textbox gets focus, those div styles are just toggled between none and block..
Something like this(jsFiddle)..

Related

xamarin forms - changing button text on all carousel content pages

What would be the "best" (or a better) way to do the following?
In a carousel page (say 6 content pages), I click a button, part of the action changes the text on that button, but it also has to change for all the other content pages.
I currently have this happening in the carousel pages OnCurrentPageChanged(), where I call a function and pass in "this". helpers.ChangeAll(this);
public void ChangeAll(CarouselSwipePage page)
{
foreach (SwipePageContent v in page.Children)
{
Button b = v.Content.FindByName<Button>("pause");
if (GlobalSettings.Settings.Default.CarouselCountEnabled) //this is set elsewhere and is used to determine whether the carousel is changing automatically, if it is then set the text to pause
{
b.Text = FontAwesomeFont.PauseCircleO;
}
else
{
b.Text = FontAwesomeFont.PlayCircleO;
}
}
}
This works ok on android but on ios when the user swipes to the next content page after clicking the button, the button text is momentarily the old value before changing to the new value, due to the function being called OnCurrentPageChanged().
Apart from that I'm sure there must be a better way to do this, it looks rubbish.
What about creating a style for the buttons, and just change the text value of the style?
So, using binding properties or dynamic resources, when you change the value, it is going to change all the buttons of your application that use this style. I think this approach is pretty much better and simpler than a loop.

How do I register a script with ScriptManger on partial postback?

I have a RadGrid within a RadAjaxPanel that has a View button that displays a user control in a jQuery popup, also within a RadAjaxPanel, that displays details of the grid record with a delete button. Clicking the delete button causes a partial postback that causes the record to be deleted and the grid to be rebound, removing the deleted record from it.
What I then need to do is run some client script to close the popup. I have tried:
private void RiskEditor_DeleteClick( object sender, EventArgs e )
{
this.grdRiskAnalysis.Rebind();
ScriptManager.RegisterStartupScript(this.RadAjaxPanelRiskEditor,
this.RadAjaxPanelRiskEditor.GetType(),
"closepopup",
"delayClosePopup($j(this).closest('.ui-dialog'), 1000);",
true);
}
In this example, RadAjaxPanelRiskEditor is the AjaxPanel that the User Control is in, but I have also tried registering the script with the panel that the grid is in. Neither works.
Can someone explain where I am going wrong and how to achieve this?
Thanks
Stewart
There are a couple of things I will do:
I would use ScriptManager.RegisterClientScriptBlock, since the RegisterStartupScript, according to msdn: "Registers a startup script block for every asynchronous postback".
If that does not work, there are some client js from ms: http://msdn.microsoft.com/en-us/library/bb397536 and you can use: http://msdn.microsoft.com/en-us/library/bb383810.aspx - Sys.WebForms.PageRequestManager endRequest, that will execute a specific js code, so maybe you can put your close dialog code there, with some conditional logic.

Disable Editing WebBrowser in C#

So I've been working on this project, and we have a WebBrowser object on the form. The purpose of the object is that it loads in HTML Forms into it to be viewed, at this current point in time though, you are able to edit the contents of the HTML form, which is not desired.
I want to simply display this HTML form of information to the user, but not allow them to alter the textboxes or checkboxes or anything of that nature on the form.
I tried using the Navigating event and set e.cancel = true;. This haulted the control from even loading the page. And if I set it to only execute e.cancel = true; after the form had loaded, I could still change text boxes and such on the form, as it only seemed to randomly called the Navigating event.
Does anyone know of a way to get a WebBrowser object to be read only?
Cheers!
You can apply contentEditable attribute to the Body tag of the document.
Document.Body.SetAttribute("contentEditable", false);
This will make your document readonly for user.
You could try accessing all form elements on the page and set the readonly attribute on the tag. Something like:
var inputs = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement element in inputs)
{
element.SetAttribute("readonly", "readonly");
}
You'd obviously have to repeat the process for all form elements (select etc.), but it should work.
I have been running into this issue as well. Thanks to steavy I have been able to come up with a solution :
Hook up to the DocumentCompleted event (you can do this in the designer) :
myWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_procedure_DocumentCompleted);
Then make it readonly in the event :
private void myWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
myWebBrowser.Document.Body.SetAttribute("contentEditable", "false");
}
I do this in the event when the document is fully loaded because I sometimes ran into a NullReferenceException, the body wasn't loaded yet and the line would throw.

dynamic controls and events issue with post back

here's my page's life story
first: there's a button (linkButton if that matters) and its onClick action-1 (event set in Page_Load)
that action makes a panel get visible
the now-visible panel contains not-Dynamic controls ; a button and its onclick action-2
action-2 adds the dynamic controls (table&row&cell ,radioButtonList )
what I wanted to do is to get the selected item of each radioButtonList that has been generated !!
and here's what I tried doing :
tryout-1 {radioButtonList1.SelectedIndexChanged += new EventHandler (function);} ->failed , I don't know why event won't fire!!!
tryout-2 {
foreach (Control x in radioButtonList1.Controls)
{RadioButton one = (RadioButton)x;
one.CheckedChanged += new Eventhandler(function);} -> failed .. won't fire O.o
}
tryout-3 I added a button(plus onClick event) along with each radioButtonList so I can get the selected item when the button is clicked .. but it failed as well ; when the button is clicked the dynamically created controls are gone
I know there's "IsPostBack"thing , but as far as I know it should be in Page_Load|Init and my controls are generated in an event action !! am I mistaken??
and now after more than 2 days of work I'm still in square 1 , and I ran out of games to play around this
any idea what to do ?!
PS : I did the story-like post cuz I don't know what code to post .. it's mostly objects declaration !! so, if u need a particular piece of code let me know and I'll post it
thank you
You must set PostBack to true for radio button.

JQuery Auto-Complete With TextChanged / OnBlur PostBack

I'm having a pretty nasty problem with an auto-completing textbox. I want to initiate an asynchronous PostBack whenever a user selects an item from the auto-completed field and retain the value of the item, rather than the text inputted. This works perfectly when enter is pressed rather than a mouse click.
An example of my issue:
Someone goes to the page, and types 1000 into the textbox. The autocomplete displays 10002, 1000B, and 10000. The user clicks on 1000B and an asynchronous PostBack initiates. Instead of 1000B, the TextBox.Text value is still 1000.
My assumption is that the textbox is initiating the PostBack before the value is actually getting assigned to it. I'm just curious if anyone has any possible solutions for what I'm talking about.
I fixed it in this manner:
As per another question on the site I added an autoPostBack parameter to the options list.
In bottom of the SelectCurrent() function, I added these lines.
if (options.autoPostBackSelection == true) {
__doPostBack($input.id, "");
}
Then, my blur function looked like this:
.blur(function() {
hasFocus = 0;
if (!config.mouseDownOnSelect) {
hideResults();
}
if (options.autoPostBackSelection == true) {
selectCurrent();
}
I actually struggled with this for a bit, my Javascript/DOM event skills aren't very good. Hopefully this helps someone.

Categories