Binding data to TextBox without showing text - asp.net

When binding text/data to a TextBox, it is done like
#Html.TextAreaFor(model=> model.Question)
but, when the PostBack happens, the text shows up in the TextBox. Is there a way to still bind the data to the textbox, but not display the text in the textbox?
I have a search functionality on my website, and when someone searches I want the results page to have the textbox without the text (the searched word).

You've said Postback, so you're using Server side implementation, right?
If that's the case, the simplest way is to add a script that runs every page load.
For example, you have this search textbox:
<input type="textbox" id="search">
In your javascript :
<script type="text/javacript">
$(document).ready(function{
//get the attribute of the search textbox and clear the text
$('#search').val('');
});
</script>
If you're using Ajax implementation then just add a script when your form post succeeds.

You should set the Question property to null or empty on the controller action before displaying the result, or also you make it empty after the page is loaded in the document ready method using jquery.

Related

Get element text before page reloads in ASP.net web forms

I am unfortunately having to work with asp.net web forms. I have a label that has a different Text property every time the page loads. I have a button that is clicked. I have double clicked on the button and it has shown me a code view.
I get a reference to the label via labelID.Text, but it refers to the value of the text that is about to be displayed on the next page load. How would I get the text of the value when the button was actually clicked? Or is web forms not advanced enough for that.
Search where the labelID.Text is modified (maybe Page_Load event), and save the text before in a global variable.
It sounds like on every page load or postback, somewhere there is code which applies a new value to labelID.Text. Where is that work being done? Page_Load?
In any case, wherever that work is being done, you most likely have access to both the existing text value of the control, and the new text value you're about to give it.

How to use Textchange property of ASP Textbox

I have an asp:textbox. On textchange of this textbox, I'm doing validation for the text entered. If the text entered is incorrect, I want to flash a message of incorrect text entered. Please re-enter. How can I do this in ASP?
Just use a RegularExpressionValidator and keep client validation property at it's default "true" value. The control will handle this behavior for you.
Here's an example in action with the code: http://www.w3schools.com/ASPNET/showasp.asp?filename=demo_regularexpvalidator
You should avoid using that property if you can. Your validation code will only run when you postback to the server, and you don't want to do a full postback every time the text changes unless you really have to. Instead, use javascript to handle the onchange event of the input element rendered in the raw html by asp.net. Then remember to duplicate your validation code on the server.

Ordering javascript code on a page executed with argument calculated in codebehind in ASP.NET

A situation requires me to order javascript code on a page be executed with an argument calculated in codebehind in ASP.NET
Here is the situation:
I have a page called search.aspx . This page contains a button and a textbox. Users put their arguments for search in the textbox and then click the button. The button posts back and runs a button click method. This code behind logic (running on the server) serializes and inserts the contents of the textbox to a DB.
Assuming a rowID or something to identify the serialized query by will be returned inside the button click method, how can I then tell the page (search.aspx) to open a new tab with results.aspx?query=.
I know I have to use javascript as the code behind can't open a new tab, but I am just wondering how to do so.
I've never used JS so a maximum amount of details in the answer is better.
Assuming your rowID is loaded in a variable named, well, rowID, then put this at the end of your click event (VB.Net):
ClientScript.RegisterStartupScript(me.GetType(), "results", _
string.Format("window.open('results.aspx?query={0}','Search Results','status=1')", rowID), True )
To directly answer your question, you can insert something like the following directly in the page output just before the body end tag.
<script type="text/javascript">
window.open("http://servername/pagename.aspx?queryid=blah");
</script>
by using Response.Write.
However, did you also consider simply opening a new window on the button click with the button's text passed in as a query string to an aspx page?

Asp.net & JQuery: Showing a Dialog Window when a Form fails validation

I want to show a modal dialog/pop-up window when the user has not filled in a text field on the page, instead of using an error text field on the page.
If the required text field is filled in, I want the submit button to work as normal (aka call the onClick function), but if it's not filled in, I want a dialogue window/modal pop-up to show up stating that they need to fill in the specific field.
What's the best way of this?
I personally prefer using the jquery ui library over the ajax control toolkit modal popup if possible.
All that you have to do is to do the following.
Create a JS function that does the validation, display the dialog if it errors. Have that function return true/false if validation is ok or fails.
On your button, add an "onClick" method to the attributes. "javascript: return YourFunction();" and you should be set to go.
The return value of your method will prevent postback if returning false!

Retain anchor after postback in asp.net

I'm using a coda slider like consctuct on one of my pages. Naturally, the anchor ("#currentTab") information is lost after a postback. This is annoying because when you press a button on a certain tab, you always end up on the first tab after the postback.
What is the best way of letting this information survive a postback?
Try this is your page_load event
Me.Form.Attributes("onsubmit") = "this.action+=top.location.hash;"
I'm not doing in ASP, but as a general solution attempt this might work for you too:
create a hidden field within the form that you send
on tab change, fill in the value for this selected tab (if there are no values for identifying the tabs, create them)
when rendering the page after submit, create a piece of javascript on the fly, which switches the tab on DOM:ready.
Either execute your postback as an AJAX request, or add some javascript to the form that will send the anchor value to the server
Rough example
<form onsubmit="this.anchor.value=top.location.hash">
<input name="anchor" type="hidden" value="">
<!-- rest of form -->
</form>
Then you'll need a convention to return it to the client and perform the appropriate action.

Resources