如何在选择下拉列表的默认值时清除表数据
本文关键字:清除 数据 默认值 选择 下拉列表 | 更新日期: 2023-09-27 18:16:13
每当我选择了一个插入到我的下拉列表中的默认值时,我如何在我的webapp中清除我的表数据?
我有一个下拉列表框,有3个选项
- 选择PoliceReportID(默认值(
- PoliceReportID123(数据库值(
- PoliceReportID456(数据库值(
当我选择DB值时,它们将显示单独的DB值,但是当我选择默认值时,以前单击的DB值信息仍将保留在表数据上。
这是我的页面加载代码,其中将显示策略端口ID
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");
SqlCommand cm = new SqlCommand("Select pr.policereportid from PoliceReport pr, MemberReport mr where pr.memberreportid=mr.memberreportid and mr.caseprogress='completed'", con);
con.Open();
SqlDataReader dr;
dr = cm.ExecuteReader();
while (dr.Read())
{
DDLCase.Items.Add(dr["policereportid"].ToString());
}
dr.Close();
con.Close();
}
在我之前的问题中,我只能通过在数据绑定后插入以下代码来从网格视图中清除我的数据
DDLCase.Items.Clear();
DDLCase.DataSource = ds2;
DDLCase.DataTextField = "memberreportid";
DDLCase.DataValueField = "memberreportid";
DDLCase.DataBind();
DDLCase.Items.Insert(0, new ListItem("Select Member Report ID", ""));
DDLCase.SelectedIndex = 0;
这是我的下拉列表
protected void DDLCase_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");
con.Open();
SqlCommand cm = new SqlCommand("Select lro.fullname, lro.contact, mr.typeofcrime, mr.location,mr.crdatetime, pr.policeid, pr.prdatetime, pr.policereport, pr.image1, mr.citizenreport from MemberReport mr, PoliceReport pr, LoginRegisterOthers lro where pr.policereportid = '" + DDLCase.SelectedValue + "' and mr.memberreportid=pr.memberreportid and lro.username=mr.username and mr.caseprogress='completed'", con);
SqlDataReader dr;
dr = cm.ExecuteReader();
if (dr.Read())
{
lblFullName.Text = dr["fullname"].ToString();
lblContact.Text = dr["contact"].ToString();
lblTOC.Text = dr["typeofcrime"].ToString();
lblLocation.Text = dr["location"].ToString();
lblCRDT.Text = dr["crdatetime"].ToString();
lblPicture.Text = dr["image1"].ToString();
lblAssign.Text = dr["policeid"].ToString();
lblPRDT.Text = dr["prdatetime"].ToString();
lblCR.Text = dr["citizenreport"].ToString();
lblPR.Text = dr["policereport"].ToString();
}
con.Close();
}
我的表的源代码。我没有使用asp:table。我用程序将该表添加到源代码中。
<table style="width: 100%; height: 576px;">
<tr>
<th style="width: 595px; height: 49px;">Full Name :</th>
<td style="width: 533px; height: 49px; text-align: left;">
<asp:Label ID="lblFullName" runat="server" Text=""></asp:Label>
</td>
<th style="height: 49px; width: 134px">Contact :</th>
<td style="width: 185px; height: 49px; text-align: left;">
<asp:Label ID="lblContact" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<th style="width: 595px">Location :</th>
<td style="width: 533px; height: 49px; text-align: left;">
<asp:Label ID="lblLocation" runat="server" Text=""></asp:Label>
</td>
<th style="width: 134px">Type of Crime :</th>
<td style="width: 185px; height: 49px; text-align: left;">
<asp:Label ID="lblTOC" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<th style="width: 595px">Picture : </th>
<td style="width: 533px; height: 49px; text-align: left;">
<asp:Label ID="lblPicture" runat="server" Text=""></asp:Label>
</td>
<th style="width: 134px">Citizen Report Date & Time :</th>
<td style="width: 185px; height: 49px; text-align: left;">
<asp:Label ID="lblCRDT" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<th style="width: 595px">AssignTo :</th>
<td style="width: 533px; height: 49px; text-align: left;">
<asp:Label ID="lblAssign" runat="server" Text=""></asp:Label>
</td>
<th style="width: 134px">Police Report Date & Time :</th>
<td style="width: 185px; height: 49px; text-align: left;">
<asp:Label ID="lblPRDT" runat="server" Text=""></asp:Label>
</td>
</tr>
<tr>
<th style="width: 595px; height: 100px;">Citizen Report :</th>
<td colspan="4" style="height: 100px" text-align:"left">
<asp:Label ID="lblCR" runat="server" Text="" style="display: block; text-align: left;"></asp:Label>
</td>
</tr>
<tr>
<th style="width: 595px; height: 135px;">Police Report :</th>
<td colspan="4" style="height: 135px" text-align: "left">
<asp:Label ID="lblPR" runat="server" Text="" style="display: block; text-align: left;"></asp:Label>
</td>
</tr>
<tr>
<th style="width: 595px; height: 135px;">Official Report :</th>
<td colspan="4" style="height: 135px">
<asp:TextBox ID="tbofficial" runat="server" Height="121px" TextMode="MultiLine" Width="878px" ></asp:TextBox>
<br />
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</td>
</tr>
</table>
在DDLCase_SelectedIndexChanged
中低于
if(DDLCase.SelectedIndex == 0) // this is the default value
{
lblFullName.Text = String.Empty;
lblContact.Text = String.Empty;
// clear all the textboxes
}else
{
// your code
}
如果选择了默认值,也可以隐藏html表。但是您需要将id和runat="server"
标记添加到表中。之后,您可以通过C#代码设置yourtable.visible =false;
如果要清除所有标签文本,请使用类似的SelectedIndex
if(DDLCase.SelectedIndex == 0) // default value
{
foreach (Control ctl in this.Controls)
{
if (ctl is Label)
{
ctl.Text = "";
}
}
}
else
{
// code
}
但如果你想清除DataTable
,你必须使用
DataTable.Clear();
方法(System.Data(来清除数据表。
MSDN