I'm trying to handle the updates on 2 related tables in one DetailsView using Jquery and Linq, and h
Posted
by Ben Reisner
on Stack Overflow
See other posts from Stack Overflow
or by Ben Reisner
Published on 2010-05-07T01:20:23Z
Indexed on
2010/05/07
1:28 UTC
Read the original article
Hit count: 341
Given two related tables (reports, report_fields) where there can be many report_fields entries for each reports entry, I need to allow the user to enter new report_fields, delete existing report_fields, and re-arrange the order.
Currently I am using a DetailsView to handle the editing of the reports. I added some logic to handle report_fields, and currently it allows you to succesfully re-arrange the order, but i'm a little stumped as to the best way to add new items, or delete existing items.
The basic logic I have is that each report_fields is represented by a . It has a description as the text, and a field for each field in the report_fields table. I use JQuery Sortable to allow the user to re-arrange the LIs.
Abbreviated Create Table Statements:(foreign key constraint ignored for brevity)
create table report(
id integer primary key identity,
reportname varchar(250)
)
create table report_fields(
id integer primary key identity,
reportID integer,
keyname integer,
keyvalue integer,
field_order integer
)
My abbreviated markup:
<asp:DetailsView ...>
...
<asp:TemplateField HeaderText="Fields">
<EditItemTemplate>
<ul class="MySortable">
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("report_fields") %>'>
<ItemTemplate>
<li>
<%# Eval("keyname") %>: <%# Eval("keyvalue") %>
<input type="hidden" name="keyname[]" value='<%# Eval("keyname") %>' />
<input type="hidden" name="keyvalue[]" value='<%# Eval("keyvalue") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</EditItemTemplate>
</asp:TemplateField>
</asp:DetailsView>
<asp:LinqDataSource ID="LinqDataSource2" onupdating="LinqDataSource2_Updating" table=reports ... />
$(function() {
$(".MySortable").sortable({
placeholder: 'MySortable-highlight'
}).disableSelection();
});
Code Behind Class:
public partial class Administration_AddEditReport
protected void LinqDataSource2_Updating(object sender, LinqDataSourceUpdateEventArgs e)
{
report r = (report)e.NewObject;
MyDataContext dc = new MyDataContext();
var fields = from f in dc.report_fields
where f.reportID == r.id
select f;
dc.report_fields.DeleteAllOnSubmit(fields);
NameValueCollection nvc = Request.Params;
string[] keyname = nvc["keyname[]"].Split(',');
string[] keyvalue = nvc["keyvalue[]"].Split(',');
for (int i = 0; i < keyname.Length; i++)
{
report_field rf = new report_field();
rf.reportID = r.id;
rf.keyname = keyname[i];
rf.keyvalue = keyvalue[i];
rf.field_order = i;
dc.report_fields.InsertOnSubmit(rf);
}
dc.SubmitChanges();
}
}
© Stack Overflow or respective owner