auto generate number of days in another textbox upon selection of start and end date in asp.net c# -
i got task creating leave application employees.. contains 3 tabs 1 apply leave has
- type leave
- description
- begin date
- end date
- number of days
those begin , end date contains calendar extenders , once select both dates , cursor should point on number of days textbox
, should calculate total number of days leave taken... please me sort out..
i have tried
cs
protected void btnapply_click(object sender, eventargs e) { mtmsdto objc = new mtmsdto(); int flag = 0; lbllogdinuser.text = session["empname"].tostring(); objc.loggedinuser = lbllogdinuser.text; objc.typeofleave = drptypeofleave.selecteditem.text; string date; date = convert.todatetime(txtbegindate.text).tostring("dd/mm/yyyy"); datetime dt = new datetime(); dt = convert.todatetime(date); objc.begindate = dt; objc.enddate = convert.todatetime(txtenddate.text); objc.description = txtdescription.text; objc.numofdays = convert.toint32(txtnumofdays.text); //objc.empname = lbllogdinuser.text; int x = obj.insertleave(objc); { if (x >= 0) { flag = 1; } else { flag = 0; } } if (flag == 1) { lblsuccess.visible = true; lblsuccess.text = "data added successfully"; drptypeofleave.clearselection(); txtbegindate.text = ""; txtenddate.text = ""; txtdescription.text = ""; txtnumofdays.text = ""; } else { lblerr.visible = true; lblerr.text = "failed add data!!!"; } } protected void txtnumofdays_textchanged1(object sender, eventargs e) { mtmsdto objc = new mtmsdto(); txtnumofdays.text = session["numofdays"].tostring(); objc.numofdays = convert.toint32(txtnumofdays.text); } protected void txtenddate_textchanged(object sender, eventargs e) { datetime begindate = convert.todatetime(txtbegindate.text); datetime enddate = convert.todatetime(txtenddate.text); timespan diff = enddate.subtract(begindate); txtnumofdays.text = diff.days.tostring(); txtnumofdays.focus(); }
aspx
<asp:panel id="panel1" runat="server" height="567px" width="858px"> <table style="width:100%; height: 587px;"> <tr> <td class="style50"> </td> <td class="style51"> <asp:tabcontainer id="tabcontainer1" runat="server" activetabindex="0" height="550px" width="833px"> <asp:tabpanel id="tablap" runat="server" headertext="leave application"> <headertemplate> <span class="style40"><strong>leave application</strong></span> </headertemplate> <contenttemplate> <table style="width:101%; height: 505px; margin-left: 0px;"> <tr> <td class="style160" style="font-weight: 700;"> </td> <td class="style161" style="font-weight: 700; "> <asp:label id="lbllogdinuser" runat="server" bordercolor="#0061c1" borderwidth="1px" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#0061c1" height="23px" visible="false" width="123px"></asp:label> <asp:roundedcornersextender id="lbllogdinuser_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="lbllogdinuser"></asp:roundedcornersextender> </td> <td class="style162" style="font-weight: 700; "> </td> </tr> <tr> <td class="style159" style="font-weight: 700; "> type of leave:</td> <td class="style156"> <asp:dropdownlist id="drptypeofleave" runat="server" appenddatabounditems="true" bordercolor="#0061c1" borderwidth="1px" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#0061c1" height="29px" onselectedindexchanged="drptypeofleave_selectedindexchanged1" style="text-align: left" width="123px"><asp:listitem selected="true" value="0">please select</asp:listitem><asp:listitem value="1">sick leave</asp:listitem><asp:listitem value="2">casual leave</asp:listitem><asp:listitem value="3">earned leave</asp:listitem> </asp:dropdownlist> <asp:roundedcornersextender id="drptypeofleave_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="drptypeofleave"></asp:roundedcornersextender> </td> <td class="style141" style="font-weight: 700; "> </td> </tr> <tr> <td class="style146" style="font-weight: 700; "> description:</td> <td class="style154" style="font-weight: 700; "> <asp:textbox id="txtdescription" runat="server" bordercolor="#0061c1" borderwidth="1px" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#0061c1" height="25px" style="text-align: left" textmode="multiline" width="103px"></asp:textbox> <asp:roundedcornersextender id="txtdescription_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="txtdescription"></asp:roundedcornersextender> </td> <td class="style144" style="font-weight: 700; "> </td> </tr> <tr> <td class="style159" style="font-weight: 700; "> begin date:</td> <td class="style156" style="font-weight: 700; "> <asp:textbox id="txtbegindate" runat="server" bordercolor="#0061c1" borderwidth="1px" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#0061c1" height="25px" style="text-align: left" width="73px"></asp:textbox> <asp:imagebutton id="imagebutton1" runat="server" alternatetext="click show calendar" imageurl="images/calendar.png" /> <asp:calendarextender id="txtbegindate_calendarextender" runat="server" enabled="true" format="dd/mm/yyyy" popupbuttonid="imagebutton1" popupposition="right" targetcontrolid="txtbegindate" todaysdateformat="dd/mm/yyyy"></asp:calendarextender> <asp:roundedcornersextender id="txtbegindate_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="txtbegindate"></asp:roundedcornersextender> <asp:comparevalidator id="chkbegindate" runat="server" controltovalidate="txtbegindate" display="dynamic" errormessage="you must supply valid start date" forecolor="red" operator="datatypecheck" type="date" /><td class="style141" style="font-weight: 700; "> </td> </td> </tr> <tr> <td class="style159" style="font-weight: 700; "> end date:</td> <td class="style156" style="font-weight: 700; "> <asp:textbox id="txtenddate" runat="server" bordercolor="#0061c1" borderwidth="1px" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#0061c1" height="25px" ontextchanged="txtenddate_textchanged" style="text-align: left" width="73px" autopostback ="true"></asp:textbox> <asp:imagebutton id="imagebutton2" runat="server" alternatetext="click show calendar" imageurl="images/calendar.png" /> <asp:calendarextender id="txtenddate_calendarextender" runat="server" enabled="true" format="dd/mm/yyyy" popupbuttonid="imagebutton2" popupposition="right" targetcontrolid="txtenddate" todaysdateformat="dd/mm/yyyy"></asp:calendarextender> <asp:roundedcornersextender id="txtenddate_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="txtenddate"></asp:roundedcornersextender> <asp:comparevalidator id="chkenddate" runat="server" controltovalidate="txtenddate" display="dynamic" errormessage="you must supply valid end date" forecolor="red" operator="datatypecheck" type="date" /><br /> <asp:comparevalidator id="cmpbeginandenddates" runat="server" controltocompare="txtbegindate" controltovalidate="txtenddate" display="dynamic" errormessage="the end date must after start date" forecolor="red" operator="greaterthan" /></td> <td class="style141" style="font-weight: 700; "> </td> </tr> <tr> <td class="style159" style="font-weight: 700; "> number of days:</td> <td class="style156" style="font-weight: 700; "> <asp:textbox id="txtnumofdays" runat="server" bordercolor="#0061c1" borderwidth="1px" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#0061c1" height="25px" ontextchanged="txtnumofdays_textchanged1" style="text-align: left" width="73px"></asp:textbox> <asp:roundedcornersextender id="txtnumofdays_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="txtnumofdays"></asp:roundedcornersextender> </td> <td class="style141" style="font-weight: 700; "> </td> </tr> <tr> <td class="style164" style="font-weight: 700; "> <br /> <asp:label id="lblsuccess" runat="server" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#009933" height="14px" style="text-align: right" visible="false"></asp:label> <br /> <asp:label id="lblerr" runat="server" font-bold="true" font-names="verdana" font-size="x-small" forecolor="#cc0000" height="14px" visible="false"></asp:label> </td> <td class="style163" style="font-weight: 700; "> <asp:button id="btnapply" runat="server" backcolor="white" bordercolor="#0061c1" borderwidth="1px" cssclass="buttonclass" font-bold="true" font-names="verdana" font-size="small" forecolor="#0061c1" height="21px" onclick="btnapply_click" text="apply" width="65px" /> <asp:roundedcornersextender id="btnapply_roundedcornersextender1" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="btnapply"></asp:roundedcornersextender> <asp:button id="btncancel" runat="server" backcolor="white" bordercolor="#0061c1" borderwidth="1px" cssclass="buttonclass" font-bold="true" font-names="verdana" font-size="small" forecolor="#0061c1" height="21px" onclick="btncancel_click" text="cancel" width="65px" /> <asp:roundedcornersextender id="btncancel_roundedcornersextender" runat="server" bordercolor="black" enabled="true" radius="4" targetcontrolid="btncancel"></asp:roundedcornersextender> </td> <td class="style165" style="font-weight: 700; "> </td> </tr> <tr> <td class="style159" style="font-weight: 700; "> </td> <td class="style156" style="font-weight: 700; "> </td> <td class="style141" style="font-weight: 700; "> </td> </tr> </table> </contenttemplate> </asp:tabpanel> </asp:panel>
i'm getting number of days value in database not showing in textbox
, cursor not pointing textbox
of number of days wen run project..
i think taking hard way out there easier way of implementing trying in asp.net. first require use of jquery ui (date-picker). came pure javascript , html5 solution problem. think going bit on board post of data can date out of it.
note: not doing validation nor have made specific towards application.
here solution:
css style, make things okay looking ;)
<!-- link jquery ui css --> <link href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> <style> #employee_form { width: 350px; background: #f0e68c; padding: .2em; } li { list-style: none; font-weight: bold; } li input { float: right; } </style>
aspx page
<form id="form1" runat="server"> <div id="employee_form"> <ul> <li> <p> <asp:label id="label1" runat="server" text="type of leave"></asp:label> <asp:textbox id="textbox1" runat="server"></asp:textbox> </p> </li> <li> <p> <asp:label id="label6" runat="server" text="description"></asp:label> <asp:textbox id="textbox6" runat="server"></asp:textbox> </p> </li> <li> <p> <asp:label id="label2" runat="server" text="begin date"></asp:label> <asp:textbox id="begindate" runat="server"></asp:textbox> </p> </li> <li> <p> <asp:label id="label3" runat="server" text="end date"></asp:label> <asp:textbox id="enddate" runat="server"></asp:textbox> </p> </li> <li> <p> <asp:label id="label4" runat="server" text="number of days"></asp:label> <asp:textbox id="textbox4" runat="server"></asp:textbox> </p> </li> <asp:button id="button1" runat="server" text="submit" onclick="button1_click" /> </ul> </div> </form>
javascript
<!-- link latest jquery --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <!-- link jquery ui --> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <script> (function () { $('.date').datepicker(); var resultdate = ""; $('.date').on('change', function () { var begindate = $('#begindate').val(); var enddate = $("#enddate").val(); if (begindate != "" && enddate != "") { begindate = new date(begindate); enddate = new date(enddate); resultdate = enddate.getdate() - begindate.getdate(); } $("#days").val(resultdate + " "); }); })(); </script>
cs
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; namespace _web_stackoverflow_question_test_code { public partial class employeeleaveform : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { // submit data database } } }
end of solution feel free ask me question on twitter @muhammad_khan40.
thanks, let me know if solution helped problem.
Comments
Post a Comment