locked
How can I soft code the textbox and column names used in a ListView RRS feed

  • Question

  • User793608999 posted

    In a ListView example I want to replace the literal Strings shown in blue with a soft coded text string equivalent

    // Nasty hardcoded naming...
    <EditItemTemplate>
    <tr> <td> <asp:TextBox ID="FirstNameEditTextBox" runat="server" Text=' <%#Eval("FirstName")%>'></asp:TextBox> </td> <td> <asp:TextBox ID="LastNameEditTextBox" runat="server" Text=' <%#Eval("LastName")%>'></asp:TextBox> </td> </tr>
    </EditItemTemplate>

    I want to soft code the column names e.g. (simplistic)

    protected const string FirstNameEdit = "FirstNameEditTextBox";
    protected const string LastNameEdit = "LastNameEditTextBox";
    protected const string FirstName = "FirstName";
    protected const string LastName = "LastName";

    //More ideal attempt (I know why this fails, but I cannot find a solution)
    <EditItemTemplate> <tr> <td> <asp:TextBox ID="<%=FirstNameEdit %>" runat="server" Text=' <%#Eval("<%=FirstName %>")%>'></asp:TextBox> </td> <td> <asp:TextBox ID="<%=LastNameEdit %>" runat="server" Text=' <%#Eval("<%=LastName %>")%>'></asp:TextBox> </td> </tr> </EditItemTemplate>

    Any help would be welcomed.

    Russ

    Wednesday, April 11, 2018 2:00 PM

All replies

  • User475983607 posted

    Your approach will not work in ASP Web Forms.  It has to do with how the framework fundamentally works.  The markup is tied to a designer file by the element Id.  Whenever a server control is added to the aspx page, Visual Studio updates a designer file.  The design file wires up server controls and events associated with the server controls.

    The best match for what you are trying to do is creating dynamic controls but this approach bypassess ASP.NET state management. 

    Simply use the framework as intended.  Perhaps go through a few getting started tutorials.

    https://www.asp.net/web-forms

    Wednesday, April 11, 2018 2:40 PM
  • User793608999 posted

    Thank you for your answer. It's sad that controls are not referenced by their programatical names and use strings instead.  This is a source of errors and breaks refactoring.

    I take your point that aspx is what it is and and has to be used accordingly.  I was taught to avoid HardCoding at all costs.

    I do appreciate your assistance, it will save me time searching for an ideal solution.

    Russ

    Wednesday, April 11, 2018 3:28 PM
  • User475983607 posted

    Thank you for your answer. It's sad that controls are not referenced by their programatical names and use strings instead.  This is a source of errors and breaks refactoring.

    The IDs are actually dynamically created by the framework at run-time.   You'll get the hang of it with a bit more experience.  

    Wednesday, April 11, 2018 3:33 PM