2013-05-30 80 views
0

如何在我的web應用程序中清除我的表格數據時,只要我選擇了我已插入到我的下拉列表中的默認值?如何在選擇下拉列表的默認值時清除表格數據

我有3個選項

  1. SelectPoliceReportID(默認值)
  2. PoliceReportID123(數據庫值)
  3. PoliceReportID456(數據庫值)

當我選擇一個數據庫中的下拉列表框值時,它們將顯示單個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 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> 
+0

這裏設置yourtable.visible =false;'表data'是C#'datatable'或數據庫'datatable' – Rahul

+0

如果它是一個C#'DataTable'然後使用'table.Clear();' – Rahul

回答

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#代碼

+0

嘿謝謝你的回覆。通過用「」替換String.Empty來管理清除它。很抱歉改變你的代碼,但在我看來,這對我來說更容易。但是,謝謝你的幫助。 –

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

+0

對不起。我意識到我遺漏了一些非常重要的東西。我沒有使用asp:表。我添加了我自己的html表格。所以我想知道該方法是否可以類似工作。 –

+0

如果您正在使用'Html'表,然後通過循環'Empty'控制'Label'的值,就像我在選擇默認值時一樣。 – Rahul

+0

感謝您的回覆。我找到了一個合適的答案,我已勾選它:D –

相關問題