copy several textboxes data into target textbox calling same function - c#

I have several input textboxes with different id and class name. I want to track changes in any of the input textboxes, and reflect it into target textbox, but don't want to replicate the function for every individual textbox. Is it possible to use one function for all?
Appreciate your help

You can bind the change event for all the textboxes in the page using
$("input:text").change(function(){
var textValue = this.value;
$("#targetbox").val(textValue);
});
If the textboxes are inside a particular container then you can limit the search inside the container
$("#parentcontainer input:text").change(function(){
var textValue = this.value;
$("#targetbox").val(textValue);
});

try something like this..........
var finalValue ='';
$('.textBoxesId').each(function(){
$(this).change(function(){
finalValue = $(this).val();
});
});
$('#targetID').val(finalValue );

Related

Change Textbox text from another frame(container)

I see here lot of similar question, but I still not find answer that help me in situation.
I have two frame(lets say FrameChild), one is "in" another(practically FrameChild is in this frame, lets say FrameMain).
When I insert all parameters in FrameChild and tap on button witch is on bottom of FrameMain I call method that return string...
Now when i get string i need to change textbox text in FrameChild
I have tray many way.
First idea was something like:
FrameChild frm = new FrameChild;
frm.textbox.text = "somestring";
But nothing happen.
Than i thing use some property.
in FrameChield:
public string setTicNo
{
set
{
textBox.Text = value;
}
}
in FrameMain:
FrameChild frm = new FrameChild;
frm.setTicNo = "somestring";
When i debbuging I get value, but textbox still is empty...
On the end I try to bind textbox text on setTicNo;
public string setTicNo
{
get
{
return setTicNo;
}
set
{
setTicNo = value;
}
}
Xaml:
Text = {Binding setTicNo, Mode=TwoWay,UpdateSourceTrigger=Explicit}
(here i try use more bindings, but every time i get infinite loop.
Please help , I not have more ideas..
Thanx
Did you try building a single view model and bind it to both frames, if it was passed by ref which is the default it will change the value once you do.
A side note implement a INOTIFYPROPERTYCGANGED in the View model

How do I add a function stored in a string to a jquery .click event

I am trying to write a generic table generator where I can tell it the function to call in the onclick event of each cell. The function is then defined somewhere else on the page.
The problem is I cannot get the page to evaluate the function at the right moment. I want it to convert my string to a function when it is building the DOM not when it fires the click event. Maybe this is not possible in Javascript/JQuery?
A simplified version of the code I originally wrote server-side in C# is:
string functionCall = "DoSomething(4);";
TableCell detailCell = new TableCell();
detailCell.Attributes.Add("onClick", functionCall);
functionCall = "DoSomething(5);";
detailCell = new TableCell();
detailCell.Attributes.Add("onClick", functionCall);
This then worked when it was rendered as html in the browser. It turned into:
function DoSomething(p)
{
alert(p);
}
<td onclick="DoSomething(4)"></td>
<td onclick="DoSomething(5)"></td>
I want to replicate this functionality in Javascript.
If I do:
var functionCall = 'DoSomething(4);';
$('#detailCell').attr('onclick', functionCall);
it won't fire the event in IE7 for some reason but fires it in Chrome.
If I do:
var functionCall = 'DoSomething(4);';
$('#detailCell1').click(function () { eval(functionCall); });
functionCall = 'DoSomething(5);';
$('#detailCell2').click(function () { eval(functionCall); });
then it doesn't change functionCall to a piece of text when the click event is added. Instead it evals functionCall WHEN the cell is clicked. This means the value of functionCall is now wrong.
e.g. it always does DoSomething(5) even if I click on detailCell1 because that was the last value of functionCall.
If I do:
var functionCall = 'DoSomething(4);';
$('#detailCell1').click(eval(functionCall));
it runs the function in functionCall immediately when adding the click event rather than when it is clicked.
I hope this makes some sense as it is difficult to explain.
Basically I just want to be able to do
<td onclick="any javascript I feel like storing in a string"></td>
using JQuery.
Is this even possible?
This is my first ever post on stackoverflow so please be gentle...
Try this...
var functionCall = 'DoSomething(4);';
$('#detailCell1').click((function (code) {
return function () {
eval(code);
};
}(functionCall)));
What happens here is that you capture the string at the time that you create the handler by assigning it to the parameter 'code'.
In your current scenario the click handler uses the value of the string at the time it is clicked and not at the time the handler is created.
This is called a closure!
(function (code) {
return function () {
eval(code);
};
}(functionCall));
The functionCall value is assigned to the code parameter.
Now when you change functionCall the code parameter still refers to the old value.
Not clear why function call should be in string. As I can see, you have many cells that should fire one function with different params for each cell.
In this case I would do next:
TableCell detailCell = new TableCell();
detailCell.Attributes.Add("class", "detailCell");
detailCell.Attributes.Add("data-num", "4");
detailCell = new TableCell();
detailCell.Attributes.Add("class", "detailCell");
detailCell.Attributes.Add("data-num", "5");
And than, somewhere in js:
$(function() {
$(".detailCell").click(function(){
DoSomething($(this).data("num"));
});
})
class on table cell is needed in order to easily assign click handler to all detail cells, so you do not need to write something like:
$('#detailCell1').click(function () { DoSomething(4); });
$('#detailCell2').click(function () { DoSomething(5); });
This detailCell.Attributes.Add("data-num", "5"); allows to put number that should be used as a parameter for DoSomething. You can see it being taken in this row: DoSomething($(this).data("num")); jQuery.data function is being used. and this will point to an element being clicked.
$(function() {}) - this is a shorthand for jQuery.ready function. Using it you can guaranty that code inside that function will be executed when page is ready for use.
instead of first creating it as a string and then trying to evaluate that string you can simply create a function value (functions are object too in JS)
so instead of
var functionCall = 'DoSomething(4);';
$('#detailCell1').click(eval(functionCall));
do
$('#detailCell1').click(function(e){DoSomething(4);});
this creates a new function for every click. (In your case you could omit the e parameter, it's simply there to make any changes that requires it easier
if you have a set of parameters you are passing you can create a factory method
var doSomethingFactory = function(num){
return function(e) { DoSomething(num);};
}
$('#detailCell1').click(doSomethingFactory(4));
As per my understanding, you have to call different function with different arguments.To achieve this you are generating string and try to call those function by using eval() function. Let us do these as follows.
1.Just pass those functioncalls string as array, So that on load your javascript code look like as follows,
var functioncalls = ['DoSomething(1)','DoSomethingMore(1,2)','DoDifferent("hello world")'];
2.Then loop your function call array in javascript and assign them to different click event, To avoid complexity, I assume you selector are #detailCell0,#detailCell1,#detailCell2 and so on. Then your code look like as follows,
$(function(){
functioncalls.forEach(function(functioncall,index){
$("detailCell"+index).click(function(){
eval(functioncall);
})
});
})
Note: There are lot of ways to do this.

Textbox onchange event

So I have a text box, where I add an onchange event of markAsException.
My javascript is -
function markAsException(recordID) {
//alert("Exception");
//mark exception column
document.getElementById("ctl00_cpMain_lblScrollException_" + recordID).innerText = "Exception";
document.getElementById("ctl00_cpMain_lblScrollException_" + recordID).style.color = "#FF0000";
document.getElementById("ctl00_cpMain_tdScrollException_" + recordID).style.backgroundColor = "#99CCFF";
//enable comments ddl and remove blank (first item)
document.getElementById("ctl00_cpMain_ddlCommentId_" + recordID).disabled = false;
document.getElementById("ctl00_cpMain_ddlCommentId_" + recordID).focus();
document.getElementById("ctl00_cpMain_ddlCommentId_" + recordID).options[0] = null;
}
What I want to do is, when a user changes the value in a textbox, to mark a column as "Exception", and then focus a drop down list where they have to chose the reason for the exception.
This is what happens.. If I am on that text box and change it, then tab, it tabs to the drop down list.
However, if I change the value and then simply click in another text box on the form, I don't focus the drop down list.
How would I accomplish that?
I would suggest using JQuery's change() function.
The advantage is that it will be more stable across different browsers.
Something like:
$('#<%= TextBox1.ClientID %>').change(function(){
// extract the recordID from the textbox - perhaps with an attribute?
markAsException(recordID);
});
My comment was getting longer so I'm expanding:
Take a look at the JQuery documentation for setting this up as the page finishes loading. If tb is the ID of your textbox your selector would be
$('#<%= tb.ClientID %>')
I would suggest you replace your code and use
tb.Attributes.Add("recordID", recordId.ToString());
This will add the ID you need onto the textbox tag. Once you're in the function I outlined above you can use the following selector to get the recordID in javascript
var recordID = $('#<%= TextBox1.ClientID %>').attr('recordID');
All together
$(document.ready(function(){
$('#<%= tb.ClientID %>').change(function(){
var recordID = $('#<%= tb.ClientID %>').attr('recordID');
if(recordID){
markAsException(recordID);
}
});
});

aspx change focus from different TextBoxes on the fly

I am trying to change the focus from one textbox to another while the character count in
on textbox reaches 13 I am using the code below with nothing happening whatsoever:
if (!this.ClientScript.IsClientScriptBlockRegistered("qtyFocus"))
{
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "qtyFocus",
#"<script type='text/javascript' language='javascript'>function qtyFocus(){
var trckNumberLength = document.getElementById('txtTrackingNumber').value.length;
if(trckNumberLength == 13){
document.getElementById('txtQuantity').focus();
}}</script>");
}
txtTrackingNumber.Attributes.Add("onchange", "javascript:return qtyFocus();");
can anyone help please ?
Probably because the line in the script that's doing
var trckNumberLength = document.getElementById('txtTrackingNumber').value.length;
Needs to be changed for:
var trckNumberLength = document.getElementById('"+txtTrackingNumber.ClientID+"').value.length;
The reason being that txtTrackingNumber will very likely have a different Id when it's rendered on the page so you need to use the ClientID property of the control instead of the id you defined on the markup.

how i can got the value who are define n times in the form in ASP.NET MVC?

i have a form where user fill the n time a information using add another textbox and fill them.
i put the name them as
textbox_1
textbox_2
now how i can got all form values who are start with textbox_1. any idea to do it in asp.net mvc
you can get the value of this text box using javascript and save it in hidden field as comma separated then read the hidden field value from your action method(I am using jquery)
var AllTextBoxesInPage = $('input[type=text]');
var AllValues='';
AllTextBoxesInPage.each(function(index){
if(index==0)
{
AllValues+=',';
}
AllValues+=$(this).val();
});
$('#HiddenFieldID').val(AllValues);
in your controller class
public ActionResult MyAction(FormCollection MyForm)
{
String AllValues =MyForm["HiddenFieldName"];
String[] SeparatedValuse = AllValues.Split(",");
}
Alternatively, you can use BeginCollection from steve sanderson and optionally you can have a look at master detail form

Categories