Dynamic text box
-
Dear all, I have a dynamic grid, in that I have textboxes added dynamically.When I enter value in textbox1 and textbox2 in a row,the sum should be calculated and displayed in the third textbox at the time of enytering values. I added event handler at the time of creation of text box.So the handler is getting fired at the time of databind event.But i need that handler should be raised at the time of textbox blur event. Or else you can give your known solution. Thanks in advance, Srinivas Mateti
-
Dear all, I have a dynamic grid, in that I have textboxes added dynamically.When I enter value in textbox1 and textbox2 in a row,the sum should be calculated and displayed in the third textbox at the time of enytering values. I added event handler at the time of creation of text box.So the handler is getting fired at the time of databind event.But i need that handler should be raised at the time of textbox blur event. Or else you can give your known solution. Thanks in advance, Srinivas Mateti
Basically you need javascript to handle this... During databind do like this :
TextBox tb1 = e.item.FindControl("textbox1") as TextBox;
TextBox tb2 = e.item.FindControl("textbox2") as TextBox;
TextBox tb3 = e.item.FindControl("textbox3") as TextBox;tb1.Attributes.Add("onblur", "javascript:return refreshValue('" + tb2.ClientId + "','" + tb3.ClientId + "');");
tb2.Attributes.Add("onblur", "javascript:return refreshValue('" + tb1.ClientId + "','" + tb3.ClientId + "');");Now place the function in your page inside a script tag:
function refreshValue(operand, target){
try {
var value1 = parseInt(this.value);
var value1 = parseInt(document.getElementById(operand).value);
document.getElementById(target).value = (value1 + value2);
}
catch(err) {
alert(err.description);
return false;
}
return true;
}I think this will solve the issue. Try it. ;)
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Basically you need javascript to handle this... During databind do like this :
TextBox tb1 = e.item.FindControl("textbox1") as TextBox;
TextBox tb2 = e.item.FindControl("textbox2") as TextBox;
TextBox tb3 = e.item.FindControl("textbox3") as TextBox;tb1.Attributes.Add("onblur", "javascript:return refreshValue('" + tb2.ClientId + "','" + tb3.ClientId + "');");
tb2.Attributes.Add("onblur", "javascript:return refreshValue('" + tb1.ClientId + "','" + tb3.ClientId + "');");Now place the function in your page inside a script tag:
function refreshValue(operand, target){
try {
var value1 = parseInt(this.value);
var value1 = parseInt(document.getElementById(operand).value);
document.getElementById(target).value = (value1 + value2);
}
catch(err) {
alert(err.description);
return false;
}
return true;
}I think this will solve the issue. Try it. ;)
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript -
Thanks abhishake, Actually here the probles is the client id. All textboxes will be added dynamically.I doent know the textbox name..I will work on your advice. Thanks once again, Srinivas Matetu
What do you mean by Dynamic controls. Do you mean the controls will be generated inside DataList's ItemTemplate ? Yes, you can access controls inside
ItemTemplate
from codebehind just like I mentioned in my other post. You need to find the control when
ItemDataBound
event executes.
<asp:Datalist....OnItemDataBound="Item_Bound" >
<itemtemplate>
<asp:textbox runat="server" id="tb1" />
</itemtemplate>Now from codebehind :
void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{TextBox tb1 = e.Item.FindControl("tb1") as TextBox; //same as the control inside Itemtemplate
}
}
Note : You must place Runat = server if you want the control to be found properly using
e.Item.FindControl
Hope this clears your doubt. "Dont forget to click "Good Answer" if it helped you" ;) ;)Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript