简体   繁体   English

如何使用C#隐藏和显示html模板表?

[英]how to Hide and Show a table of html Template using C#?

I am using html template for Email and i want to hide its one table at some condition by code behind, how can i do it, please suggest me. 我正在使用电子邮件的html模板,我想在某些情况下通过代码隐藏它的一个表,我怎么能这样做,请建议我。 I am wondering how can i do it, i am not able to find any way, please suggest me any way to come out from this, thanks in advance. 我想知道我怎么能这样做,我无法找到任何办法,请建议我从这里出来的任何方式,谢谢你提前。 My code is : 我的代码是:

if (File.Exists(appPath + "\\rptBookDetails.Template"))
        {
            FinalOutPut = File.ReadAllText(appPath + "\\rptBookDetails.Template");

            FinalOutPut = FinalOutPut.Replace("{AccessionNo}", dsBookDetails.Tables[0].Rows[0]["AccessionNo"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Title}", dsBookDetails.Tables[0].Rows[0]["TITLE"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Edition}", dsBookDetails.Tables[0].Rows[0]["EDITION"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Volume}", dsBookDetails.Tables[0].Rows[0]["Volume"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Authors}", dsBookDetails.Tables[0].Rows[0]["Authors"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Pages}", dsBookDetails.Tables[0].Rows[0]["PAGES"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Publication}", dsBookDetails.Tables[0].Rows[0]["PUB_PLACE"].ToString());
            FinalOutPut = FinalOutPut.Replace("{PublicationYear}", dsBookDetails.Tables[0].Rows[0]["YEAR_O_PUB"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Price}", dsBookDetails.Tables[0].Rows[0]["COST"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Supplier}", dsBookDetails.Tables[0].Rows[0]["AccessionNo"].ToString());
            FinalOutPut = FinalOutPut.Replace("{BillNo}", dsBookDetails.Tables[0].Rows[0]["BILL_NO"].ToString());
            FinalOutPut = FinalOutPut.Replace("{BillDate}", dsBookDetails.Tables[0].Rows[0]["DT_O_BILL"].ToString());
            FinalOutPut = FinalOutPut.Replace("{Grant Source}", dsBookDetails.Tables[0].Rows[0]["GRANT_SR"].ToString());

        }

What i want to do is, in case i dont have books detail in database then i dont want to show the table in HTML out put. 我想要做的是,如果我在数据库中没有书籍细节,那么我不想在HTML输出中显示表格。 please help me 请帮我

<table width="84%" border="1" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td width="30%" align="left" valign="top">
                    Accession No.
                </td>
                <td width="2%">
                    :
                </td>
                <td width="68%" align="left" valign="top">
                    B10
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Title
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Title}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Edition
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Edition}
                </td>
            </tr>
        <tr>
            <td align="left" valign="top">
                    Volume
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Volume}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Authors
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Authors}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Pages
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Pages}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td align="left" valign="top">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Publication Place
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Publication}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Publication Year
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {PublicationYear}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td align="left" valign="top">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Price
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Price}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Source of Purchase
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Supplier}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Bill No.
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {BillNo}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Bill Date
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {BillDate}
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    Grant Source
                </td>
                <td>
                    :
                </td>
                <td align="left" valign="top">
                    {Grant Source}
                </td>
            </tr>
    </table>

<table width="97%" id="tblHistry" border="1" align="center" cellpadding="2" cellspacing="0">
            <tr>
                <th>
                    Sr
                </th>
                <th>
                    Trans. Id
                </th>
                <th>
                    Borrower Id
                </th>
                <th>
                    Issue Date
                </th>
                <th>
                    Expected Return Date
                </th>
                <th>
                    Actual Return Date
                </th>
                <th>
                    Fine
                </th>
                <th>
                    Fine Status
                </th>
                <th>
                    Issue Remarks
                </th>
                <th>
                    Return Remarks
                </th>
            </tr>
            {HistoryTable}
        </table>

I want to Hide and Show tblHistry on condition, please help me. 我想在条件下隐藏和显示tblHistry,请帮助我。

use asp panel control and place your table inside that panel... now in code behind file in perticular events code write 使用asp面板控件并将您的表放在该面板中...现在在相关事件代码写入的代码隐藏文件中

Panelname.visible=true;

OR 要么

Panelname.visible=false;

Considering that your table is dynamically created and is stored in a String and is not a Control. 考虑到您的表是动态创建的并存储在String中而不是Control。

There are two ways to achive what you need; 有两种方法可以满足您的需求;

Way 1 方式1

Amend the template and include another tag to be replaced style="{CSSStyle}" in the table table you want to hide/show. 修改模板并在要隐藏/显示的表格表中包含要替换的另一个标记style =“{CSSStyle}”

<table width="97%" id="tblHistry" style="{CSSStyle}" border="1" align="center" cellpadding="2" cellspacing="0">

In the code behind do the following to show the table; 在后面的代码中执行以下操作来显示表;

 FinalOutPut = FinalOutPut.Replace("{CSSStyle}", "");

while the below to hide it; 而下面隐藏它;

  FinalOutPut = FinalOutPut.Replace("{CSSStyle}", "display:none");

Way 2 方式2

Close the tblHistry table in 2 custom tags like below; 关闭2个自定义标签中的tblHistry表,如下所示;

[HistoryTable]
   <table width="97%" id="tblHistry" cellpadding="2" cellspacing="0">
     ....
   </table>
[/HistoryTable]

In the code behind code use the below code to hide and show the table; 在代码背后的代码中使用下面的代码来隐藏和显示表格;

string strHistoryTableOpeningTag = "[HistoryTable]";
string strHistoryTableClosingTag = "[/HistoryTable]";
int intStartPos = 0;
int intEndPos = 0;

if (blnWantToHide == True) {
  //Remove history table
  intStartPos = FinalOutPut.IndexOf(strHistoryTableOpeningTag);
  intEndPos = FinalOutPut.IndexOf(strHistoryTableClosingTag) + strHistoryTableClosingTag.Length;

 FinalOutPut = FinalOutPut.Remove(intStartPos, intEndPos - intStartPos);
} else {
  //Remove unwanted tags
  FinalOutPut = FinalOutPut.Replace(strHistoryTableOpeningTag, "");
  FinalOutPut = FinalOutPut.Replace(strHistoryTableClosingTag, "");
}

Personally I'd go for Way 2 because it's neater and you won't have unwanted hidden HTML. 就个人而言,我会选择Way 2,因为它更整洁,你不会有不需要的隐藏HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM