c# - Alert on button click -
i have 4 textboxes , need check null values. if of textbox value has null values on button click need show alert 'please enter values'.
this code disable if textbox value empty.
aspx:
<tr style="height: 40px;"> <td> <asp:textbox id="txt1" runat="server"> </asp:textbox> </td> <td> <asp:textbox id="txt2" runat="server"> </asp:textbox> </td> <td> <asp:textbox id="txt3" runat="server"> </asp:textbox> </td> </tr> <tr> <td> <asp:button id="button1" runat="server" text="add" cssclass="button" width="50px" onclick="button1_click" /></td> </tr>
cs:
protected void button1_click(object sender, eventargs e) { if (string.isnullorwhitespace(txt1.text) || string.isnullorwhitespace(txt2.text) || string.isnullorwhitespace(txt3.text)) { string message = "textbox can empty, please enter value"; string script = string.format("alert('{0}');", message); this.page.clientscript.registerstartupscript(this.page.gettype(), "msgbox", script, true); } if (txt1.text != "" && txt2.text != "" && txt3.text != "" && ddlcategory.text != "--select--") { dataset ds = new dataset(); using (sqlconnection connection = new sqlconnection(str)) { sqlcommand command = new sqlcommand(); command.connection = connection; string strquery = "select * product code='metal' , id='" + txt1.text + "'"; sqlcommand cmd = new sqlcommand(strquery, connection); sqldataadapter da = new sqldataadapter(cmd); da.fill(ds); } if (ds.tables[0].rows.count > 0) { clientscript.registerstartupscript(this.gettype(),"key", "<script type='text/javascript'>window.onload = function(){alert('product exists.');return false;}</script>"); } else { sqlconnection conn = new sqlconnection(str); conn.open(); sqlcommand cmd = new sqlcommand("products", conn); cmd.parameters.add("@id", sqldbtype.varchar, 30).value = txt1.text; cmd.parameters.add("@name", sqldbtype.varchar, 50).value = txt2.text; cmd.parameters.add("@email", sqldbtype.varchar, 30).value = txt3.text; cmd.parameters.add("@category", sqldbtype.varchar, 20).value = ddlcategory.text; cmd.parameters.add("@id", sqldbtype.int).value = 1; cmd.commandtype = commandtype.storedprocedure; cmd.executenonquery(); conn.close(); conn.dispose(); clientscript.registerstartupscript(this.gettype(), "key", "<script type='text/javascript'>window.onload = function(){alert('product created successfully.');return false;}</script>"); } } }
anyone suggest me on this.
try below
if (string.isnullorwhitespace(txt1.text) || string.isnullorwhitespace(txt2.text) || string.isnullorwhitespace(txt3.text) || string.isnullorwhitespace(txt4.text)) { string message = "please enter values"; string script = string.format("alert('{0}');", message); this.page.clientscript.registerstartupscript(this.page.gettype(), "msgbox", script, true); return; }
Comments
Post a Comment