简体   繁体   中英

Set text in <p> embedded in asp:Panel from code behind c# asp.net

I am trying to add text from my code behind file based on a URL query string. I have an asp:Label that works fine, but I need to add text to the body of an asp:Panel and that is causing my problem. Here is my code:

ASPX file:

<asp:Panel ID="PanelAboutUs" class="panel panel-primary" runat="server">
    <asp:Panel ID="PanelAboutUsHeader" class="panel-heading panel-success" runat="server">
        <asp:Label ID="LabelAboutUsHeader" runat="server"></asp:Label>
    </asp:Panel>

    <asp:Panel ID="PanelAboutUsBody" class="panel-body" runat="server">    
        <asp:TextBox ID="TextAboutUsBody" runat="server"></asp:TextBox>    
    </asp:Panel>

    <div class="divide-30"></div>       

    <div class="panel-footer">
        <div class="form-group">
            <asp:LinkButton runat="server" Text="Ok" CausesValidation="True" ID="CancelRequest" CssClass="btn btn-lg btn-block btn-success"  OnClick="Ok_OnClick"></asp:LinkButton>
        </div>
    </div>

</asp:Panel>

And here is my code behind:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!string.IsNullOrEmpty(Request.QueryString["result"]))
            result = int.Parse(Request.QueryString["result"]);
        else Response.Redirect("Default.aspx");
    }
    catch
    {
        Response.Redirect("Default.aspx");
    }

    if (result == 0)
    {
        LabelAboutUsHeader.Text = "Cancelled!";
        TextAboutUsBody.Text = "Before you can schedule services you will need to enter a preferred payment method. You can add this payment method the next time you successfully login to your account.";
    }
    else
    {
        LabelAboutUsHeader.Text = "Success!";
        TextAboutUsBody.Text = "Your payment information has been successfully added. To access the system you must first validate your account via the email sent during registration.";
    }
}

The problem is with my asp:TextBox . Originally I had the text hard coded through using <p> instead of asp:TextBox and everything worked fine. But I could not get my <p> to update with dynamic text so I changed to asp:TextBox . The formatting of the asp:TextBox is not working right and only part of my message is actually displaying.

So given all that explanation, my question is how can I make my original code work using <p> but updating from code behind file. Here is my original code.

<asp:Panel ID="PanelAboutUsHeader" class="panel-heading panel-success" runat="server">
    <asp:Label ID="LabelAboutUsHeader" runat="server"></asp:Label>
</asp:Panel>

<asp:Panel ID="PanelAboutUsBody" class="panel-body" runat="server">    
    <p>[Dynamic text from code behind should go here.]</p>    
</asp:Panel>

The asp:Panel control does not have a text property. So this line: TextAboutUsBody.Text = "Your payment ..." is not valid.

Here is one way to do this:

In your html, create an asp:Literal control.

<asp:Panel ID="PanelAboutUsBody" class="panel-body" runat="server">    
    <p><asp:Literal runat="server" Id="litAboutUsBody"></asp:Literal></p>    
</asp:Panel>

And set your literal as such in your code behind:

litAboutUsBody.Text = "Your payment inform ...";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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