How to instruct ObjectDataSource to match form parameter with method parameter - asp.net

When working with ObjectDataSource I have following select:
<asp:DropDownList runat="server" SelectedValue='<%# Bind("InceptionCycle.ID") %>' ID="InceptionCycle"
DataSourceID="odsAllCycles" DataTextField="CycleName" DataValueField="ID" AppendDataBoundItems="true">
<asp:ListItem Text="Choose..." Value=""></asp:ListItem>
</asp:DropDownList>
and ObjectDataSource configured with UpdateMethod and InsertMethod. However insert/update methods have parameters with InceptionCycle name (because InceptionCycle.ID is not valid identifier).
Is there a way to instruct ObjectDataSource to take InceptionCycle.ID form parameter and place in as InceptionCycle method parameter?
Here's full ObjectDataSource code:
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Sources.DomainSource"
SelectMethod="FindById" InsertMethod="Add" UpdateMethod="Update"
OnInserted="ods_Inserted" OnUpdated="ods_Updated">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
</SelectParameters>
<InsertParameters>
<asp:Parameter ConvertEmptyStringToNull="true" Name="ShortTitle" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="WMRId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="MDSId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="Status" />
</InsertParameters>
<UpdateParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="ShortTitle" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="WMRId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="MDSId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="Status" />
</UpdateParameters>
</asp:ObjectDataSource>

You can only use the Bind syntax with a top level property - it doesn't support nested properties (VS 2012 will, though).
So, you should create a new property on your data source like below and bind the SelectedValue to this new property instead
public string InceptionCycleID
{
get
{
return InceptionCycle.ID;
}
set
{
InceptionCycle.ID = value;
}
}

Related

Detailsview templatefield mapping to ObjectDataSource?

I need help to understand the proper way to map or associate TemplateFields to the ObjectDataSource. I show only one (of many) template definitions here:
<asp:TemplateField HeaderText="First Name" SortExpression="cFIRSTNAME">
<ItemTemplate>
<asp:Label ID="lblValFirstName" runat="server" Text='<%# Eval("cFIRSTNAME") %>' style="font-weight: bold;" ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("cFIRSTNAME") %>' MaxLength="20"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("cFIRSTNAME") %>' MaxLength="20"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
The ObjectDataSource and update parameters are:
<asp:ObjectDataSource ID="ODS_LOGIN_DETAILS" runat="server"
SelectMethod="GetLOGINSbyLoginID"
TypeName="bllLOGINS"
DeleteMethod="DeleteFromDetailsView"
InsertMethod="InsertFromDetailsView"
UpdateMethod="UpdateFromDetailsView" OldValuesParameterFormatString="original_{0}"> <UpdateParameters>
<asp:Parameter Name="p_UID_LOGIN" Type="Int32" />
<asp:Parameter Name="p_UID_CONTACT" Type="Int32" />
<asp:Parameter Name="p_UID_USER_TYPE" Type="Int32" />
<asp:Parameter Name="p_TXT_USERNAME" Type="String" />
<asp:Parameter Name="p_TXT_PASSWORD" Type="String" />
<asp:Parameter Name="p_BOOL_IS_ACTIVE" Type="Boolean" />
<asp:Parameter Name="p_DT_END" Type="DateTime" />
<asp:Parameter Name="p_FirstName" Type="String" />
<asp:Parameter Name="p_LastName" Type="String" />
<asp:Parameter Name="p_CONTACTTITLE" Type="String" />
<asp:Parameter Name="p_TXT_PHONE" Type="String" />
<asp:Parameter Name="p_CONTACT_EMAIL" Type="String" /> </UpdateParameters>
What is NOT clear to me is how is the mapping made from the templatefield textbox (txtFirstName) to the UpdateParameter "p_FirstName"?
In the existing code I am working with, I see the ObjectDataSource_Updating()-event that does a series of DVW.FindControl() values being assigned/set to the
e.InputParameter("param-name") = text-box-value
Questions?
Is this done in either the ObjectDataSource_Updating()-event or in the DetailsView_Updtaing()-event -or- by declaration aspx code?
or how?
What is the best way to do this?
Would naming the textbox-ID and the parameter-name simplify the code?
I am assuming the solutions offered would be "similar" or the same as with SqlDataSource -- please confirm?
Thanks in advance...John
I had a similar problem and was unsure how to map the textbox to the params as was getting NULL param value passed when doing a SQL trace. In the end I set the parameter name to match the value in the Text attribute bind which worked.
<UpdateParameters>
<asp:Parameter Name="my_field" Type="String" Direction="Input" />
...
<asp:TextBox Text='<%# Bind("my_field") %>'...

How do I force refresh an ObjectDataSource?

In my ASP.NET web forms I find that the ObjectDataSource won't update/refresh when the SelectParameter values do not change.
How do I force refresh an ObjectDataSource regardless of whether or not the parameter values change?
Example code:
<asp:ObjectDataSource ID="odsUserSearchResults" runat="server"
SelectMethod="GetData" EnablePaging="false"
TypeName="MySolution.ObjectDataSources.Users">
<SelectParameters>
<asp:ControlParameter Name="name" ControlID="txtName" PropertyName="Text" Type="String" />
<asp:ControlParameter Name="statusId" ControlID="ddlStatus" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
You need to disable the object data source view state by setting the EnableViewState property to false, like so:
<asp:ObjectDataSource ID="odsUserSearchResults" EnableViewState="false" runat="server"
SelectMethod="GetData" EnablePaging="false"
TypeName="MySolution.ObjectDataSources.Users">
<SelectParameters>
<asp:ControlParameter Name="name" ControlID="txtName" PropertyName="Text" Type="String" />
<asp:ControlParameter Name="statusId" ControlID="ddlStatus" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

order gridview list decending

I currently have a table of data which is ordered in ID order, but i want the list to be ordered in descending order. This i am sure is a simple question but i just can not figure out how to do it.
Here is the ObjectDataSource etc. I am just not sure where to change the code...
Thanks
<asp:ObjectDataSource
ID="odsTours"
runat="server"
SelectMethod="GetFiltered"
TypeName="blive.Shop.Tour"
SortParameterName="sortExpression">
<SelectParameters>
<asp:Parameter
Name="isActive"
ConvertEmptyStringToNull="true"
DefaultValue="" />
<asp:Parameter
Name="isInThePast"
ConvertEmptyStringToNull="true"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource
ID="odsBookings"
runat="server"
SelectMethod="GetFiltered"
TypeName="blive.Shop.Booking"
SortParameterName="sortExpression">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlTourFilter"
PropertyName="SelectedValue"
Name="tourId"
Type="Int32"
ConvertEmptyStringToNull="true" />
<asp:Parameter
Name="username"
ConvertEmptyStringToNull="true"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
It should be done in the select method of the object data source.
In your case it should be in
GetFiltered method.
You can apply the order by in this method.

Change Data Sources on ASP.net

Issue: I have 2 datasources declared on my .aspx file as shown below:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="UsuarioDelete" SelectMethod="SelectMyAllNotME_Data" TypeName="BLLayer.Usuarios" OnInserted="ObjectDataSource_Inserted">
<DeleteParameters>
<asp:ControlParameter ControlID="ConfirmDialogBox1" Name="id_usuario" PropertyName="Aceptar_CommandName" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:SessionParameter DefaultValue="1" Name="id_usuario" SessionField="id_usuario" Type="Int32" />
<asp:SessionParameter DefaultValue="1" Name="id_pais" SessionField="id_pais" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="UsuarioDelete" SelectMethod="SelectMyAllNotME_Data" TypeName="BLLayer.Usuarios" OnInserted="ObjectDataSource_Inserted">
<DeleteParameters>
<asp:ControlParameter ControlID="ConfirmDialogBox1" Name="id_usuario" PropertyName="Aceptar_CommandName" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="id_emp" QueryStringField="id_emp" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
The method "SelectMyAllNotME_Data" is an overloaded method that in one he receives 2 values and in the other just one value, it is used to filter the information.
What I want to do is than in my onLoadPage method is something like this:
if (Request.QueryString["id_emp"] != null)
{
GridViewUsers.DataSource = ObjectDataSource2.SelectMethod;
GridViewUsers.DataMember = ObjectDataSource2.SelectMethod;
}
else
{
GridViewUsers.DataSource = ObjectDataSource1.SelectMethod;
GridViewUsers.DataMember = ObjectDataSource1.SelectMethod;
}
}
I've already tried this, but it isn't working, can anyone help me please?
You should call
GridViewUsers.DataBind();
on the datasource in aspx just do
EnablePaging="true"

ASP.NET ObjectDataSource, change CommandName property of FormView Button

I have an ObjectDataSource that I'm using with a FormView, and it works fine, but i want to change one small thing. On the FormView the button that fires the update has the CommandName attribute set to "Update," but I would like to change that attribute to something other than "Update" - when I do change that attribute the update no longer works. The reason I want to do this is I have multiple FormViews on the same page and need to have multiple update buttons. Below is my code:
FormView:
<asp:FormView ID="fvGeneralInfo" runat="server"
DataSourceID="objInstructorDetails" CssClass="Gridview"
OnItemCommand="fvGeneralInfo_ItemCommand"
DefaultMode="Edit">
<EditItemTemplate>
<table>
....
<tr>
<td style="text-align:right;">
<asp:Label runat="server" ID="lblGeneralInfoMessage" Text="General Info updated successfully" Visible="false" />
</td>
<td>
<asp:Button runat="server" ID="btnUpdateGeneralInfo" ValidationGroup="UpdateGeneralInfo" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="btnCancelGeneralInfo" Text="Cancel" CommandName="CancelGeneralInfo" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
ObjectDataSource:
<asp:ObjectDataSource ID="objInstructorDetails" runat="server" TypeName="AIMLibrary.BLL.Instructor" SelectMethod="GetInstructorDetails"
InsertMethod="InsertInstructor" UpdateMethod="UpdateInstructor" OnInserting="objInstructorDetails_OnInserting"
OnUpdating="objInstructorDetails_OnUpdating" >
<SelectParameters>
<asp:QueryStringParameter Name="InstructorId" QueryStringField="InstructorId" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="instructorId" Type="Int32" />
<asp:Parameter Name="firstName" Type="String" DefaultValue="" />
<asp:Parameter Name="middleName" Type="String" DefaultValue="" />
<asp:Parameter Name="lastName" Type="String" DefaultValue="" />
<asp:Parameter Name="phone" Type="String" DefaultValue="" />
<asp:Parameter Name="email" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine1" Type="String" DefaultValue="" />
<asp:Parameter Name="addressLine2" Type="String" DefaultValue="" />
<asp:Parameter Name="city" Type="String" DefaultValue="" />
<asp:Parameter Name="state" Type="String" DefaultValue="" />
<asp:Parameter Name="zip" Type="String" DefaultValue="" />
<asp:Parameter Name="abcBoardNumber" Type="String" DefaultValue="" />
</UpdateParameters>
</asp:ObjectDataSource>
Each FormView will have it's own event for handling updates so the CommandName being the same for different FormViews should not be a problem.
You can change the name of the buttons if that's an issue by changing it's text value.

Resources