Get text box value on .cs page in asp.net - c#

I want to develop an application in asp.net with C#.
In that application there are one text box and one label.
I want the following functionality:
When I press any key in textbox then I should get this value on .cs page i.e; code behind. And from .cs page I want to add this value to the label.
The problem is that there is no keypress event for an asp textbox and if I take a html text box then I don't get its value on .cs page
How can I come out with this problem?

Because a keypress on a textbox is a client side event but you want to perform server-side processing you will need to use AJAX requests.
You may find the following useful:
AJAX Toolit
Using Jquery to call asp.net page methods

In asp.net the TextBox will have TextChanged event but you will need to enable post back for the button and the event will fire when you tab out of the TextBox.
For the task you want either use javascript or add a button and when this button is do what you want.

I don't think this is a good aproach in web app., In this way you will end with a lot of post-backs.
However, if you still want this functionality. Texbox has TextChanges event, and if you also change the textboxs's AutoPostBack property to true you will get something close, but you will still have to move a currsor.
But it is still a terible solution. Why don't you simply use a button that fires click event instead?
Alternative solution is to use Ajax or javaScript,..

You can simply create a JavaScript-Method for this.
Your Textbox:
<asp:TextBox ID="textBox" runat="server" onkeydown="onFilterTextChanged()">
</asp:TextBox>
Your JavaScript, do a TimeOut to not do this every 0,0001 secs.
function onFilterTextChanged() {
if (timeoutID)
window.clearTimeout(timeoutID);
timeoutID = window.setTimeout(updateFilterText, 600);
}
Send the Values to the CodeBehind, textis your TextBox-Text.
function updateFilterText() {
var text = document.getElementById("<%=textBox.ClientID %>").value;
__doPostBack("<%=textBox.ClientID%>", "CommandArg" + text);
}
You won't need to do as many PostBacks as with the native TextChanged-Event and you can simply use this e.g. for Auto-Extender-Plugins. Pack the TextBox into an UpdatePanel and you're good to go!
Unless of course you do not NEED to go back to the server, in which case just set the labeltext in updateFilterText.

Related

Read asp.net link button text from server side

net page with two controls
linkbutton
button
While running my application, I'm changing my link button text using javascript function.
Now I want to read that text when I press button. Button event is there in server side.
When I try to read like below
string s = linkButton.Text;
It is not giving my updated text.
How can I get it?
At first, declare this HiddenField in your markup
<asp:HiddenField ID="link" runat="server" />
Then in the function, you change the link button text, you should add the following code, in order the new text to the HiddenField been added.
document.getElementById(<%=link.ClientID%>).setAttribute("Value",newText);
Last, in your server side code you can get the value you want with the following way:
string s = link.Value;
You can use a HiddenField.
The LinkButton does not implement IPostBackDataHandler, therefore it doesn't load postback data.
You can write the HiddenField.Value on client- and read it on serverside.
Here's a tutorial-video: [How Do I:] Use a Hidden Field to Store and Manipulate Client-Side Information

onUploadComplete event of asyncFileUpload

how to display a btn after onUploadComplete event is executed? It's not coming now even I say btn.visible=true inside that event..
I read in one of the thread that this event happens asynchronously so we have to write javascript for it and call onClientUploadCompete.
But do anyone know how to do it withoiut writing javascript? please its urgent thanx in advance!
You've got two options - to execute client-side using JavaScript and AsyncFileUpload's OnClientUploadError and OnClientUploadComplete, or to handle the server-side UploadedComplete or UploadedFileError events fired by your AsyncFileUpload object.
If you choose client side, you can still include your Button as normal and include CSS for it to be display: none, which can be then altered in the JavaScript with something like the following:
$get(<%= AsyncFileUploaderInstanceName.ClientId %>).style.display = "block"
If you choose server side, you'll be able to refer to the .Visible property of whatever controls you like, and can alter them then. However, you'll have to update whatever UpdatePanel the button would be sitting in for the button to be rendered on the page.

Display search results dynamically as typing

I am trying to build a SharePoint 2007 web part in Visual Studio.
This web part should search a sharepoint list and display the results.
What I want to accomplish is to display the results as soon as the user stops typing, so no clicking button involved.
Probably, a combination of text_changed event and onkeydown javascript?
Any thought would be great.
This sharepoint site is "Ajax-enabled", btw.
Thanks
I would suggest using jquery and keyup:
$("input#txtid").keyup(function () {
if (this.value.length < 8)
return false;
$.get("ServiceUrl", { arg: this.value }, function (result) { $("#output").html(result); });
});
The easiest way to take care of the UI part is to use the AjaxToolkit AutoCompleteExtender se MOSS, AJAX and the AutoCompleteExtender then all you have to do is decide how you want the searching inside the web service to work
I approached this by using an UpdatePanel in my webpart. Then I added a Button (more on this later) and a TextBox to the UpdatePanel.
I also have a JavaScript class which handles all of the logic for submitting a query after the user has paused while typing their query. It contains the event handler for the onkeyup event which is attached to the TextBox:
t.Attributes.Add("onkeyup", "javascript:oSearchClass.KeyUpEventHandler(event);");
I used setTimeout and clearTimeout to handle when the class should call a SubmitQuery function.
When SubmitQuery() is called, it makes the TextBox read only (so the user can't type anything while it is querying) and then "clicks" the button using click(). Since you're using a normal Button, you can handle the Button.click event like normal to re-query the list and display results.
If you don't want your user to see the button, you can simply put it inside a span WebControl that is hidden.
Have a look at this sample, it adds 'search as you type' to the standard SharePoint search box.
Automatically add ‘Search As You Type’ to every SharePoint page using Infuser.

page postback in asp.net?

in my application i have the playvideo page where video will play, below that i have the option for sharing the video like adding to favorite ,playlist and sending mail.
when i click on any of the link the page is postbacking and video will start from the first.
i place update panel for link button even though it is not working (video is playing from the first i.e., page is postbacking. can u help me. thank you
Actually, the part of page that is within the UpdatePanel does the postback. Make sure you have only those controls(for instance, your links) inside the UpdatePanel.
Alternatively, you can use multiple UpdatePanels; for instance one for your video and one for the links. In this case note that, when one UpdatePanel gets updated other UpdatePanels also gets updated, which you may not want; so all you have to do then is to mark the UpdateMode property to Conditional and call YourDesiredUpdatePanel.Update() method manually - whenever required.
Btw, updating selected portions of the page also reduces the load on the server
Or you may want to look into using client callbacks instead of a postback. But since client callback uses XMLHTTP, which means Microsoft implementation of AJAX, therefore callbacks are just awesome as long as your are working with IE.
You might want to try taking advantage of Page Methods to do the work you need done server side.
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Also, if you want to prevent a control from posting back, you can add return false to the end of your javascript onclick event on the control.
For example, if you had an asp button you were using you could do this:
<asp:Button ID="myButton" runat="server" OnClientClick="DoThingsInJavascript(); return false;" />
Or if you were just using a standard button you could say:
<input type="button" onclick="DoThingsInJavascript(); return false;" />
I've never really liked the update panel and I have sometimes found it's behaviour awful. Have you thought of trying something like a proper ajax call from Javascript

stop setting onclick event for asp:LinkButton if no OnClick event was explicitly defined

How do I setup a default setting so that if I do not set an OnClick (i.e the asp.net OnClick attribute) explicitly for an asp:LinkButton tag, it will not render an onclick(html attribute for javascript) attribute client side? By default, asp.net adds an onclick='doPostBack....' for the LinkButton.
Case for use:
There is a LinkButton tag on the page. For this page, if the user has one friend, I only want to run client side code if the button is clicked and would not for any reason want to make a post back. If the user has more than one friend I would want a click to trigger a postback.
Solutions that include the following are not helpful:
Using any asp.net Ajaxtoolkit
Dynamically switching the control type (i.e. if friends == 1 use a asp:Hyperlink)
-I want to avoid this because it is not scalable. There might be many cases where I want an asp:Link tag to do a postback or to not do a postback depending on the user context or user attributes
Using OnClientClick (I am using jQuery would like to avoid this)
Solution that would be helpful if possible:
If I could see server side at runtime whether an OnClick event was explicitly set on an asp:LinkButton tag, this would solve my problem, too. any ideas?
How about rather than dynamically switching the controls (as you mentioned is a solution you don't want), you could always use an asp:HyperLink and set the NavigateUrl property to redirect your page back to itself with a query string of some sort indicating what was clicked.
If you don't want the post to happen at all, simply leave the NavigateUrl property blank.
Of course, this will be pretty worthless if the rest of the page is dependent on ViewState and such.
http://forums.asp.net/t/1129106.aspx
This link explains how to see server side at runtime whether an OnClick event was explicitly set using reflection

Categories

Resources