2

You know if you're working with Repeater Controls you're not able to access the controls the generic way (as far as I know). So we try to use const strings for most situations we have to work with strings in C#.

So I've made a class with const strings. They define the ID names of the Controls inside a repeater. That makes it possible to access the controls in a more generic way than using the plain strings in code. Example:

public class FormularRepeaterFieldIDs
{
    public const string RubrikLiteral = "BuchungRubrikLiteral";
    public const string RubrikDropDownList = "BuchungRubrikList";
    public const string RubrikIdHiddenField = "BuchungRubrikIdHidden";
}

Which enables access like this (of course you could make a easier access wrapper for this):

                Literal rubrikLiteral = (Literal)e.Item.FindControl(TimeExpressHelper.FormularRepeaterFieldIDs.RubrikLiteral);
                rubrikLiteral.Visible = false;

Now I would also be cool to use the predefined IDs in Webforms Frontend code. It looks this at the moment:

<asp:Literal ID="BuchungRubrikLiteral" Text='<%#Eval("strRubrik")%>' runat="server" />

Would it be possible to make something like this?

<asp:Literal ID="<% FormularRepeaterFieldIDs.RubrikLiteral %>" Text='<%#Eval("strRubrik")%>' runat="server" />
7
  • 1
    try <%= FormularRepeaterFieldIDs.RubrikLiteral %> Commented Jun 22, 2012 at 9:11
  • 1
    Do you really change ids that often? Commented Jun 22, 2012 at 9:16
  • @nunespascal Not really. But It would be nice to have all things together. I'm just asking ;) Commented Jun 22, 2012 at 9:28
  • ID's in asp.net front can't contains "'|','.',',','@','#','%','^','&','*',mathematic operators" I don't really know is this posible but try fullNamespace.FormularRepeaterFieldIDs.RubrikLiteral.GetType().Name.Replace('.','_'); Commented Jun 22, 2012 at 9:32
  • @harry180 Nope still doesn't work. Not valid identifier. So I think it's not possible without using code behind. Commented Jun 22, 2012 at 9:36

2 Answers 2

1

You could probably make this work with a custom ExpressionBuilder. Something like:

  <asp:Literal ID="<%$ Const:FormularRepeaterFieldIDs.BuchungRubrik" 
     Text='<%#Eval("strRubrik")%>' runat="server" />

with a ExpressionBuilder that uses Reflection and/or a CodeSnippetExpression to read the value:

  class ConstExpressionBuilder : ExpressionBuilder {
       override bool SupportsEvaluate { get { return true; } }

       override CodeExpression GetCodeExpression(BoundPropertyEntry entry, ...) {
           return new CodeSnippetExpression(entry.Expression);
       }

       override object EvaluateExpression(BoundPropertyEntry entry, ...) {
           var splitExpression = entry.Expression.Split('.');
           var fieldName = splitExpression.Last();
           var typeName = entry.Expression.Substring(entry.Expression.Length - fieldName.Length - 1);
           var type = Type.GetType(typeName);
           return type.GetField(fieldName).GetValue(null);
       }
  }

and a web.config to register it to handle the Const prefix:

  <expressionBuilders>
     <add expressionPrefix="Const" type="ConstExpressionBuilder" />
  </expressionBuilders>

Or, you could take advantage of the existing ResourceExpressionBuilder, and make them resources instead of constants.

Sign up to request clarification or add additional context in comments.

1 Comment

That sounds nice. I'm not going to try this one day. I give you that point. I'm sure this works. About resources: I already thought this could be a good way. Because we use const for such definitions I asked for I didn't use resources that way, but you're right, that would work as well.
0

You can try

<div runat="server" id="container">
<% 
   List<string> listOfIds = new List<string>{FormularRepeaterFieldIDs.RubrikLiteral};
   foreach(var i in listOfIds.Count)
   {
      var l = new label();
      l.ID = listOfIds[i];
      container.Controls.Add(l);
   }
%>
</div>

1 Comment

Yeah of course I could. But thats actually the same doing it in code behind. I'm just searching for a really short way assigning a controls ID in frontend with help of a predefined const.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.