ASP.NET - 如果条件与HTML代码内联

时间:2012-10-17 16:02:29

标签: asp.net

  

可能重复:
  Conditional Logic in ASP.net page

我是ASP.NET新手。

假设我有几个表,有些表我使用TD标签将GridView和DetailsView放在一边。请注意,这些DetailsView和GridView不相关,只需单击另一次刷新,只显示记录。在DetailsView中,我还有添加,编辑和删除选项。示例如下。

<form name="form1" runat="server" class="FormDetail">
<table width="80%" align="center" >
    <tr>
        <td width="20%"></td>
        <td width="80%" class="PageTitle"></td>
        <td width="20%">
            <asp:Button runat="server" CommandName="AddNewProposal" ID="AddNewProposal" Text="Create Note" 
            href="ProposalCreateNote.aspx?ProposalID=ProposalID=" 
            title="Create Note" class="button3"/>           
        </td>
    </tr>
</table>   

    <div style="width:40%; margin-right: auto; margin-left: auto;">
        <asp:DetailsView AutoGenerateRows="False" DataKeyNames="ProposedID" DataSourceID="SqlDataSource2"
            HeaderText="Proposal Detail View" ID="DetailsView1" 
            runat="server" Width="100%" validateRequest="false" 
            CssClass="products"
            HeaderStyle-CssClass="header"
            FieldHeaderStyle-CssClass="fieldHeader"
            AlternatingRowStyle-CssClass="alternating"
            RowStyle
            CommandRowStyle-CssClass="command"
            PagerStyle-CssClass="pager"
            >

        <Fields>
            <asp:TemplateField HeaderText="ProposedID" SortExpression="Name" > 
                <ItemTemplate >
                    <asp:Label ID="ProposalID" runat="Server" style="text-align:left;" 
                     Text='<%# Eval("ProposedID")%>' />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField DataField="MDFName" HeaderText="MDF Name" SortExpression="MDFName" />  

                <asp:TemplateField HeaderText="VendorName">
                    <ItemTemplate>
                        <asp:Label ID="lblVendorName" runat="Server" style="text-align:left;" Text='<%# Eval("VendorName")%>'/>
                    </ItemTemplate>
                    <InsertItemTemplate>
                        <asp:DropDownList id="insertVendorName" datasourceid="VendorSqlDataSource"
                            datatextfield="VendorName" DataValueField="VendorID" 
                            SelectedValue='<%# Bind("VendorID") %>'
                            OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged" 
                            runat="server" AutoPostBack="true">
                            <asp:ListItem Text="Select" Value="-1"></asp:ListItem>  
                        </asp:DropDownList>

                        <asp:SqlDataSource ID="VendorSqlDataSource"  ConnectionString="<%$Connectionstrings:ConnectionString%>"
                            SelectCommand="SELECT VendorID, VendorName from MDF_Vendor"  runat="server">
                        </asp:SqlDataSource>
                    </InsertItemTemplate>

                    <EditItemTemplate>
                        <asp:DropDownList id="updateVendorName" datasourceid="VendorSqlDataSource"  AutoPostBack="true"
                        datatextfield="VendorName"  DataValueField="VendorID"  
                        SelectedValue='<%# Bind("VendorID") %>'     
                        runat="server" 
                        OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged"  />                       
                        <asp:SqlDataSource ID="VendorSqlDataSource"  ConnectionString="<%$Connectionstrings:ConnectionString%>"
                            SelectCommand="SELECT VendorID, VendorName from MDF_Vendor"  runat="server">
                        </asp:SqlDataSource>
                    </EditItemTemplate>
                </asp:TemplateField>

        </Fields>

        </asp:DetailsView>                   
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT p.*
                        , (SELECT VendorName FROM MDF_Vendor WHERE VendorID = p.VendorID) AS VendorName
                        , (SELECT VendorBUName FROM MDF_VendorBU WHERE VendorBUID = p.VendorBUID) AS VendorBUName
                        FROM dbo.MDF_Proposed p Where p.ProposedID = @ProposedID"
            UpdateCommand="UPDATE [MDF_Proposed] SET [VendorID] = @VendorID
                        , [VendorBUID] = @VendorBUID
                        , [MDFSummary] = @MDFSummary
                        , [MDFAmount] = @MDFAmount
                        , [BeginDate] = @BeginDate
                        , [EndDate] = @EndDate
                        WHERE ProposedID = @ProposedID"
            InsertCommand = "INSERT INTO [MDF_Proposed] ([VendorID], [VendorBUID], [MDFName], [BeginDate], [EndDate], [CreatedBy]) 
                             VALUES (@VendorID, @VendorBUID, @MDFName, @BeginDate, @EndDate, '1234') "
            DeleteCommand = "DELETE  [MDF_Proposed]  WHERE ProposedID = @ProposedID"
        >   
            <SelectParameters>
                    <asp:QueryStringParameter Name="ProposedID" QueryStringField="ProposedID"  runat="server" />
            </SelectParameters>                        
            <UpdateParameters> 
            <asp:Parameter Name="ProposedID" Type="Int32" />
            </UpdateParameters>                    
        </asp:SqlDataSource>
    </div>

<hr align="center" width="96%" size="1" color="darkblue" /> 

<!-- //////////////////// NEED TO HIDE THI TABLE WHERE QueryString Mode=Insert ///////////////////// -->
<table  align="center"  width="96%" id="table2" border="0">
    <tr>
         <td class="center TitleSmall">Notes</td>
     </tr>
    <tr>
        <td width="48%">
    <asp:GridView ID="GridView_Note" runat="server"
    AutoGenerateColumns="False" 
        DataKeyNames="ProposedNoteID"
        DataSourceID="dsProposalNote" 


        ShowFooter="true"
        CssClass="mGrid"
        PagerStyle-CssClass="pgr"
        AlternatingRowStyle-CssClass="alt"                      
    >
    <AlternatingRowStyle ForeColor="Black"  BackColor="#F7F6F3"  VerticalAlign="top" />
    <HeaderStyle BackColor="#B5DAFF" Font-Bold="True" HorizontalAlign="center" />
    <PagerStyle BackColor="Gold" ForeColor="White" HorizontalAlign="Center" />    
    <FooterStyle Font-Bold="True" ForeColor="White"  BackColor="" />
        <Columns>

            <asp:BoundField DataField="ProposedNoteID" HeaderText="ID"  ItemStyle-Width="5%" />
            <asp:TemplateField ItemStyle-Width="70%">
                <ItemTemplate>

                    <asp:Label runat="server" DataField="Note" HeaderText="Note" Text='<%#Eval("Note") %>' />

                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CreatedBy" HeaderText="Created By"  ItemStyle-Width="13%"/>
            <asp:BoundField DataField="CreatedDate" HeaderText="Created Date"  ItemStyle-Width="12%" DataFormatString="{0:dd/MM/yyyy}"/>

            <asp:TemplateField HeaderText="Delete"> 
                <ItemTemplate> 

                    <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" 

                  OnClientClick="return confirm('Are you certain you want to delete this?');"/> 

                </ItemTemplate> 
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
        <asp:sqldatasource ID="dsProposalNote" runat="server"  ConnectionString="<%$ConnectionStrings:ConnectionString %>"
        SelectCommand="Select * from MDF_ProposedNote where ProposedID = @ProposedID"    
        DeleteCommand="DELETE MDF_ProposedNote where ProposedNoteID = @ProposedNoteID"
            >
        <SelectParameters>
            <asp:QueryStringParameter Name="ProposedID" QueryStringField="ProposedID"  runat="server" />
        </SelectParameters>

    </asp:sqldatasource>

    </td>
        <td></td>
        <td width="48%"></td>
    </tr>
</table>
</form>

我想做的是,如果QueryString在URL上发送插入模式(Page1.aspx?mode = insert)。在页面加载时,我希望DetailsView1处于插入模式,并隐藏Table2,其中包含GridView(GridView_Note)和其他HTML代码。

如果这是经典的ASP,我相信我可以根据Page.asp?mode = insert的条件将queryString值存储在变量和do和IF语句之前显示。

如何在ASP.NET中完成此任务?如何存储变量并将其与HTML代码一起内联?如果我困惑你,请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将表包装在asp:placeholder或asp:面板中,然后在Page_Load事件中查询QueryString集合中的“mode”键值对,并相应地隐藏或显示。

<强> HTML

<asp:Panel id="pnlNotes" runat="server">
   ... Table and gridView
</asp:Panel>

代码背后

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
         if (Request.QueryString["mode"] != null && Request.QueryString["mode"] == "Insert")
         {
             pnlNotes.Visible = false;
         }
    }
}