.net - template field issue in the gridview -
q:
i have following problem , , don't know how fix really.
i have grid view
1 of columns template field
(text box). grid view consists of 8
rows . every time user enter data in text box,i put total in last text box(which set enabled = false).i sum data entry in text boxes through method , call in event text changed
. every time enter number in text box , click tab in keyboard
or use mouse cursor move next box lose focus , , have put mouse cursor again in intended textbox.
i try following methods fix problem in vain .
foreach (gridviewrow r in gv_evaluation.rows) { ((radtextbox)r.cells[3].findcontrol("txt_evaluateweights")).attributes.add("blur", "calc()"); }
in page load , doesn't work @ all.
protected void txt_evaluateweights_textchanged(object sender, eventargs e) { calc(); ((textbox)sender).focus(); }
this way return focus previous textbox (i mean 1 have done) not text box wanna focus in, enter data.
edit:
my calc method:
private void calc() { float sum = 0; (int = 0; < 7; i++) { radtextbox txt1 = (radtextbox)gv_evaluation.rows[i].cells[3].findcontrol("txt_evaluateweights"); int weight; bool result = int32.tryparse(txt1.text, out weight); if (result) { sum += weight; } } double percentage; percentage = math.round((sum / 100) * 100, 2); radtextbox txt3 = (radtextbox)gv_evaluation.rows[7].cells[3].findcontrol("txt_evaluateweights"); txt3.text = percentage.tostring();//string.format("{0:0.0%}", percentage.tostring()); }
doing using server side postback horrendous way of doing this.
use javascript instead. here small example in jquery
the gridview
<asp:gridview id="demogrid" runat="server" autogeneratecolumns="false" showfooter="true"> <columns> <asp:templatefield headertext="index"> <itemtemplate><%# container.dataitemindex + 1 %></itemtemplate> </asp:templatefield> <asp:templatefield headertext="item"> <itemtemplate> <asp:label id="demolabel" runat="server" text='<%# container.dataitem %>' /> </itemtemplate> <footertemplate>total</footertemplate> </asp:templatefield> <asp:templatefield headertext="amount"> <itemtemplate> <asp:textbox id="demotext" runat="server" cssclass="quantity"> </asp:textbox> </itemtemplate> <footertemplate> <asp:label id="totallabel" runat="server" cssclass="result"/> </footertemplate> </asp:templatefield> </columns> </asp:gridview>
the code behind
protected void page_load(object sender, eventargs e){ if (!ispostback) { string[] array = new string[] { "demo1", "demo2", "demo3", "demo4", "demo5" }; demogrid.datasource = array; demogrid.databind(); } }
the javascript (jquery)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $(".quantity").bind("blur", function () { var $quantity = $(this); var quantity = +$quantity.val(); //cast number if (!isnan(quantity)) { var $sum = $quantity.closest("table").find("tr:last .result"); var sum = +$sum.html(); $sum.html(sum + quantity); } }); }); </script>
hope helps
Comments
Post a Comment