我在这里遇到的问题是,代码实际上有效,但它只能运行一次。 例如,如果我已经选择了数据库中已有的日期和时间,那么它显示的日期不可用,如果我选择的日期不在数据库中,则显示可用的日期。在此之前没问题,但是在这两个标签都显示一次之后如果我第三次选择不可用的日期,代码就不再执行了。我的意思是,它只停留在之前显示的标签上。我该怎么做这个循环?
前端代码如下:
<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";
}
答案 0 :(得分:1)
您的预期行为是什么?
<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>
根据您的代码,您在下拉列表的txtUsername_TextChanged
事件期间调用OnSelectedIndexChanged
。
这意味着即使您更改日期选择器的日期值,该事件也不会触发。相反,reset_ddl
将在OnTextChanged
事件期间触发。
我们不知道reset_dll
做了什么。如果您希望检查日期可用性的代码触发,则将其放在日期选择器文本框的OnTextChanged
事件上,而不是放在下拉列表中。
答案 1 :(得分:0)
我发现问题所在的地方实际上代码没有问题,它的工作完全问题在我的数据库中