getting client id of a control from variable - asp.net

I want the client id of an asp.net textbox control(txtTest) in javascript.But the problem here is the control id comes from a variable as shown below
var testName = 'txtTest';
var testCntrl = document.getElementById('<%=' + testName + '.ClientID %>');
But its throwing
CS1012: Too many characters in
character literal
Can any one please help....

Try this:
document.getElementById('<%=txtTest.ClientID %>');
Or more along the lines of your original example:
var testName = '<%=txtTest.ClientID %>';
var testCntrl = document.getElementById(testName);
It appears from your example that you have managed to confuse yourself over what is server side and what is client side code.

<%= aspControlID.ClientID %> is a server side control, but you are trying to pass a clientside variable name to it. By the time testName is set equal to 'txtTest' its too late, you're already on the client.
There are a number of alternatives to get the server side ClientIDs as Rick Stahl discusses.
1) You can pre-load all the control IDs that you know you're going to need like this, they query them (he uses jquery) when you need their elements.
var ids = {
txtSymbol: "#<%= txtSymbol.ClientID %>",
PageContent: "#<%= PageContainer.ClientID %>"
}
This can also be written:
var txtSymbol = document.getElementById('<%= txtSymbol.ClientID %>');
var txtBlah = document.getElementById('<%= txtBlah.ClientID %>');
2) Or, he wrote a function that will get a control for you from the clientside
function $$(id, context) {
var el = $("#" + id, context);
if (el.length < 1)
el = $("[id$=_" + id + "]", context);
return el;
}
Be aware that there are some serious caveats. This relies on JQuery, so be sure to include that library and use it like this $$('myASPControlID').val('new val'); The catch is that if you have any controls that create other controls, like listviews, repeaters, gridviews etc. Then finding a single instance of a child control will take some strategy. In that situation, this tool will only get the first instance of a repeated control.
Still, the function provides a way to solve this problem by allowing you to specify a containing element in the second field.
EDIT
Hey L G, if you really need to pass your variable from the client side, then just add the second function and a link to the JQuery library. Then you can get your control with this simple code:
var testName = 'txtTest';
var testCntrl = $$(testName);

If it is C# code (which I assume, given the compilation error), you need to surround strings with " instead of '.
' is used for char values, that can contain only one character (or two if it is an escaped one, such as '\n').
But I don't really get the connection between the compilation error and the code, since the C# compiler should not bother about the javascript code...

Related

Getting QueryString for Model in ASP.NET MVC4

I have a simple form based on a model.
One of the features I have to support is the ability to create a url of the current paraments of the model. For example:
Form Begin
Model.Name
Model.Age
Form End
Then in javascript some how get:
http://Site/Controller/Action?Name=Bill&Age=45
Is there a helper that will take the current state and gen a Url?
The reason is that I need to be able to open a new window with the values of the model while leaving the form and page as is.
You should be able to do something like this in your javascript
var url = "#Html.Action("Action", "Controller", new { Name = Model.Name, Age = Model.Age})";
window.open(url);
Use window.location and then window.open to open it in a new window/page/tab.
For example:
var myURL = window.location.href; and then use window.open(myURL)
AFAIK, there isn't a way to get what you are asking (nice idea though). I upvoted NKD because of the second part of your question which was on how to open the browser in a new window (perhaps you already knew how to do this).
you would need to use the JQuery/Javascript way of obtaining the values you desire. Alternatively you could make a quick client helper where you give it the key name you are after, it would find it and give it back to you i.e:
function GetValueFromForm(key)
{
return $('#' + key).val();
}
then you call it:
var url = '/SomeAction?name=' + GetValueFromForm('Name') + '&age=' + GetValueFromForm('Age');
Alternatively, if the data is NOT changing then just use the Model values in your javascript:
var url = '/SomeAction?name=' + #Model.Name + '&age=' + #Model.Age;

How to pass javascript value in Razor syntax(as a parameter value)

i am using jquery grid in MVC 3 .as a formatter option i am using following code as a script segment.
function viewformateadorLink(cellvalue, options, rowObject) {
return "" + cellvalue + "";
}
but i don't want to use href here .better option i have found is as follows:
func....(){
var url = '#Html.ActionLink( "_name_", "Edit", new { id = "_id_" })';
url = url.replace(/_name_/, cellvalue);
url = url.replace(/_id_/, rowObject[0]);
return url;
}
are there any simple way exist for passing parameter?
If the value you want to pass to the link is in a JavaScript variable and you don't want to use href, then I'm afraid you can't go further than you already have gone and this is why:
The JavaScript code is ALWAYS executed after the server-side code.
I imagine you would like to do something like:
#Html.ActionLink( "_name_", "Edit", new { id = *JAVASCRIPT VARIABLE* })
but due to the reason I mentioned above, such thing is impossible.
I would suggest you stick to any of the other methods you used in your question.

ASP.Net Auto-populate field based on other fields

I've just moved to web development and need to know how i can implement below requirement using asp.net and vb.net.
I have three fields in a form which are filled by users. Based on these three values, i need to auto-populate the 4th field. I have planned to implement this in the following way
Write a separate class file with a function to calculate the possible values for the 4th fields based on 1st 3 inputs. This function can return some where between 1-10 values. So I've decided to use drop-down for 4th field, and allow users to select the appropriate value.
Call the above function in onchange function of 3rd field and take and use the return values to populate the 4th field. I'm planning to get the return values in array field.(Does this need a post back?)
Please let me know how if there is better way to implement this.
Thanks.
You may want to consider doing this with Javascript. You could read and control the fields pretty easily with pure Javascript, or using a nice library like jQuery (my favorite). If you did it this way, no post-back would be required and the 4th field would update immediately. (Nice for your users)
You can also do it with ASP.NET for the most part. "onchange" in ASP.NET still requires Javascript as far as I know, it just does some of it for you. A post-back will definitely happen when you change something.
You need javascript or to set autopostback=true on your form elements.
From a user perspective the best thing is to use javascript to populate the field for display, BUT when the form is submitted use your backend function to validate it. This will make sure the user didn't change the value.
An easy way is to use jQuery for the UI (that way you don't have to worry about long winded javascript and deal with browser compatibility as it's already taken care of for you) and have it call to the server for the data. For the server, your easiest route is to return JSON for looping values.
Include your jQuery:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
Then add in a handle for the JavaScript:
<script type="text/javascript">
function autoPopulate() {
var value1 = $('#ddl1').val();
var value2 = $('#ddl2').val();
var value3 = $('#ddl3').val();
var url = 'path/to/your/file.aspx?value1=' + value1 + '&value2=' + value2 + '&value3=' + value3;
$.getJSON(url, function(data) {
data == null ? return false : data = eval(data);
var ddl = $('#ddl4')[0];
for (i = 0; i < data.length; i++) {
var option = new Option(data[i][0], data[i][1]);
if ($.browser.msie) {
ddl.add(option);
} else {
ddl.add(option, null);
}
}
}
}
</script>
(Yes, I know I used a native loop but I'm little lazy here today :) )
Now, for your server side code you'll want your code your page to return data in the format of:
[['value1','text1'],['value2','text2'],['value3','value3']]
so something like:
<script type="vb" runat="server">
Private Sub Page_Init()
// get your data
// loop through it and add in values
// ex.
Dim result As String = "[" //start multi-dimensional array
For Each Item As String In data
result += String.Format("['{0}','{1}'],", _value, _text)
Next
result = result.SubString(0, result.Length - 1) // removes trailing comma
result += "]" // closes off m-array
Response.Write(result)
Response.Flush()
End Sub
</script>

Generating an action URL in JavaScript for ASP.NET MVC

I'm trying to redirect to another page by calling an action in controller with a specific parameter. I'm trying to use this line:
window.open('<%= Url.Action("Report", "Survey",
new { id = ' + selectedRow + ' } ) %>');
But I couldn't make it work; it gives the following error:
CS1012: Too many characters in character literal.
Can't I generate the action URL this was on the client side? Or do I have to make an Ajax call by supplying the parameter and get back the needed URL? This doesn't seem right, but I want to if it's the only way.
Is there an easier solution?
Remember that everything between <% and %> is interpreted as C# code, so what you're actually doing is trying to evaluate the following line of C#:
Url.Action("Report", "Survey", new { id = ' + selectedRow + ' } )
C# thinks the single-quotes are surrounding a character literal, hence the error message you're getting (character literals can only contain a single character in C#)
Perhaps you could generate the URL once in your page script - somewhere in your page HEAD, do this:
var actionUrl =
'<%= Url.Action("Report", "Survey", new { id = "PLACEHOLDER" } ) %>';
That'll give you a Javascript string containing the URL you need, but with PLACEHOLDER instead of the number. Then set up your click handlers as:
window.open(actionUrl.replace('PLACEHOLDER', selectedRow));
i.e. when the handler runs, you find the PLACEHOLDER value in your pre-calculated URL, and replace it with the selected row.
I usually declare a javascript variable in the section to hold the root of my website.
<%="<script type=\"text/javascript\">var rootPath = '"
+ Url.Content("~/") + "';</script>" %>
To solve your problem I would simply do
window.open(rootPath + "report/survey/" + selectedRow);
Could you do
window.open('/report/survey/' + selectedRow);
instead where selected row I assume is the id? The routing should pick this up fine.
or use getJSON
Perhaps you could use JSONResult instead. Wherever the window.open should be call a method instead i.e. OpenSurveyWindow(id);
then add some jquery similar to below
function OpenSurveyWindow(id){
$.getJSON("/report/survey/" + id, null, function(data) {
window.open(data);
});
}
now in your controller
public JsonResult Survey(int id)
{
return Json(GetMyUrlMethod(id));
}
That code isnt syntactically perfect but something along those lines should work
Just if someone is still looking for this. The controller action can have normal parameters or a model object with these fields. MVC will bind the valus automatically.
var url = '#Url.Action("Action", "Controller")';
$.post(url, {
YearId: $("#YearId").val(),
LeaveTypeId: $("#LeaveTypeId").val()
}, function (data) {
//Do what u like with result
});
You wont be able to do this, the URL.Action is a server side process so will parse before the clientside selectedRow is available. Israfel has the answer I would suggest.
If selectedRow is a client-side variable, it won't work. You have to use #Israfel implementation to link. This is because the server-side runs before the client-side variable even exists.
If selectedRow is a server-side variable within the view, change to this:
window.open('<%= Url.Action("Report", "Survey", new { id = selectedRow } ) %>');
This is because the id will infer the selectedRow variable type, or you could convert to string with ToString() method (as long as it's not null).
A way to do this that might be considered cleaner involves using the T4MVC T4 templates. You could then write the JavaScript inside your view like this:
var reportUrl = '<%= Url.JavaScriptReplacableUrl(MVC.Survey.Report())%>';
reportUrl = myUrl.replace('{' + MVC.Survey.ReportParams.id + '}', selectedRow);
window.open(reportUrl);
The advantage of this is that you get compile-time checking for the controller, action, and action parameter.
One more thing that can be done, no so clean i guess:
var url = "#Url.RouteUrl(new { area = string.Empty, controller = "Survey", action = "Report" })";
var fullUrl = url + '?id=' + selectedRow;
The easiest way is to declare a javascript variable than concate your parameter value to the link.
var url = '#Url.Action("Action","Controller")' + "/" + yourjavascriptvariable;
<a href='" + url + "'>

How to spit out a JavaScript variable (type array) from Custom Control

I want to spit out a JS variable array like so from my custom control:
var myArray = [5410, 8362, 6638, 6758, 7795]
caveat is that I want it to also be unique to this control as this control will spit out more JavaScript that will utilize myArray. So I envision something like:
var [control'sID]myArray = [5410, 8362, 6638, 6758, 7795]
and then somehow the rest of the JavaScript that is spit out can reference this somehow where needed in the JS code. I've got certain methods in the other JavaScript that is being spit out that is using myArray.
You can use ClientID
C# Example:
Response.Write("var " + this.ClientID + "myArray = [5410, 8362, 6638, 6758, 7795];");
VB Example:
Response.Write("var " & Me.ClientID & "myArray = [5410, 8362, 6638, 6758, 7795];")
I think you want to generate a javascript array from your custom control, and you want to access it from your main library,
You can use your control's ClientID property to make your arrays unique.
And you can define a global array to keep the array's names to access them from your rest of the client scripts.
EDIT :
I just look at the RegisterExpandoAttribute method, it's an interesting method, I have never heard that before. In this example it looks like it you can add custom attributes to your control.
So you can use it in your custom control like that :
Just add a custom attribute that has your array items with comma separated. and when accessing the custom attribute, simply split them by comma. I think this will work for your question.
At your custom control :
Page.ClientScript.RegisterExpandoAttribute(this.ClientID
, "myArray", "1,2,3,4");
At client side, get your array like that :
var myArray =
document.getElementById('<%= this.ClientID %>').getAttribute("myArray")).split(',');

Resources