C
C#13mo ago
AJ

❔ ASP.NET Dynamic input controls

Hello! I'm currently learning ASP.NET web development, having experience with web development using the MERN stack, and I'm struggling to wrap my head around doing so much server-sided work. Specifically right now I'm trying to create a dynamic table containing input fields in the code behind such that the user can add and remove rows, and eventually submit their inputs. I've achieved the basic functionality of adding and removing rows, however I can't seem to understand how to persist the user input in these dynamically-created rows while adding and removing them. I'm entirely open to recreating this from scratch as I've likely done it terribly inefficiently. All solutions I've thought about and tried so far just seem so impractical compared to basic client-side jquery/js Any help is appreciated, thanks!
5 Replies
Angius
Angius13mo ago
Easiest way would be sending the form with JS, as Json, and receiving it in an API endpoint Quick and dirty example
[HttpPost]
public async Task<IActionResult> AddStuff(List<ThingDto> data)
{
var things = data.Select(td => new Thing {
Foo = td.Foo,
Bar = td.Bar,
...
});
_context.Things.AddRange(things);
await _context.SaveChangesAsync();
}
[HttpPost]
public async Task<IActionResult> AddStuff(List<ThingDto> data)
{
var things = data.Select(td => new Thing {
Foo = td.Foo,
Bar = td.Bar,
...
});
_context.Things.AddRange(things);
await _context.SaveChangesAsync();
}
Send
[
{
"foo": "kashgd",
"bar": "69",
"..."
},
{
"foo": "uityurty",
"bar": "420",
"..."
}
]
[
{
"foo": "kashgd",
"bar": "69",
"..."
},
{
"foo": "uityurty",
"bar": "420",
"..."
}
]
Boom
AJ
AJOP13mo ago
Sorry, I'm not really understanding, and even if I did I'm not sure where that code would go 😅 Might be important to note that I'm working with ASP Web Forms Just to provide a little more context, this small project is for an internship and they REALLY haven't taught me much at all, so far all I've really done is create a few basic pages / forms with communication to a MSSQL db and some session storage stuff, only really working with the .aspx, .aspx.cs, and some .cs files
Angius
Angius13mo ago
A oof ouch owie webforms Won't be able to help with that, I'm no archaeologist
AJ
AJOP13mo ago
LOL no problem, thank you the more i work with this the more I feel that ASP web forms are NOT DE WAY So i got it to work... very jankily... i noticed on postback that the Request will always contain form input values even from the dynamically added controls so I grab all of those (for loop adding them to a list with Request.Form.Get()), and manually set the value of each input with
.FindControl(string id)
.FindControl(string id)
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server