.Net textbox is null when a "submit" link is clicked - asp.net

Good day,
I have a problem in a .NET page where I am using an asp:textbox in combination with an OnClick action on a link button.
What happens is that after text has been entered into the textbox, if you directly click on the link button, more often than not the textbox is considered to be null.
If you click off the text box first then click the link, all is well and the save function performed by the link button proceeds as expected.
My assumption is that there is a lifecycle event that is being missed, or not applied which is not binding the text to the textbox for use in the codebehind when the link button is clicked.
The question is, what can i do to enforce that binding short of doing something like adding an onkeypress event to the textbox to force a postback.
There must be a more elegant solution.
Thank you in advance for your help.

Do you have initializations on your textbox inside Page_Load event? If so, use IsPostBack=false and put the initialization inside.
If IsPostBack =False Then
TextBox1.text=""
End If

Related

On text change event firing in local but not in test server

I have written code in ontextchange and made autopostback=true. When we give text in textbox and click on tab...on text change is firing and giving the alert. But when we give text in textbox and click directly on button, It is not giving any alert and directly saving with empty data...(We have written code to check duplicate values in ontextchange event).
Please help me on this..
If I understood you correctly, It seems your button click event is taking precedence over your onTextChange
I suggest you to disable your button and enable it in OnTextChange event, So that both of your requests will not overlap.

Button in CustomControl added in OnClick doesn't postback until second click

I have the following scenario:
UserControlA contains a <asp:Button id="bSomeid" onClick="AddItem" /> with some code to an item to a shopping basket in AddItem.
UserControlB contains some LinkButton's that dynamically add a selection of UserControlA to the page in the OnClick event.
This is all done in an UpdatePanel. It is a little more complicated but I have pruned the information to what I believe is causing the problem, I will add more information if necessary.
The problem I have is that it takes 2 clicks for the AddItem event to trigger after I have added the items to the page after clicking the LinkButton.
I understand why this is happening - it is to late in the page cycle to register events for the next post back in the onclick - but can anyone think of a way around this? Can I force an event to be triggered on the next postback? I have tried to think of a way to run my code in page_load but I requuire access to the sender in the onClick.
Using .NET 4.0.
EDIT
I managed to find a way to get the link button sending the request in the Page_Load (using Request.Form["__EVENTTARGET"];) so I moved my code to the Page_load event. It still requires 2 clicks so I am assuming it isn't something to do with the onClick being registered to late.
Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?
If your suspicion about being late in page life cycle is true then you can try using ScriptManager.RegisterAsyncPostBackControl method to register dynamically added controls in the link button click - considering that your button is within user control, you need to add public method into UserControlA that would actually register the button bSomeid1 and link button click from UserControlB would actually call the A control's method.
EDIT :
Another cause for button click not happening can be that button being dynamic control is not added in the page hierarchy when post-back happens (or it gets added very late in the page life cycle when the post back data is already processed). A really full-proof solution should add dynamic controls back to the page hierarchy in page_load it-self (and strictly maintaining same controls ids within hierarchy). If that's not possible then you can sniff the request (Request.Form) to detect the post-back.
In your case, you should ascertain if the button is indeed causing the post-back on each click. If yes, what is the POST data (Request.Form) for the first request - what is the __EVENTTARGET value on the first click (and post-back)? That should start your trouble-shooting.
On the other hand, a simple work-around could be to use html anchor element (you can still use link button) and have a javascript handler in the click event that would set some hidden variable and then submit the form (you can simulate the click on hidden button to trigger ASP.NET client side submit pipeline) . Now the hidden variable value can be used on the post-back to determine which link button has been clicked.
"Are there any other general things to check that could cause a button to require 2 clicks to post an event properly?"
Does it require two clicks on the control, or does it take accept a single click elsewhere on the screen, and then fire first time with a single click on the control?
I have my own (similar) issue with the Updatepanel where the first (expected) trigger does not fire and it seems that a single click elsewhere, and then the subsequent triggers fires first time (which totals 2 clicks)
[edit] Since you are working on this ATM, it may help me as well. Do you have a textbox with a trigger event on it? I do, and if I leave this blank (so that it does not fire) then there is no need for a second click.

Can't change ImageButton tooltip or style on GridView fired postback event

Hi I'm having a really strange issue here. I have an ImageButton and Gridview, both of which I create on runtime. The Image button is a seperate control on the webform and is not linked in anyway to the Gridview. What I am trying to achieve is when the user completes the editing of a line in the GridView and clicks on the update row button, I perform a set of calculations on the input entered. If an error is found with the data entered, I then make visible the ImageButton which when clicked displays the error message. The function I am using to make visible or hide the Image button through
picCross.Style.Value = "display: none;"
and
picCross.Style.Value = "display: block;"
gets called on the Page.PreRender function. What I have discovered is that I am able to make changes to the ImageButton (changing the button's tooltip, setting its Style's value ) during a postback not fired by the GridView, such as when the user clicks on a button else where on the web form. If however I try making changes to the Imagebutton during the post back event of the GridView, such as when the user clicks on the edit row or update row button/link. The changes are not saved. What is even more strange is I do not have such an issue with labels. I can change the text of labels regardless on whether the change was made during the postback fired off by the Gridview control.
I have tried setting the ImageButton's EnableViewState to true and false but neither makes a difference. I have tried the same approach with panels and the same thing occurs, I can't change its properties during a postback caused by the gridview. My GridView has EnableViewState = True
P.S I'm binding the GridView on runtime too. Only my GridView's RowUpdating event fires. The RowUpdated event does not fire. I was hoping to try changing the ImageButton on RowUpdated, not that I think it would have made much of a difference anyway.
You should create the dynamic controls (Gridview and ImageButtons) in the PreInit event. Init event if you have a Master Page.

Change value and text of button

I have a textbox and a button with an onClick event connected to it, and when the button is clicked once I want to change the text of the button and clear the textbox. How can i accomplish this?
If you want to do this on a postback (a complete refresh of the page), simply put code in your button click handler to set these values. For example, textbox.Text = string.Empty;
If you want to set it on the client side (without refreshing the page), then you'd need to use client script such as JavaScript.
This needs you to know about TextBox and its properties. Normally the value of a textbox you use its Text property as TextBox1.Text. In your case you put this statement in the button's click event
string txtValue=TextBox1.Text;
TextBox1.Text=string.Empty;
To see how to use TextBox and its properties. This can help

How to Not Submit a Form Using AJAXToolkit AutoComplete Extender?

I have used AJAXToolkit AutoComplete extender in my project.It works fines.But the issue is with the form of the page.
when i type in the AutoComplete, i get list of suggestions.When i click on the page other than the Autocomplete, the form gets submitted.
any suggestions how to stop submission of entire form whenever i click on the page?I use .net 2.0
Does your TextBox have AutoPostBack="True" set? If so, it will automatically postback when you're "finished" (when the input control loses focus). If this is the case, just set it to false.
yeah,My TextBox AutoPostBack was set to true which i have changed it to false, now.
but i have a url attached to each of my suggestions.On click of suggestion, user gets navigated to that particular url. When i do AutoPostback= "false", i wont get navigated to that page.
HOw do i handle it?
I think it would be as simple as adding a required validator to your element to see if the user selected anything.
Eric
Use jquery to add an event handler that detects clicks. When a click occurs, stop the browser. Let me know if you need code.

Resources