1

I have the asp.net application where I am having the editable grid view with the edit,Delete,Add options. this grid having as usual Template fields. I have also a static Class that having static string variables. I want to keep these static variable's value as Header text of Template Field. So I have imported my constant class's namespace by :

<%@ Import Namespace="ConstantManagerNamespace" %>

Then I tried for same column:

1. <asp:TemplateField HeaderText=<%=ConstantManager.Name%>>

2. <asp:TemplateField HeaderText='<%=ConstantManager.Name%>'>

3. <asp:TemplateField HeaderText=<% ConstantManager.Name %>>

4. <asp:TemplateField HeaderText='<% ConstantManager.Name%>'>

  all probable syntax to access my constant variable value but

I got Parser error:

Literal content ('<asp:TemplateField HeaderText=') is not allowed within a 'System.Web.UI.WebControls.DataControlFieldCollection'.

how to do this ?

Nighil
  • 4,099
  • 7
  • 30
  • 56
Red Swan
  • 15,157
  • 43
  • 156
  • 238

2 Answers2

0

The problem occurs because you are trying to embed server-side control/value inside another server-side control. That is not directly possible in asp.net, unless you use databinding or custom expression builder.

For your exact situation, you need to create a custom expression builder, which returns the value from your static class.

Final result should look something like this:

<asp:TemplateField HeaderText="<$ ConstantManager:Name >">

Which is absolutely allowed withing aspx file as long as you have defined the custom expression builder with "ConstantManager" prefix.

The actual example of creating the custom expression builder can be found here: ExpressionBuilder Class.

ADDITION:

Also, I think databinding will also work, but it seems to me not the exact solution for this kind of situation.

Just use this syntax in the aspx markup:

<asp:TemplateField HeaderText="<# ConstantManager.Name >">

And, on page load, call:

protected void Page_Load(object sender, System.EventArgs e)
{
    this.DataBind();
}

Personally I don't like this solution because of the Page_Load part. Anyway, this does not need to have anything special declared/coded if compared to the custom expressions.

I hope this helps!

Tengiz
  • 8,011
  • 30
  • 39
0

Its better to bind the static class varibles in the GridView RowDataBound Event,

check the Row type is Header ie.

 if (e.Row.RowType == DataControlRowType.Header)
    {
       e.Row.Cells[0].Text = ConstantManager.Name;                  
    }
Maxymus
  • 1,460
  • 2
  • 14
  • 22