Change value and text of button - asp.net

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

Related

.Net textbox is null when a "submit" link is clicked

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

ASP.NET AJAX Control Toolkit CalendarExtender Date Change

I have a text box which I have extended with the AJAX Control Toolkit CalendarExtender. When I click on the text box, a calendar appears and I can select a date which then is added to the text box. So far so good.
This text box is used on a Grid View to filter the results in it. This was setup when I added a data source to the grid view.
This works fine other than the fact that after selecting the date in the date control, I then also have to hit enter in the text box for the grid view to update. Can I get to update as soon as the date is selected rather than having to press enter?
This is because the TextBox_TextChanged event is not being raised. This can only be raised when focus is taken off the textbox, and since focus was put on it, the text has changed.
One option would be to use jQuery to force a postback whenever text is changed in the textbox.
Something like:
$("input.textbox").change(function(){
__doPostBack();
});
This article may be of use for forcing Post Back from javascript:
http://weblogs.asp.net/yousefjadallah/archive/2010/06/27/insure-that-dopostback-function-implemented-on-the-page.aspx
If you want to refresh your grid without pressing the Enter key,
put your textbox's autopostback property to true.
Hope this helps.

Setting default button in asp.net 2.0

I have 3rd party user control (a captcha control), which has a captcha image, a text box within it.
I am using the above user control in my webpage. I have a 3 submit buttons on my webpage (Validate Captcha, Submit Page, Add User). When I click the Validate Captcha submit button using the mouse, I am validating whether captcha is empty and showing a javascript alert.
The problem comes when I enter the valid captcha text in the textbox and hit enter key when the cursor is in the textbox. The page just refreshes. I am unable to add keypress event to textbox and call Validate Captcha button event as I am using the 3rd party user control which I cannot modify.
Also, Page.ClientScript.RegisterHiddenField(...) will not work in my case as I have two other submit button inside the same page.
Only option left is to enclose these in panels and set default button.
Please let me know if anyone has any better options for achieving this.
Greetings! I too use alot of third party controls. The thing to remember about these controls, it that in the end they just emit HTML. This means you can use the DOM to access and attach event handlers such as onKeyPress. The trick is to identify how your control creator named the control you are looking for, in this case a {textbox}. The easiest way to achieve this is to simply run the page and view the page source. It is there that you can find the name as it is rendered and sent to the browser, after that all you have to do us use document.getElementByID to get the object and setup your handler
Example:
<script>
//Place this AFTER your textbox control is declared in the HTML
//Get the textbox
var textbox = document.getElementById('nameOfRenderedControlHere');
//Assign the event handler and function you want it to call
textbox.onclick = function() { validateCaptcha(); };
function validateCaptcha()
{ //Do your Stuff here }
</script>
That should be it..havent tested, let me knwo if you run into questions.
Put the captcha in its own <asp:Panel> and add a DefaultButton property for the panel with the ID of the captcha's submit button.

Is it possible to fire off another button's onclientclick event?

I have a modal popup that has a targetId to a hidden button. I want the popup to occur when a button in a grid is clicked but that button is programmed behind the code and therefore the targetId would be invalid...
So I wanted to attempt to set the gridview's button's onclientclick event to be the onclientclickevent of that hidden button. Is this possible or should I be going about this another way.
here is how i created the grid button
If Not IsPostBack Then
Dim field As New TemplateField
field.HeaderText = "Sub Departments"
Dim col As DataControlField = field
GridView1.Columns.Add(col)
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim btnview As New ImageButton
btnview.ImageUrl = "\images\icons\xp_ico_search_24x24.gif"
GridView1.Rows(i).Cells(3).Controls.Add(btnview)
Next
End If
I am assuming you are using web forms. If so then yes, it it very possible.
Do the following.
Create a javascript function on the page
function openModal(btnId){
btn = document.getElementById(btnId);
btn.click(); // this should fire the click even of the button
}
on the grid button add the onclientclick event:
gridButton.OnClientClick = String.Format("openModal('{0}');", modalButton.ClientId))
This will set the client Id of the button that trigers the modal window into the javascript function. If you need to populate the modal window with other data, you should do it in this function as well.
Are you using the ASP.Net AJAX Control Toolkit? Or something else? This assumes the toolkit.
Also, you have set the visibility of the button to hidden, but do not the the Visible=False property on the server side, as this will not render the button. To hide it you will need to use the client side property style="display:none"
This link may help: http://forums.asp.net/t/1066506.aspx
It is possible doing that, it requires you to fire off the buttons "DoPostback" function.
Try looking into the Page.ClientScript.GetPostBackClientHyperlink method.
Alternatively you can use javascript and the .click() event, but I think that limits you to IE as a browser, not sure on that though.
It would look like this
javascript:document.getElementById('clientSideID').click();

Is there a way to set a asp.net button's CommandArgument in javascript?

I have a GridView that lists a bunch of items and one of the columns has a link that displays a modal (AjaxToolkit ModalPopupExtender). Let's call that link "Show". In that modal, I have a asp:button for saving the data entered in that modal. Let's call that button "Save"
So when the user clicks on a "Show" link in a certain row, I'd like write some javascript that sets something in the "Save" button, so that in my code-behind, I can handle "Save".Command and use the CommandEventArgs parameter to get the value.
Is this possible, or do I just need to use a hidden input tag and set its value?
Not a direct answer to your question, but another possible way of solving the problem:
Place a HiddenField control on the page. In your code-behind, before displaying the modal popup, set the value of that control to the ID of the row that was clicked (or the row number, or some identifying value). Then in the code-behind of your Save button, you can just read the value of the HiddenField.
Well, after continuing the research, it looks like it cannot be done. The CommandArgument property might reside in the ViewState, but for this case, it is completely server side and cannot be changed using javascript.
If you are using Updatepanel, you need to place the Hiddenfield inside the Updatepanel. Otherwise you will not be able to get/set the value stored in hiddenfield.

Resources