c# - String.Empty not working on button click -
i trying clear form on cancel button click function clearform() uses string.empty each field of form not working. neither getting error nor getting expected result.
here's design code:
<%@ page language="c#" autoeventwireup="true" codefile="addemployee.aspx.cs" inherits="addemployee" %> <%@ register assembly="ajaxcontroltoolkit" namespace="ajaxcontroltoolkit" tagprefix="ajax" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="head1" runat="server"> <title>add employee</title> <link href="css/style.css" type="text/css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <ajax:toolkitscriptmanager id="toolkit1" runat="server"></ajax:toolkitscriptmanager> <div> <asp:updatepanel id="updatepanel1" runat="server"> <contenttemplate> <table align="center" class="loginbox"> <tr> <th colspan="2" style="color:white; font-size:medium; padding-bottom:10px;" align="left">personal information</th> </tr> <tr> <td>name:</td> <td><asp:textbox id="txtname" cssclass="signup_textbox" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="reqname" controltovalidate="txtname" runat="server" display="none" errormessage="name" ></asp:requiredfieldvalidator> </td> </tr> <tr> <td>address</td> <td><asp:textbox textmode="multiline" height="40" width="135" id="txtaddress" cssclass="signup_textbox" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="reqadd" controltovalidate="txtaddress" runat="server" display="none" errormessage="address" ></asp:requiredfieldvalidator> </td> </tr> <tr> <td>dob:</td> <td> <asp:textbox id="txtdob" cssclass="signup_textbox cal_box" runat="server" /> <ajax:calendarextender id="calendarextender1" targetcontrolid="txtdob" format="dd/mm/yyyy" runat="server"></ajax:calendarextender> </td> </tr> <tr> <td>gender</td> <td> <asp:radiobuttonlist cssclass="signup_textbox" id="genderlist" runat="server" repeatdirection="horizontal"> <asp:listitem selected="true">male</asp:listitem> <asp:listitem>female</asp:listitem> </asp:radiobuttonlist> </td> </tr> <tr> <th colspan="2" style="color:white; font-size:medium; padding-bottom:10px;" align="left">professional information</th> </tr> <tr> <td>department:</td> <td> <asp:dropdownlist id="ddldept" cssclass="signup_textbox" runat="server"></asp:dropdownlist> </td> </tr> <tr> <td>designation:</td> <td><asp:textbox id="txtdesig" cssclass="signup_textbox" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="reqdesig" controltovalidate="txtdesig" runat="server" display="none" errormessage="designation" ></asp:requiredfieldvalidator> </td> </tr> <tr> <td>salary:</td> <td><asp:textbox id="txtsal" cssclass="signup_textbox" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="reqsal" controltovalidate="txtsal" runat="server" display="none" errormessage="salary" ></asp:requiredfieldvalidator> <asp:regularexpressionvalidator runat="server" id="rexsal" controltovalidate="txtsal" validationexpression="^[0-9]{5}$" errormessage="salary must max 5 digits" display="none" /> </td> </tr> <tr> <th colspan="2" style="color:white; font-size:medium; padding-bottom:10px;" align="left">login information</th> </tr> <tr> <td>email:</td> <td> <asp:textbox id="txtemail" cssclass="signup_textbox" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="reqemail" controltovalidate="txtemail" runat="server" display="none" errormessage="email" ></asp:requiredfieldvalidator> <asp:regularexpressionvalidator id="valemail" runat="server" errormessage="invalid email" controltovalidate="txtemail" validationexpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" cssclass="error_msg" display="none" /> </td> </tr> <tr> <td>username:</td> <td> <asp:textbox id="txtuser" cssclass="signup_textbox" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="requser" controltovalidate="txtuser" runat="server" display="none" enableclientscript="true" setfocusonerror="true" errormessage="username" ></asp:requiredfieldvalidator> <asp:regularexpressionvalidator id="regularexpressionvalidator1" display="none" controltovalidate="txtuser" validationexpression="^[\s\s]{3,}$" runat="server" errormessage="username must have @ least 3 characters required."></asp:regularexpressionvalidator> </td> </tr> <tr> <td>password:</td> <td><asp:textbox id="txtpass" cssclass="signup_textbox" runat="server" textmode="password"></asp:textbox> <asp:requiredfieldvalidator id="reqpass" controltovalidate="txtpass" runat="server" display="none" enableclientscript="true" setfocusonerror="true" errormessage="password" ></asp:requiredfieldvalidator> </td> </tr> <tr> <td>confirm password:</td> <td><asp:textbox id="txtconfpass" cssclass="signup_textbox" runat="server" textmode="password"></asp:textbox> <asp:requiredfieldvalidator id="reqconfpass" controltovalidate="txtconfpass" runat="server" display="none" enableclientscript="true" setfocusonerror="true" errormessage="confirm password" ></asp:requiredfieldvalidator> <asp:comparevalidator id="cmppass" runat="server" controltocompare="txtpass" controltovalidate="txtconfpass" errormessage="password must match" cssclass="error_msg" display="none"></asp:comparevalidator> </td> </tr> <tr> <td align="center" colspan="2" style="padding:5px;"><span><asp:button id="btnregister" width="60" runat="server" text="register" cssclass="btnlogin" onclick="btn_addemp" /></span> <span><asp:button id="btnclr" width="60" runat="server" text="cancel" cssclass="btnlogin" onclick="btn_clearform" /></span> </td> </tr> </table> <asp:panel id="errorspanel" runat="server" style="display: none; border-style: solid; border-width: thin; border-color: #ffdbca" width="175px" backcolor="white"> <asp:validationsummary id="valsummary" runat="server" cssclass="error_msg" headertext="you must enter following" displaymode="bulletlist" enableclientscript="true" showsummary="true" /> </asp:panel> </contenttemplate> </asp:updatepanel> </div> </form> </body> </html>
here's code-behind:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.configuration; using system.data; using system.data.sqlclient; using system.globalization; public partial class addemployee : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void btn_clearform(object sender, eventargs e) //to clear form on ispostback or on press of cancel button { if (!ispostback) { clearform(); } } #region public functions public void clearform() { txtname.text = string.empty; txtaddress.text = string.empty; txtdob.text = string.empty; txtdesig.text = string.empty; txtsal.text = string.empty; txtemail.text = string.empty; txtuser.text = string.empty; txtpass.text = string.empty; txtconfpass.text = string.empty; genderlist.items[0].selected = true; genderlist.items[1].selected = false; } #endregion }
there nothing wrong string.empty
. not need condition in btn_clearform
btn_clearform
handler called on postback , put condition if !ispostback
// if (!ispostback) //{ clearform(); // }
your code be
protected void btn_clearform(object sender, eventargs e) { clearform(); }
Comments
Post a Comment