Quote:
Originally Posted by preetham.sarojavenkatesh
hai,
Its very urgent.can somebody tell m,how to add two textboxes and then display the result in the third textbox ,using keypress -in javascript....
regards....
preet
|
This will work for you
You would need to handle the server-side RowDataBound event of the Grid and attach the event handler there.
aspx file:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Some value">
<ItemTemplate>
<asp:TextBox ID="gridTextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Some value">
<ItemTemplate>
<asp:TextBox ID="gridTextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Some value">
<ItemTemplate>
<asp:TextBox ID="gridTextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript">
<!--
function gridTextBoxOnBlur(textBox1Id, textBox2Id, textBox3Id)
{
var textBox1Ref = document.getElementById(textBox1Id);
var textBox2Ref = document.getElementById(textBox2Id);
var textBox3Ref = document.getElementById(textBox3Id);
// Clear the totals TextBox...
textBox3Ref.value = '';
var textBox1Value = textBox1Ref.value;
var textBox2Value = textBox2Ref.value;
// Check for content...
if ( textBox1Value.length <= 0 )
{
alert('Please enter a value in TextBox #1');
textBox1Ref.focus();
textBox1Ref.select();
return false;
}
if ( textBox2Value.length <= 0 )
{
alert('Please enter a value in TextBox #2');
textBox2Ref.focus();
textBox2Ref.select();
return false;
}
// Convert to nemeric...
textBox1Value = parseFloat(textBox1Value);
textBox2Value = parseFloat(textBox2Value);
// Check for numeric content...
if ( isNaN(textBox1Value) == true )
{
alert('The TextBox #1 value is not numeric');
textBox1Ref.focus();
textBox1Ref.select();
return false;
}
if ( isNaN(textBox2Value) == true )
{
alert('The TextBox #2 value is not numeric');
textBox2Ref.focus();
textBox2Ref.select();
return false;
}
// Fille the totals TextBox...
textBox3Ref.value = textBox1Value + textBox2Value;
return true;
}
// -->
</script>
aspx.cs file:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox gridTextBox1 = (TextBox)e.Row.FindControl("gridTextBox1");
TextBox gridTextBox2 = (TextBox)e.Row.FindControl("gridTextBox2");
TextBox gridTextBox3 = (TextBox)e.Row.FindControl("gridTextBox3");
if ( (gridTextBox1 != null) && (gridTextBox2 != null) && (gridTextBox3 != null) )
{
string eventHandler = string.Format("gridTextBoxOnBlur('{0}', '{1}', '{2}');", gridTextBox1.ClientID, gridTextBox2.ClientID, gridTextBox3.ClientID);
gridTextBox1.Attributes.Add("onblur", eventHandler);
gridTextBox2.Attributes.Add("onblur", eventHandler);
}
}
}