我有四个文本框,我需要检查空值。如果任何文本框值具有空值,则在按钮单击时,我需要显示“请输入值”之类的提醒。
如果文本框值为空,则此代码将禁用。
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 be empty, please enter a 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 * from product where code='Metal' and 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 Already 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>");
}
}
}
任何人都建议我这个。
答案 0 :(得分:2)
尝试以下
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;
}
答案 1 :(得分:1)
您可以使用JavaScript代码在客户端执行此操作
function verifyValues()
{
var txtBox1,txtBox2,txtBox3,txtBox4;
txtBox1=document.getElementByID("<%= myTextBox1.ClientID %>"):
txtBox2=document.getElementByID("<%= myTextBox2.ClientID %>"):
txtBox3=document.getElementByID("<%= myTextBox3.ClientID %>"):
txtBox4=document.getElementByID("<%= myTextBox4.ClientID %>"):
if(txtBox1.value=="" || txtBox2.value==""||txtBox3.value==""
|| txtBox4.value=="")
{
alert("Please enter any one of the value");
return false;
}
return true;
}
然后你的服务器端标记应该用return
语句
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return verifyValues();"
Text="Click" />
如果要同时检查客户端和服务器端,请在服务器端验证中包含@Damith应答
答案 2 :(得分:0)
我会改用 string.IsNullOrEmpty(),然后执行以下操作。
if (string.IsNullOrEmpty(txt1.Text)) {
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
"err_msg",
"alert('Please enter values!)');",
true);
}
答案 3 :(得分:0)
尝试此操作而不是代码,并应在显示警告消息后添加return false
if (string.IsNullOrEmpty(txt1.Text) ||string.IsNullOrEmpty( txt2.Text) || string.IsNullOrEmpty(txt3.Text) || string.IsNullOrEmpty(txt4.Text))
{
string script = "<script type=\"text/javascript\">alert('Please enter values');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
Button1.Enabled = false;
return false;
}
else
{
Button1.Enabled = true;
}
}
或
if (string.IsNullOrEmpty(txt1.Text) ||string.IsNullOrEmpty( txt2.Text) || string.IsNullOrEmpty(txt3.Text) || string.IsNullOrEmpty(txt4.Text))
{
string script = "alert('Please enter values');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
Button1.Enabled = false;
return false;
}
else
{
Button1.Enabled = true;
}
}
答案 4 :(得分:0)
试试这个
if (txt1.Text == "" || txt2.Text == "" || txt3.Text == "" || txt4.Text == "")
{
ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "scrip1", "alert('Enter All fields');", true);
Button1.Enabled = false;
}
else
{
Button1.Enabled = true;
}
答案 5 :(得分:0)
在所有文本框上应用必填字段验证并为所有文本框提供组名称,对于单击哪个验证应该有效的按钮,所有文本框和按钮的组名称应相同。
答案 6 :(得分:0)
这正是您正在寻找的:(多控制的服务器端验证)
http://www.codeproject.com/Tips/722385/Single-Server-Side-Validator-for-Multiple-Controls
答案 7 :(得分:0)
您必须在客户端进行此验证,以提高您的性能
如下
function verifyValues()
{
var txtBox1,txtBox2,txtBox3,txtBox4;
txtBox1=document.getElementByID("txt2"):
txtBox2=document.getElementByID("txt2"):
txtBox3=document.getElementByID("txt2"):
txtBox4=document.getElementByID("txt2"):
if(txtBox1.value=="" || txtBox2.value==""||txtBox3.value==""
|| txtBox4.value=="")
{
alert("Please enter any one of the value");
return false;
}
return true;
}