This is a migrated thread and some comments may be shown as answers.

[Solved] Find TextBox in GridTemplateColumn from JavaScript.

3 Answers 502 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Grzesiek
Top achievements
Rank 2
Iron
Grzesiek asked on 12 Nov 2009, 09:08 AM
I have template column:

<telerik:GridTemplateColumn HeaderText="salary"><ItemTemplate>  
            <asp:TextBox ID="salary" runat="server"></asp:TextBox></ItemTemplate>  
</telerik:GridTemplateColumn>  

Now I want to change the text in textbox in each row - the text is number, so I want to multiply it by 3.

But I don't know how to get the value of TextBoxes in JavaScript code?

var masterTable = $find("<%=RadGrid1.ClientID%>").get_masterTableView();   
  
for(var row = 0; row < masterTable.get_dataItems().length; row++)   
{   
    var salary = masterTable.getCellByColumnUniqueName(masterTable.get_dataItems()[row], "profit").innerHTML;  
    salary.innerHTML = salary.innerHTML * 2;
}   

It gives me a "NaN" error, because the innerHTML returns me "<span ...> 3.423 </span>"  but not "3.423" for example.

Anyone can help me? The grid is not in edit mode. I have template column with textbox.


3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 12 Nov 2009, 09:45 AM
Hello,

I achieved same functionality by using RadTextBox in ItemTemplate instead of standard textbox. Here is the code that I tried.

ASPX:
 
        <telerik:GridTemplateColumn DataField="CompanyName" HeaderText="CompanyName1" UniqueName="CompanyName1"
            <ItemTemplate> 
                <telerik:RadTextBox ID="salary" runat="server"
                </telerik:RadTextBox> 
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 

JavaScript:
 
<script type="text/javascript"
    function add() { 
        var masterTable = $find("<%=RadGrid1.ClientID%>").get_masterTableView(); 
        for (var row = 0; row < masterTable.get_dataItems().length; row++) { 
            var item = masterTable.get_dataItems()[row]; 
            var value = item.getDataKeyValue("Number"); 
            var radTextBox1 = item.findControl("salary"); 
            radTextBox1.set_value(value * 3); 
        } 
    } 
</script> 

Regards,
Shinu.
0
Grzesiek
Top achievements
Rank 2
Iron
answered on 12 Nov 2009, 01:33 PM
Thanks, it works.

                function test(salary, profit, product) { 
                    var temp_salary = $find(salary); 
                    var temp_productivity = $find(product); 
                    var cena_produktu = document.getElementById('produkt').value.replace(",""."); 
                    var cena_surowca = document.getElementById('surowiec').value.replace(","".") * 1; 
                    var jakosc = 4; 
                    var produktywnosc = temp_productivity.get_value().replace(",""."); 
                    var pensja =  temp_salary.get_value().replace(",""."); 
                    var koszty = produktywnosc * (cena_produktu - cena_surowca * jakosc) - pensja; 
                    var temp_profit = $find(profit); 
                    temp_profit.set_value(koszty);  
                } 

But I have another problem. I put this code in onKeyUp, onKeyDown, onKeyPress, onBlur, onFocus and still the same problem.

What I mean.
Example:
profit value in RadTextBox -= salary RadTextBox value

In profit RadTextBox I have value "23.05".
In salary RadTextBox I enter value "1".

profit is has still the same value = "23.05".

So I press 2 and in salary RadTextBox I have "12". Then profit RadTextBox is "22.05" which means, that after press the "2" key, the value has been recalculate.

If I delete "2" in salary, profit becomes "11.05" - like the salary is "12".

The code fires on key to late - how to prevent it?



0
Sebastian
Telerik team
answered on 17 Nov 2009, 04:22 PM
Hello Grzesiek,

You may consider employing the client-side API of RadInput controls (RadTextBox in your case) as demonstrated on this integration example to perform the necessary calculations on the client. Feel free to adapt the technique for your particular configuration.

Best regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Grzesiek
Top achievements
Rank 2
Iron
Answers by
Shinu
Top achievements
Rank 2
Grzesiek
Top achievements
Rank 2
Iron
Sebastian
Telerik team
Share this question
or