I am using an ASP TextBox and a Telerik RadTextBox in my asp.net webform. I know the The both the controls clear its value after each postback and this is by design due to security reasons.
In my scenario both the controls are ajaxified. Still the ASP TextBox and RadTextBox loses it value on ajax request (partial postback).
So my doubt, is this expected? Will the textbox lose value on both full postback as well as on ajax request?
Both ASP.NET Textbox and Telerik Textbox will be rendered as <input type="password" />. <input type="password" /> is treated a little bit different than the other controls as it contains sensitive information. On every postback they are forcefully cleared.
But if there is some requirement then you can store the information and set it back.
string Password = txtPassword.Text;
txtPassword.Attributes.Add("value", Password);
Note: But I really don't recommend it.
Ajax based function calling never causes Data loss or refresh happened in client side. I think it may be happened due to object you have selected for ajax calling. For eg:
<input type="button" id="Submit">
<input type="submit" id="Submit">
They exhibit different behavior in ajax calling.
Related
I have a C# MVC application and a <form> in my page.cshtml file. In that form I have <input type="text" ... /> elements. If I submit this form I only get the values in Response.Params or Response.Form from the inputs where I changed the value manually (i.e. Entered the text box then typed something).
If I change the value with jQuery, $('#myInput').val('some value'); this does not count as a change in the input's value and I do not get myInput's value when I submit the form.
Is there any way to make sure all inputs are submitted? If not then is there a good workaround for this, maybe in some event that occurs before my model gets bound? I need to know all the input values from the form when submitted whether they changed or not.
Some additional info:
The form and other values are getting submitted correctly and I am receiving my model when the POST action is called in my controller.
The real issue is when my model is being bound. It is being created and bound with all values except the one not being submitted because it is not in the Request.Params collection.
I have only ever seen this behaviour when a field is disabled. Due to this, I commonly have a javascript function that handles the form submission and re-enables them on submit, this way the correct values get sent to the server.
Something like this does the trick for me (NOTE: I am using JQuery):
$(document).ready() {
$("#ButtonSubmit").click(SubmitForm);
}
function SubmitForm(e) {
e.preventDefault();
//ensure fields are enabled, this example does text and checkbox types
$("[type='text']").attr("disabled", false);
$("[type='checkbox']").attr("disabled", false);
//submit the form
document.forms[0].submit();
}
I am unaware of any easier way to do this, it would be nice if you could 'flag' something that instructs all fields to be submitted. But I don't know if this exists, maybe somebody else can offer a better solution.
EDIT: It appears that disabled fields not submitting is just the nature of HTML, and is not something that is tied to MVC.
It seems that if you make the fields readonly instead of disabled then the values will still submit. However, with this approach you lose the 'disabled' styling. The exception to this rule is select control, it seems this will not submit under readonly either. More information on this can be in this question
Try using the razor helper to build the form tag.
#using(Html.BeginForm()) {
..
// make sure this is a submit button
<input type="submit" value="Save" />
}
In your controller action post method make sure you decorate it [HttpPost].
e.g.,
[HttpPost]
public ActionResult Edit(YourModel model) {
}
I am working on an WebForms app, and we want to put a speed bump, for lack of a better term, in for the users at a particular point in the app. There is a particular action which is not supposed to be undone. So when they click the button to do that action we want to display a small confirmation window, have them enter a random string that we give them. Once they enter that string, and it matches the corresponding label, the submit button becomes enablesand they can perform the action. But for the life of me I can't figure out a good way to do this client side with WebForms. Is there a simple mechanism to use this type of workflow without a ton of post back events?
Note: This is an internal app where high security isn't truly a necessary requirement in this case. As I said, this is meant to be something to slow the user down slightly.
This is similar to the mint.com confimration style.
Add JavaScript to the textbox onChange or onKeyUp event, there do your check and enable the button.
For example:
<script type="text/javascript">
function checkConfirmationText(){
// Check if value of entered text = value of hidden text
var isOk = document.getElementById('confirmation-label').value == document.getElementById('confirmation-text').value);
// Show/Hide button depending on the text
document.getElementById('btn-submit').style.dispaly = isOk ? '' : 'none';
}
</script>
<!-- HTML -->
<input type="hidden" id="confirmation-label" value="DELETE" />
Enter "DELETE": <input type="text" id="confirmation-text" value="" onkeyup="checkConfirmationText()" />
<input type="submit" id="btn-submit" style="display:none" />
You could generate the javascript method to check the code (including the code in the method) server side and use registerclientscriptblock (or whatever the current method is), then use the onblur to call that method.
I have an asp button that produces this html:
<input type="submit" name="ctl00$m$g_a2ba5666_c8e9_4bd7_a44a_f9407dbe2199$ctl00$btnAddWebPart" value="Add Report" id="ctl00_m_g_a2ba5666_c8e9_4bd7_a44a_f9407dbe2199_ctl00_btnAddWebPart" />
When the button is submitted and the page_load method is hit, I am trying to do this:
String target = Page.Request.Params.Get("__EVENTTARGET");
but, for some reason 'target' is empty. I checked to see if __EVENTTARGET is getting populated and it is an empty string. Any ideas as to why this is happening? It is something really silly.
Thanks.
Wrap this button up in an ajaxtoolkit update panel. that way you can update the various page components (add / remove your web parts) within an async call.
This means that the page is partially rendered instead of it being the result of a full postback.
I agree with Josh on this ... handling the event in this way is ugly and against the intended purpose of this part of asp.net from microsoft.
partial postbacks dont result in that ugly flicker effect so this should produce the result you want and not effect the rest of the page.
I have a web form and want to 'get' it to another page..
is there anyway to submit it without posting the ViewState and other bits I don't want?
Or should I be catching the submit button click and redirecting with a querystring I build myself.
You have a couple of options here:
Disable ViewState
Use jQuery to remove the fields you don't want to post before the are sent to the server
You don't have to disable ViewState on all pages, just the pages that you do not care for the state to be saved.
But there is also the option to disable the ViewState completely if you never want to use it.
If you just want to compose a GET by yourself, you can use jQuery for that aswell so you only pass the parameters you really want which will give you 100% control of what is posted /getted.
If you are not using the viewstate, why have you kept it enabled? Just disable it. For every server control, set the EnableViewState = False and you are free from it. If you need the viewstate, it will be part of the post all the time.
There are different ways to persist viewstate.
I have had in the past, had to persist viewstate on the server (using ApplicationState/Session, cant remember) for a heavy AJAX page to support faster updates. Works well.
See Page.LoadPageStateFromPersistenceMedium and Page.SavePageStateToPersistenceMedium.
Sorry, no links from Reflector available.
You could add an event handler to your search button and do something similar to this
private void button_Click(object sender, EventArgs e)
{
String query = queryTextBox.Text;
Response.Redirect("SearchResults.aspx?query=" + query);
}
Using JavaScript
function doSearch()
{
// Assuming you are not using jQuery,
// using jQuery it would be $('#queryTextBox').value instead
var queryString = document.getElementById('queryTextBox').value;
window.open("SearchResults.aspx?query=" + queryString);
return false;
}
Html
<input type="text" id="queryTextBox" />
<input type="button" onclick="return doSearch()" value="Go" />
I wanna add server controls by using javascript. The main purpose of why I want is to add controls without any postback and get them in code-behind.
You can check the Request.Form collection for all form values (client side controls) on the server. Each control will need to have a unique ID to access it in the request.Form collection.
For example, if you had the following control
<input type="text" id="testBox" value="blah" />
On the server you would access the value as Request.Form["testBox"].
Try to access the html inputs with Request.Form
Request.Form["inputName"]
You will have to set the name attribute on your inputs like this:
<input type="text" value="blah" name="inputName" />
if its a form post you can get the value with request.form["control"] one of the properties will help you do it,
if its a new control in some page you can do something with ajax , i did not try it, its just a theory,
you can make an ajax request that will create a textboox control in the server and then render the html to your page.
now when you will call text1.text you will get the value ..
but its a bit of an hack to me..
That is not generally possible. You must add server side controls on the server.
If you want to avoid the visibility of a postback, use AJAX and an UpdatePanel. Otherwise you're out of luck.