如何在asp.net dotnetnuke c#中更改标签的文本并将其显示在页面上

本文关键字:文本 显示 net asp dotnetnuke 标签 | 更新日期: 2023-09-27 18:25:33

我想在运行时使用标签将错误消息放在页面上。

所以我已经完成了以下类型的代码。

protected void InsertButton_Click(object sender, EventArgs e)
{
    //this.FormView1.Visible = false;
    //LinkButton1.Text = "Update Successful - Add Another Page";
    TextBox txtauthor = this.FormView1.FindControl("txtPageAuthor") as TextBox;
    TextBox txttitle = this.FormView1.FindControl("txtPageTitle") as TextBox;
    TextBox txttag = this.FormView1.FindControl("txtTagName") as TextBox;
    Label lblAuthor = this.FormView1.FindControl("lblAuthor") as Label;
    Label lblTitle = this.FormView1.FindControl("lblTitle") as Label;
    Label lblTag = this.FormView1.FindControl("lblTag") as Label;
    if (string.IsNullOrEmpty(txtauthor.Text))
    {
        lblAuthor.Text = "Author Name must be entered";
        valid = 1;
    }
    if (string.IsNullOrEmpty(txttitle.Text))
    {
        lblTitle.Text = "Page Title must be entered";
        valid = 1;
    }
    if (string.IsNullOrEmpty(txttag.Text))
    {
        lblTag.Text = "Tag Name must be entered";
        valid = 1;
    }
    Page_Load(sender, e);
    this.GridView1.DataBind();
}
// Page Load Event
public void Page_Load(object sender, EventArgs e)
{
    Label lblAuthor = this.FormView1.FindControl("lblAuthor") as Label;
    Label lblTitle = this.FormView1.FindControl("lblTitle") as Label;
    Label lblTag = this.FormView1.FindControl("lblTag") as Label;
    if (Page.IsPostBack)
    {
        //if (valid != 0)
        //{
        lblAuthor.Text = "Author Name must be entered";
        lblTitle.Text = "Page Title must be entered";
        lblTag.Text = "Tag Name must be entered";
        //}
    }
}

下面我提到了我的InsertButton的aspx代码:

<div class="lineheight">
                <p style="display: inline; float: left; width: 100%">
                    <asp:Label ID="lblBlank" runat="server" Text="&nbsp" Width="100px" Style="float: left;"></asp:Label>
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                        Text="Add Page" CssClass="btn" onclick="InsertButton_Click" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                        CommandName="Cancel" Text="Cancel"  CssClass="btn" 
                        onclick="InsertCancelButton_Click" />
    </p>
    </div>

但每次我点击添加按钮,整个页面都会被重定向,标签的默认值,即"(空白)会打印在页面上。

热烈欢迎所有答复。。

提前谢谢。。

如何在asp.net dotnetnuke c#中更改标签的文本并将其显示在页面上

您可以使用ASP验证器。它们很好而且更简单,而且您还可以制作自定义验证器。这是链接