0

每当我选择已插入下拉列表的默认值时,如何清除我的 web 应用程序中的表格数据?

我有一个带有 3 个选项的下拉列表框

  1. SelectPoliceReportID(默认值)
  2. PoliceReportID123(数据库值)
  3. PoliceReportID456(数据库值)

当我选择 DB 值时,它们会显示单独的 DB 值,但是当我选择默认值时,之前单击的 DB 值信息仍将保留在表数据中。

这是我的页面加载代码,将显示policyreportID

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 &amp; 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 &amp; 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&nbsp; 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>
4

2 回答 2

1

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"tag 添加到您的表中。之后,您可以yourtable.visible =false;通过 C# 代码设置

于 2013-05-30T03:34:57.130 回答
0

如果要清除所有标签文本,请使用SelectedIndexlike

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

于 2013-05-30T03:33:48.530 回答