【发布时间】:2016-07-20 07:15:34
【问题描述】:
我在这里遇到的问题是,代码实际上可以工作,但它只能工作一次。 例如,如果我选择了一个已经在数据库中的日期和时间,它会按预期显示日期不可用,如果我选择一个不在数据库中的日期,它会显示可用的日期。在那之前没问题,但是在这两个标签都显示一次之后,如果我第三次选择不可用的日期,则代码不会再次执行。我的意思是,它只是停留在之前显示的标签上。我怎样才能在这个上做一个循环?
前端代码如下:
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptmanager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript" language="javascript">
Sys.Application.add_load(jScript);
</script>
<div id="dvRecWed" style="display: none">
<asp:UpdatePanel ID="PnlUsrDetails" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtReceptionDate" class="form-control" autocomplete="off" CssClass="datepicker" placeholder="mm/dd/yy" AutoPostBack="true" OnTextChanged="reset_ddl" runat="server"></asp:TextBox>
<asp:DropDownList AutoPostBack="true" ID="ddlReceptionTime" runat="server" OnSelectedIndexChanged="txtUsername_TextChanged">
<asp:ListItem Value="-1">--Select--</asp:ListItem>
<asp:ListItem Value="Forenoon">Forenoon</asp:ListItem>
<asp:ListItem Value="Afternoon">Afternoon</asp:ListItem>
<asp:ListItem Value="FullDay">Full Day</asp:ListItem>
</asp:DropDownList>
<div id="checkdate" class="checkdate" runat="server" visible="false">
<asp:Image ID="imgstatus" runat="server" Width="17px" Height="17px" />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
后端代码如下
protected void txtUsername_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ddlReceptionTime.Text))
{
string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
OleDbConnection connection = new OleDbConnection(connString);
// SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName=@Name", con);
string selectQuery = "SELECT FunctionTime FROM function WHERE FunctionDate=@FunctionDate AND FunctionTime='FullDay'";
connection.Open();
OleDbCommand command = new OleDbCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@FunctionDate", txtReceptionDate.Text);
command.Parameters.AddWithValue("@FunctionTime", ddlReceptionTime.Text);
OleDbDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/cross.png";
lblStatus.Text = "Date Unavailable";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
else
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/check.png";
lblStatus.Text = "Date available";
}
}
else if (!string.IsNullOrEmpty(ddlReceptionTime.Text))
{
string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString;
OleDbConnection connection = new OleDbConnection(connString);
// SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName=@Name", con);
string selectQuery = "SELECT FunctionTime FROM function WHERE FunctionDate=@FunctionDate AND FunctionTime=@FunctionTime";
connection.Open();
OleDbCommand command = new OleDbCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@FunctionDate", txtReceptionDate.Text);
command.Parameters.AddWithValue("@FunctionTime", ddlReceptionTime.Text);
OleDbDataReader dr = command.ExecuteReader();
if (dr.HasRows)
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/cross.png";
lblStatus.Text = "Date Unavailable";
lblStatus.ForeColor= System.Drawing.Color.Red;
}
else
{
checkdate.Visible = true;
imgstatus.ImageUrl = "~/img/check.png";
lblStatus.Text = "Date Available";
}
}
else
{
checkdate.Visible = false;
}
}
protected void reset_ddl(object sender, EventArgs e)
{
ddlReceptionTime.SelectedValue = "-1";
}
【问题讨论】:
-
点不是群体动物。您可以在适当的地方设置它们。不仅是 3 人一组。
-
我认为你的帖子有问题,为什么有 2 次相同的代码块?
-
不,区别在于第二个查询参数。他可以在那里放一个 if 块,但他选择重复整个 shebang。
-
我如何做 if 块?我是新手。所以我对此不太了解。所以我用我所拥有的一点知识做了我能做的。谢谢。