我在SqlDataSource1对象的Select语句如下:
SELECT COUNT(*) AS 数量 FROM 口令表 WHERE (操作员 = @操作员) "
将参数@操作员与this.textbox1相关联后
用DetailsView1显示结果的
如果textbox1不为空时,查询有结果(也许是0),DetailsView1显示
如果textbox1为空时,查询结果为空,DetailsView1不显示结果
而我希望DetailsView1显示结果为0;
请问怎么办?
我用的是vs2005
我的代码如下:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default2.aspx.cs " Inherits= "Default2 " %>
<!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 runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
输入: <asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox> <br />
<asp:Button ID= "Button1 " runat= "server " Text= "Button " /> <br />
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " ConnectionString= " <%$ ConnectionStrings:HFC2ConnectionString %> "
SelectCommand= "SELECT COUNT(*) AS 数量 FROM 口令表 WHERE (操作员 = @操作员) ">
<SelectParameters>
<asp:ControlParameter ControlID= "TextBox1 " DefaultValue= " " Name= "操作员 " PropertyName= "Text " />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:DetailsView ID= "DetailsView1 " runat= "server " DataSourceID= "SqlDataSource1 " Height= "50px "
Width= "125px ">
</asp:DetailsView>
</div>
</form>
</body>
</html>
------解决方案--------------------------------------------------------
觉得问题出现在你的ControlID= "TextBox1 " DefaultValue= " " ,当你TextBox1无输入内容时本身就应该做个保护,因为这样的查询无意义。