9 Replies - 2211 Views - Last Post: 19 December 2012 - 03:20 AM Rate Topic: -----

#1 jt_work  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 12

dynamic field names

Posted 08 December 2012 - 10:08 AM

Please see the below code

I want to be able to make the below code dynamic so it can be reused and in place of filterTaskList and TaskListId i want to use string variables. I believe its done using reflection (i could be wrong) can anyone help?

foreach (var x in Model.filterTaskList)
        {
            CbTaskListIds = CbTaskListIds + "<tr><input type=\"checkbox\" class=\"filtercheckbox\" onclick=\"checkFilterAll('" + x.TaskListId + "')\" name=\"" + x.TaskListId + "\" value=\"" + x.TaskListId + "\">" + x.TaskListId + "</input></tr>";
        }



Is This A Good Question/Topic? 0
  • +

Replies To: dynamic field names

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: dynamic field names

Posted 08 December 2012 - 10:16 AM

It might be plain to you what that code is supposed to do, but not to the rest of us.

Are you trying to dynamically make a bunch of controls named
"control1", "control2", "control3" then use them by referencing their names? If so, just stop. Many rookies go this route and it is just plain wrong.

How about telling us your end goal, and not the route you think you need to get there and we can try to give you better advice.
Was This Post Helpful? 0
  • +
  • -

#3 jt_work  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: dynamic field names

Posted 08 December 2012 - 10:57 AM

to give some background i am creating my first MVC2 web app. on my page i am going to have a link that shows a div and populates the div with a list of checkboxes. i have a number of different checkboxes which i will use depending on the link. At first i looked at holding a json object for each different set up checkboxes in a hidden field but i found that creating the checkboxes in jquery was slow and took a while to populate. I have now tried creating the lisk of checkboxes in my view (original code) and populated my hidden fields with this info. loading this when i show the div is instant. I therefore wanted to use the code i have created to loop through my c# list for each of my group of checkboxes to populate the hidden fields (approx 12 different groups of checkboxes). I was sure though how to make the list and field name in the loop dynamic?

hope this makes sense
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,662
  • Joined: 02-June 10

Re: dynamic field names

Posted 08 December 2012 - 11:04 AM

Web apps are out of my area. There was no indication of it in the title or the original question.

But I'm sure someone here should be able to help you now that there is a more clear explanation.
Was This Post Helpful? 0
  • +
  • -

#5 jt_work  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: dynamic field names

Posted 08 December 2012 - 11:14 AM

ultimately i am trying to loop through a list and create a string using the field name of the list and i want to make this dynamic so i can pass in a variable for the Model name and the field name. Can anyone help?
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: dynamic field names

Posted 08 December 2012 - 12:45 PM

Just make a method that accepts the list and the field name, if I understand what you are trying to do. Not very complicated.

Off topic, sort of: Why are you building the HTML in a string rather than using the HTML generating classes?
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3475
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: dynamic field names

Posted 09 December 2012 - 08:09 AM

View PostMomerath, on 08 December 2012 - 02:45 PM, said:

Off topic, sort of: Why are you building the HTML in a string rather than using the HTML generating classes?

+1. Use the HtmlTextWriter instead.

Or even better since you are writing a MVC2 application, I think that the Repeater would be an even better fit.
http://weblogs.asp.n...s-to-views.aspx


View PostMomerath, on 08 December 2012 - 02:45 PM, said:

Just make a method that accepts the list and the field name, if I understand what you are trying to do. Not very complicated.


Unfortunately going down this route will force him to use reflection. Or at least that's the way I seeing it.
string GenerateHtml(List<T> listToProcess, string fieldName)
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    foreach(T item in listToProcess)
    {
        fieldValue = // ... do reflection on item to get the value of fieldName

        htmlWriter.RenderBeginTag("tr");
        htmlWriter.RenderBeginTag("input");
        htmlWriter.AddAttribute("type", "checkbox");
        htmlWriter.AddAttribute("name", fieldValue);
        :
        htmlWriter.RenderEndTag();
        htmlWriter.RenderEndTag();
    }

    return stringWriter.ToString();
}


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3475
  • View blog
  • Posts: 10,721
  • Joined: 05-May 12

Re: dynamic field names

Posted 09 December 2012 - 08:16 AM

Perhaps using a IEnumerable<string> would be better...
string GenerateHtml(IEnumerable<string> values)
{
    :
    foreach(string value in values)
    {
        :
        htmlWriter.AddAttribute("name", value);
        :
    }
    :
}

:
    GenerateHtml(Model.filterTaskList.Select(item => item.TaskListId));
:


Was This Post Helpful? 0
  • +
  • -

#9 jt_work  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: dynamic field names

Posted 10 December 2012 - 12:54 AM

Thanks for your suggestions i will take a look at these
Was This Post Helpful? 0
  • +
  • -

#10 jt_work  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-December 12

Re: dynamic field names

Posted 19 December 2012 - 03:20 AM

[quote name='Skydiver' date='09 December 2012 - 08:09 AM' timestamp='1355065768' post='1764044']

View PostMomerath, on 08 December 2012 - 02:45 PM, said:

Off topic, sort of: Why are you building the HTML in a string rather than using the HTML generating classes?

+1. Use the HtmlTextWriter instead.

Or even better since you are writing a MVC2 application, I think that the Repeater would be an even better fit.
http://weblogs.asp.n...s-to-views.aspx


View PostMomerath, on 08 December 2012 - 02:45 PM, said:

Just make a method that accepts the list and the field name, if I understand what you are trying to do. Not very complicated.



thats for this i will look into the htmlwriter but it is the reflection part i need the assistance with. can anyone help?

Quote

Unfortunately going down this route will force him to use reflection. Or at least that's the way I seeing it.

string GenerateHtml(List<T> listToProcess, string fieldName)
{
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    foreach(T item in listToProcess)
    {
        fieldValue = // ... do reflection on item to get the value of fieldName

        htmlWriter.RenderBeginTag("tr");
        htmlWriter.RenderBeginTag("input");
        htmlWriter.AddAttribute("type", "checkbox");
        htmlWriter.AddAttribute("name", fieldValue);
        :
        htmlWriter.RenderEndTag();
        htmlWriter.RenderEndTag();
    }

    return stringWriter.ToString();
}


This post has been edited by JackOfAllTrades: 19 December 2012 - 03:49 AM
Reason for edit:: Fixed post and moved to ASP.NET

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1