繁体   English   中英

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

[英]how to change text of label in asp.net dotnetnuke c# and display it on page?

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

因此,我完成了以下类型的代码。

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验证器 它们既好又简单,您也可以创建自定义验证器。 这是链接

暂无
暂无

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

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