0

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

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

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>

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

热烈欢迎所有答案..

提前致谢..

4

1 回答 1

0

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

于 2013-08-01T10:12:52.187 回答