Poidda
asked on
Gridview onUpdate set new value
Hi, I am using a gridview and want to set a field in the database to log that a change has been made.
Here is a trimmed down version of my code
<asp:GridView ID=gvGroup runat="server" OnRowUpdating="gvGroup_OnR owUpdating "
DataKeyNames="iGroupId" AutoGenerateColumns=false DataSourceID="SqlDataSourc e_gvGroup" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=lblPlayer1 Width="95%" runat="server" Text='<%# Eval("vcPlayer1") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=txtPlayer1 Width="95%" runat="server" Text='<%# Bind("vcPlayer1") %>'/>
<asp:Label ID=lblLogin1 Width="95%" runat="server" Text='<%# Eval("vcLogin1") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<asp:GridView>
<asp:SqlDataSource ID="SqlDataSource_gvGroup" runat="server" "
SelectCommand="spGetGroup_ CompId" SelectCommandType="StoredP rocedure"
UpdateCommand="spUpdateGro up" UpdateCommandType="StoredP rocedure"
DeleteCommand="spDeleteGro up" DeleteCommandType="StoredP rocedure">
<SelectParameters>
<asp:ControlParameter ControlID="gvComp" Name="iCompId" PropertyName="SelectedValu e" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="iGroupId" Type="Int32" />
<asp:SessionParameter Name="iCompId" SessionField=CompId Type=Int32/>
<asp:Parameter Name="dtGroupTime" Type="DateTime"/>
<asp:Parameter Name="iHoleNo" Type="Int32" />
<asp:Parameter Name="vcPlayer1" Type="String" />
<asp:Parameter Name="vcLogin1" Type="String" />
<asp:Parameter Name="vcPlayer2" Type="String" />
<asp:Parameter Name="vcLogin2" Type="String" />
<asp:Parameter Name="vcPlayer3" Type="String" />
<asp:Parameter Name="vcLogin3" Type="String" />
<asp:Parameter Name="vcPlayer4" Type="String" />
<asp:Parameter Name="vcLogin4" Type="String" />
<asp:Parameter Name="iRetVal" DefaultValue=0 Type="int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="iGroupId" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
Now I thought I could set the lblLogin1 by checking on the RowUpdate like this.
protected void gvGroup_OnRowUpdating(obje ct sender, GridViewUpdateEventArgs e)
{
string strNewPlayer;
string strOldPlayer;
strNewPlayer = e.NewValues["vcPlayer1"] == null ? string.Empty : e.NewValues["vcPlayer1"].T oString();
strOldPlayer = e.OldValues["vcPlayer1"] == null ? string.Empty : e.OldValues["vcPlayer1"].T oString();
if (strNewPlayer != strOldPlayer)
{
Label lblLogin1 = (Label)gvGroup.Rows[e.RowI ndex].Find Control("l blLogin1") ;
lblLogin1.Text = Context.User.Identity.Name ;
}
}
So this is all executed, but my database is not updated with the correct LoginId. Im guessing that the update to te database is carried ou before gvGroup_OnRowUpdating is executed. My question is:
How can I do this?
Cheers for your help in advance.
Here is a trimmed down version of my code
<asp:GridView ID=gvGroup runat="server" OnRowUpdating="gvGroup_OnR
DataKeyNames="iGroupId" AutoGenerateColumns=false DataSourceID="SqlDataSourc
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=lblPlayer1 Width="95%" runat="server" Text='<%# Eval("vcPlayer1") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=txtPlayer1 Width="95%" runat="server" Text='<%# Bind("vcPlayer1") %>'/>
<asp:Label ID=lblLogin1 Width="95%" runat="server" Text='<%# Eval("vcLogin1") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<asp:GridView>
<asp:SqlDataSource ID="SqlDataSource_gvGroup"
SelectCommand="spGetGroup_
UpdateCommand="spUpdateGro
DeleteCommand="spDeleteGro
<SelectParameters>
<asp:ControlParameter ControlID="gvComp" Name="iCompId" PropertyName="SelectedValu
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="iGroupId" Type="Int32" />
<asp:SessionParameter Name="iCompId" SessionField=CompId Type=Int32/>
<asp:Parameter Name="dtGroupTime" Type="DateTime"/>
<asp:Parameter Name="iHoleNo" Type="Int32" />
<asp:Parameter Name="vcPlayer1" Type="String" />
<asp:Parameter Name="vcLogin1" Type="String" />
<asp:Parameter Name="vcPlayer2" Type="String" />
<asp:Parameter Name="vcLogin2" Type="String" />
<asp:Parameter Name="vcPlayer3" Type="String" />
<asp:Parameter Name="vcLogin3" Type="String" />
<asp:Parameter Name="vcPlayer4" Type="String" />
<asp:Parameter Name="vcLogin4" Type="String" />
<asp:Parameter Name="iRetVal" DefaultValue=0 Type="int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="iGroupId" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
Now I thought I could set the lblLogin1 by checking on the RowUpdate like this.
protected void gvGroup_OnRowUpdating(obje
{
string strNewPlayer;
string strOldPlayer;
strNewPlayer = e.NewValues["vcPlayer1"] == null ? string.Empty : e.NewValues["vcPlayer1"].T
strOldPlayer = e.OldValues["vcPlayer1"] == null ? string.Empty : e.OldValues["vcPlayer1"].T
if (strNewPlayer != strOldPlayer)
{
Label lblLogin1 = (Label)gvGroup.Rows[e.RowI
lblLogin1.Text = Context.User.Identity.Name
}
}
So this is all executed, but my database is not updated with the correct LoginId. Im guessing that the update to te database is carried ou before gvGroup_OnRowUpdating is executed. My question is:
How can I do this?
Cheers for your help in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
e.NewValues.Add("vcLogin1"
points are yours!