如何在选择下拉列表的默认值时清除表数据

时间:2013-05-30 02:59:47

标签: c# asp.net drop-down-menu

每当我选择插入到我的下拉列表中的默认值时,如何清除我的webapp中的表数据?

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

  1. SelectPoliceReportID(默认值)
  2. PoliceReportID123(数据库价值)
  3. PoliceReportID456(数据库价值)
  4. 当我选择DB值时,它们将显示各个DB值,但是当我选择默认值时,先前单击的DB值信息仍将保留在表数据上。

    这是我的页面加载代码,其中将显示policereportID

    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();
            }
    

    在我之前的问题中,我只能通过在数据绑定之后插入以下代码来清除我的gridview中的数据

         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>
    

2 个答案:

答案 0 :(得分: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"标记。之后,您可以通过C#代码

设置yourtable.visible =false;

答案 1 :(得分:0)

如果要清除所有标签文字,请使用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