I have been fighting with trying to update a GridView for a while. I
don't want to use the "built-in" way to do it because I am using
business layer methods for updating and deleteing and I don't want to
have my parameter names in those methods have to be
"original_Param etername" or even if I change the
OldValuesParame terFormatString to get rid of the "original" I would
still have just "Parametername" . I think most people user parameters
like sParametername or iParametername name to help define the type of
the parameter. They typically don't follow the exact name of the
database column name....and they especially don't have "original_"
tacked on the fron of them....anyway. ...
So I am using RowEditing, RowUpdating on the GridView so I can use my
business layer method and send the parameters as I want. Now the
problem is I get an exception stating that I need to specify the
UpdateMethod on the ObjectDataSourc e. If I am manually handling these
events why do I need to specify the UpdateMethod?
So I play along and specify the UpdateMethod and put in my business
layer method. Now it performs the update, but still throws the
exception "could not find a non-generic method 'AddUpWaveHeigh t' that
has parameters: Height, ID". This is of course because my parameters
are sHeight and sID, not Height and ID.
So I can work around this by cancelling the ObjectdataSourc e_Updating
method in the codebehind.
But this seems like a workaround to me. I would like to know if this
is the correct behavior and the correct way to do this or if I am
missing something. I don't want to move forward until I have a good
feeling of how this is supposed to work and that I can make it work
the way I woudl like it to.
Any help is appreciated. Below is my page code and codebehind:
---------------------
PAGE CODE
---------------------
<asp:ObjectData Source ID="odsWaveHeig ht" runat="server"
OldValuesParame terFormatString ="{0}" SelectMethod="d sWaveHeightList "
UpdateMethod="A ddUpWaveHeight"
TypeName="FindM yBoardLibrary.C Selector">
<SelectParamete rs>
<asp:Paramete r Name="sRiderSki llID" DefaultValue="0 "
Type="String" />
</SelectParameter s>
</asp:ObjectDataS ource>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="o dsWaveHeight" AutoGenerateCol umns="False"
DataKeyNames="I D"
OnRowEditing="G ridView1_RowEdi ting"
OnRowCancelingE dit="GridView1_ RowCancelingEdi t"
OnRowUpdating=" GridView1_RowUp dating"
OnRowDeleting=" GridView1_RowDe leting">
<Columns>
<asp:TemplateFi eld>
<EditItemTempla te>
<asp:LinkButt on ID="lbtUpdate" runat="server"
CausesValidatio n="True" CommandName="Up date" Text="Update"></
asp:LinkButton>
<asp:LinkButt on ID="lbtCancel" runat="server"
CausesValidatio n="False" CommandName="Ca ncel" Text="Cancel"></
asp:LinkButton>
</EditItemTemplat e>
<ItemTemplate >
<asp:LinkButt on ID="lbtEdit" runat="server"
CausesValidatio n="False" CommandName="Ed it" Text="Edit"></
asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld SortExpression= "Height"
HeaderText="Hei ght">
<EditItemTempla te>
<asp:TextBox ID="txtHeight" Runat="server" Text='<
%# Bind("Height") %>'></asp:TextBox>
<asp:RequiredFi eldValidator ID="rfvHeight"
Runat="server" ErrorMessage="Y ou must enter the Height"
ControlToValida te="txtHeight"> </asp:RequiredFie ldValidator>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="lblHeight" Runat="server" Text='<%#
Bind("Height") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld>
<ItemTemplate >
<asp:LinkButt on ID="lbtDelete" runat="server"
CausesValidatio n="False" CommandName="De lete" Text="Delete"></
asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
</asp:GridView>
---------------------
CODEBEHIND
---------------------
Protected Sub GridView1_RowCa ncelingEdit(ByV al sender As Object,
ByVal e As System.Web.UI.W ebControls.Grid ViewCancelEditE ventArgs)
Handles GridView1.RowCa ncelingEdit
GridView1.EditI ndex = -1
GridView1.DataB ind()
End Sub
Protected Sub GridView1_RowDe leting(ByVal sender As Object, ByVal
e As System.Web.UI.W ebControls.Grid ViewDeleteEvent Args) Handles
GridView1.RowDe leting
Dim cSelector As New CSelector
Dim sID As String =
GridView1.DataK eys(e.RowIndex) .Values(0).ToSt ring
cSelector.Delet eWaveHeight("0" , sID)
GridView1.DataB ind()
End Sub
Protected Sub GridView1_RowEd iting(ByVal sender As Object, ByVal e
As System.Web.UI.W ebControls.Grid ViewEditEventAr gs) Handles
GridView1.RowEd iting
GridView1.EditI ndex = e.NewEditIndex
GridView1.DataB ind()
End Sub
Protected Sub GridView1_RowUp dating(ByVal sender As Object, ByVal
e As System.Web.UI.W ebControls.Grid ViewUpdateEvent Args) Handles
GridView1.RowUp dating
Dim cSelector As New CSelector
Dim sID As String =
GridView1.DataK eys(e.RowIndex) .Values(0).ToSt ring
Dim txtHeight As TextBox =
CType(GridView1 .Rows.Item(e.Ro wIndex).FindCon trol("txtHeight "),
TextBox)
Dim sHeight As String = txtHeight.Text
cSelector.AddUp WaveHeight("0", sID, sHeight)
GridView1.EditI ndex = -1
GridView1.DataB ind()
End Sub
Private Sub odsWaveHeight_U pdating(ByVal sender As Object, ByVal e
As System.Web.UI.W ebControls.Obje ctDataSourceMet hodEventArgs) Handles
odsWaveHeight.U pdating
e.Cancel = True
End Sub
don't want to use the "built-in" way to do it because I am using
business layer methods for updating and deleteing and I don't want to
have my parameter names in those methods have to be
"original_Param etername" or even if I change the
OldValuesParame terFormatString to get rid of the "original" I would
still have just "Parametername" . I think most people user parameters
like sParametername or iParametername name to help define the type of
the parameter. They typically don't follow the exact name of the
database column name....and they especially don't have "original_"
tacked on the fron of them....anyway. ...
So I am using RowEditing, RowUpdating on the GridView so I can use my
business layer method and send the parameters as I want. Now the
problem is I get an exception stating that I need to specify the
UpdateMethod on the ObjectDataSourc e. If I am manually handling these
events why do I need to specify the UpdateMethod?
So I play along and specify the UpdateMethod and put in my business
layer method. Now it performs the update, but still throws the
exception "could not find a non-generic method 'AddUpWaveHeigh t' that
has parameters: Height, ID". This is of course because my parameters
are sHeight and sID, not Height and ID.
So I can work around this by cancelling the ObjectdataSourc e_Updating
method in the codebehind.
But this seems like a workaround to me. I would like to know if this
is the correct behavior and the correct way to do this or if I am
missing something. I don't want to move forward until I have a good
feeling of how this is supposed to work and that I can make it work
the way I woudl like it to.
Any help is appreciated. Below is my page code and codebehind:
---------------------
PAGE CODE
---------------------
<asp:ObjectData Source ID="odsWaveHeig ht" runat="server"
OldValuesParame terFormatString ="{0}" SelectMethod="d sWaveHeightList "
UpdateMethod="A ddUpWaveHeight"
TypeName="FindM yBoardLibrary.C Selector">
<SelectParamete rs>
<asp:Paramete r Name="sRiderSki llID" DefaultValue="0 "
Type="String" />
</SelectParameter s>
</asp:ObjectDataS ource>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="o dsWaveHeight" AutoGenerateCol umns="False"
DataKeyNames="I D"
OnRowEditing="G ridView1_RowEdi ting"
OnRowCancelingE dit="GridView1_ RowCancelingEdi t"
OnRowUpdating=" GridView1_RowUp dating"
OnRowDeleting=" GridView1_RowDe leting">
<Columns>
<asp:TemplateFi eld>
<EditItemTempla te>
<asp:LinkButt on ID="lbtUpdate" runat="server"
CausesValidatio n="True" CommandName="Up date" Text="Update"></
asp:LinkButton>
<asp:LinkButt on ID="lbtCancel" runat="server"
CausesValidatio n="False" CommandName="Ca ncel" Text="Cancel"></
asp:LinkButton>
</EditItemTemplat e>
<ItemTemplate >
<asp:LinkButt on ID="lbtEdit" runat="server"
CausesValidatio n="False" CommandName="Ed it" Text="Edit"></
asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld SortExpression= "Height"
HeaderText="Hei ght">
<EditItemTempla te>
<asp:TextBox ID="txtHeight" Runat="server" Text='<
%# Bind("Height") %>'></asp:TextBox>
<asp:RequiredFi eldValidator ID="rfvHeight"
Runat="server" ErrorMessage="Y ou must enter the Height"
ControlToValida te="txtHeight"> </asp:RequiredFie ldValidator>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="lblHeight" Runat="server" Text='<%#
Bind("Height") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld>
<ItemTemplate >
<asp:LinkButt on ID="lbtDelete" runat="server"
CausesValidatio n="False" CommandName="De lete" Text="Delete"></
asp:LinkButton>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
</asp:GridView>
---------------------
CODEBEHIND
---------------------
Protected Sub GridView1_RowCa ncelingEdit(ByV al sender As Object,
ByVal e As System.Web.UI.W ebControls.Grid ViewCancelEditE ventArgs)
Handles GridView1.RowCa ncelingEdit
GridView1.EditI ndex = -1
GridView1.DataB ind()
End Sub
Protected Sub GridView1_RowDe leting(ByVal sender As Object, ByVal
e As System.Web.UI.W ebControls.Grid ViewDeleteEvent Args) Handles
GridView1.RowDe leting
Dim cSelector As New CSelector
Dim sID As String =
GridView1.DataK eys(e.RowIndex) .Values(0).ToSt ring
cSelector.Delet eWaveHeight("0" , sID)
GridView1.DataB ind()
End Sub
Protected Sub GridView1_RowEd iting(ByVal sender As Object, ByVal e
As System.Web.UI.W ebControls.Grid ViewEditEventAr gs) Handles
GridView1.RowEd iting
GridView1.EditI ndex = e.NewEditIndex
GridView1.DataB ind()
End Sub
Protected Sub GridView1_RowUp dating(ByVal sender As Object, ByVal
e As System.Web.UI.W ebControls.Grid ViewUpdateEvent Args) Handles
GridView1.RowUp dating
Dim cSelector As New CSelector
Dim sID As String =
GridView1.DataK eys(e.RowIndex) .Values(0).ToSt ring
Dim txtHeight As TextBox =
CType(GridView1 .Rows.Item(e.Ro wIndex).FindCon trol("txtHeight "),
TextBox)
Dim sHeight As String = txtHeight.Text
cSelector.AddUp WaveHeight("0", sID, sHeight)
GridView1.EditI ndex = -1
GridView1.DataB ind()
End Sub
Private Sub odsWaveHeight_U pdating(ByVal sender As Object, ByVal e
As System.Web.UI.W ebControls.Obje ctDataSourceMet hodEventArgs) Handles
odsWaveHeight.U pdating
e.Cancel = True
End Sub