Search code examples
c#asp.netdotnetnuke

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


I want to put error messages on page at runtime using labels.

So I have done following type of code.

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";
        //}
    }
}

below i have mentioned my InsertButton's aspx code:

<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>

But everytime I click the add button the whole page is redirected and the default value of label i.e. "" (blank) is printed on the page.

All answers are warmly welcome..

Thanks in advance..


Solution

  • You can use ASP validators. They are good and more simpler, and also you can make custom validators. Here is the link