.net - Manually Binding a Model to an ASP.NET FormView using model binding -
i trying load details of single product list of products displayed in listview formview control using asp.net 4.5's model binding capability.
this hosted in single aspx page decides display using multiview control. in 1 view have listview displays products. in view have formview control display/edit details of single product. when click on single product record in listview, want display view contains formview, , load product formview editing or review.
following direction model binding in webforms, have defined "selectmethod" property of formview method takes productid it's parameter. method returns single corresponding product object. understanding somewhere in lifecycle of page method called , formview automatically loaded.
now, when click on record in listview view details, fires itemcommand event handler. event handler uses product id selected record , retrieves product corresponds id.
the problem comes in need bind product formview. how manually assign product model formview binds to? when attempt manually set datasource of formview error:
"datasource or datasourceid cannot defined on 'productform' when uses model binding."
has else has similar issue , figured out how handle it?
thanks,
g
there's no need manually assign product because data binding takes care of leg work you.
firstly, make sure have sort of button in each row of listview commandname of select:
<asp:listview id="productlist" runat="server" datakeynames="id" itemtype="product" selectmethod="getproductlist"> <itemtemplate> <%# item.name %> <asp:button id="selectbutton" runat="server" commandname="select" /> </itemtemplate> </asp:listview>
secondly, selectmethod of formview make sure product id parameter has control attribute specified string passed constructor must match id of listview control:
public product getproduct([control("productlist")] int? id) { if (id.hasvalue) { return getproductlist().first(p => p.id == id.value); } return null; }
that's need do, because when select button clicked selectedvalue of listview changes , getproduct method automatically called , formview updated.
Comments
Post a Comment