jholmes0724
asked on
Updating Gridview using OnUpdating Event does not get Values from Editing row
Scenario:
I am using the RowUpdating Event of the Gridview to do updates because I need more flexibility than the SQLDatasource Update provides. So the problem I am having is that when I get the Datakey value I am trying to get the values in the controls in the Edit Mode of the current row I am trying to update. But I get some strange results. I am using the FindControl function to get the values from the editing row so I specifically name the dropdownlists and textboxes so I can access them here. But what happens is that I do get some of the values but they are the original values not the new ones being changed. In the code below I do get the CountryID and RegionID but they are the original values not the new ones selectedfrom the Edit Mode row. The Textbox values come back as empty, which is even stranger. Need some help figuring this out. Below is the code for both the GridView HTML and the C# code behind.
Code (C# OnUpdating Event)
protected void gvMain_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//---- Get Gridview Datakey Value and Dropdown value from selected Index ---
string recIDTxt = gvMain.DataKeys[e.RowIndex ].Value.To String();
int recID = Convert.ToInt32(recIDTxt);
//--- Define Gridview Row Elements
GridView grid = (GridView)sender;
GridViewRow row = (GridViewRow)grid.Rows[e.R owIndex];
DropDownList ddlCountryEdit = (DropDownList)row.FindCont rol("ddlCo untryEdit" );
DropDownList ddlRegionEdit = (DropDownList)row.FindCont rol("ddlRe gionEdit") ;
TextBox txtModelNameEdit = (TextBox)row.FindControl(" txtModelNa meEdit");
TextBox txtTransModelEdit = (TextBox)row.FindControl(" txtTransMo delEdit");
TextBox txtPriceEdit = (TextBox)row.FindControl(" txtPriceEd it");
int countryID = Convert.ToInt32(ddlCountry Edit.Selec tedValue); // This returns old value
int regionID = Convert.ToInt32(ddlRegionE dit.Select edValue); // This returns old value
string modelName = txtSearchModelName.Text; // This returns and empty string
string transModel = txtTranslateModel.Text; // This returns and empty string
decimal price = Convert.ToDecimal(txtPrice Edit.Text) ; // this retuns the old value
if (row.RowType == DataControlRowType.DataRow )
{
HttpCookie cookie = Request.Cookies["DOTOOLS"] ;
string returnUser = string.Empty;
if (cookie != null)
{
string userID = cookie["UserID"];
string fname = cookie["FirstName"];
string lname = cookie["LastName"];
returnUser = fname.Substring(0, 1) + "." + lname;
}
UpdatedByUpdate(recID, returnUser, countryID, regionID, modelName, transModel, price);
}
}
Gridview HTML Code
<asp:GridView ID="gvMain" runat="server" AllowSorting="True" AutoGenerateColumns="False "
DataSourceID="dsrcGridView Main1" DataKeyNames="RecID,Countr yID,ModelN ame" OnRowDataBound="gvMain_Row DataBound" OnRowUpdating="gvMain_RowU pdating" OnRowUpdated="gvMain_RowUp dated">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkBu tton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkBu tton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButt on>
<asp:LinkButton ID="lnkDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkBu tton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RecID" HeaderText="RecID" InsertVisible="False" ReadOnly="True"
SortExpression="RecID" Visible="False" />
<asp:BoundField DataField="RegionID" HeaderText="RegionID" SortExpression="RegionID"
Visible="False" />
<asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID"
Visible="False" />
<asp:TemplateField HeaderText="CompanyCode" SortExpression="CompanyCod e">
<EditItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CompanyCode") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CompanyCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<EditItemTemplate>
<asp:DropDownList ID="ddlCountryEdit" runat="server" AppendDataBoundItems="True "
DataSourceID="dsrcCountryC odes" DataTextField="Country" DataValueField="CountryID" SelectedValue='<%# Bind("CountryID") %>'>
<asp:ListItem>-- Select Country --</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Region" SortExpression="Region">
<EditItemTemplate>
<asp:DropDownList ID="ddlRegionEdit" runat="server" AppendDataBoundItems="True "
DataSourceID="dsrcNewRegio ns" DataTextField="Region" DataValueField="RegionID" SelectedValue='<%# Bind("RegionID") %>'>
<asp:ListItem>-- Select Region --</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Region") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ModelName" SortExpression="ModelName" >
<EditItemTemplate>
<asp:TextBox ID="txtModelNameEdit" runat="server" Text='<%# Bind("ModelName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("ModelName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TranslateModel " SortExpression="TranslateM odel">
<EditItemTemplate>
<asp:TextBox ID="txtTransModelEdit" runat="server" Text='<%# Bind("TranslateModel") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("TranslateModel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<EditItemTemplate>
<asp:TextBox ID="txtPriceEdit" runat="server" Text='<%# Bind("Price") %>' Width="60px"></asp:TextBox >
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Price", "{0:0.00}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Updated" SortExpression="LastUpdate dOn">
<EditItemTemplate>
<asp:Label ID="lblUpdatedOn" runat="server" Text='<%# Bind("LastUpdatedOn", "{0:d}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("LastUpdatedOn", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="By" SortExpression="LastUpdate dBy">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastUpdatedBy") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastUpdatedBy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle ForeColor="White" />
<AlternatingRowStyle BackColor="#EFF3FB" />
</asp:GridView>
<asp:SqlDataSource ID="dsrcGridViewMain" runat="server" ConnectionString="<%$ ConnectionStrings:ControlT ablesConne ctionStrin g %>"
DeleteCommand="DELETE FROM dbo.AARP_DATA_D1_BillMatri x WHERE (RecID = @RecID) "
SelectCommand="SELECT RecID, CompanyID, RegionID, CountryID, CompanyCode, CompanyName, AppID, CountryCode, CountryName, RegionCompanyID, Region, ModelName, TranslateModel, Price, LastUpdatedOn, LastUpdatedBy FROM dbo.AARP_DATA_D1_BillMatri x_View;"
UpdateCommand="UPDATE dbo.AARP_DATA_D1_BillMatri x SET RegionID = @RegionID, CountryID = @CountryID, ModelName = @ModelName, TranslateModel = @TranslateModel, Price = @Price WHERE (RecID = @RecID)">
<DeleteParameters>
<asp:Parameter Name="RecID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="RegionID" />
<asp:Parameter Name="CountryID" />
<asp:Parameter Name="ModelName" />
<asp:Parameter Name="TranslateModel" />
<asp:Parameter Name="Price" />
<asp:Parameter Name="RecID" />
</UpdateParameters>
</asp:SqlDataSource>
I am using the RowUpdating Event of the Gridview to do updates because I need more flexibility than the SQLDatasource Update provides. So the problem I am having is that when I get the Datakey value I am trying to get the values in the controls in the Edit Mode of the current row I am trying to update. But I get some strange results. I am using the FindControl function to get the values from the editing row so I specifically name the dropdownlists and textboxes so I can access them here. But what happens is that I do get some of the values but they are the original values not the new ones being changed. In the code below I do get the CountryID and RegionID but they are the original values not the new ones selectedfrom the Edit Mode row. The Textbox values come back as empty, which is even stranger. Need some help figuring this out. Below is the code for both the GridView HTML and the C# code behind.
Code (C# OnUpdating Event)
protected void gvMain_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//---- Get Gridview Datakey Value and Dropdown value from selected Index ---
string recIDTxt = gvMain.DataKeys[e.RowIndex
int recID = Convert.ToInt32(recIDTxt);
//--- Define Gridview Row Elements
GridView grid = (GridView)sender;
GridViewRow row = (GridViewRow)grid.Rows[e.R
DropDownList ddlCountryEdit = (DropDownList)row.FindCont
DropDownList ddlRegionEdit = (DropDownList)row.FindCont
TextBox txtModelNameEdit = (TextBox)row.FindControl("
TextBox txtTransModelEdit = (TextBox)row.FindControl("
TextBox txtPriceEdit = (TextBox)row.FindControl("
int countryID = Convert.ToInt32(ddlCountry
int regionID = Convert.ToInt32(ddlRegionE
string modelName = txtSearchModelName.Text; // This returns and empty string
string transModel = txtTranslateModel.Text; // This returns and empty string
decimal price = Convert.ToDecimal(txtPrice
if (row.RowType == DataControlRowType.DataRow
{
HttpCookie cookie = Request.Cookies["DOTOOLS"]
string returnUser = string.Empty;
if (cookie != null)
{
string userID = cookie["UserID"];
string fname = cookie["FirstName"];
string lname = cookie["LastName"];
returnUser = fname.Substring(0, 1) + "." + lname;
}
UpdatedByUpdate(recID, returnUser, countryID, regionID, modelName, transModel, price);
}
}
Gridview HTML Code
<asp:GridView ID="gvMain" runat="server" AllowSorting="True" AutoGenerateColumns="False
DataSourceID="dsrcGridView
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkBu
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkBu
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButt
<asp:LinkButton ID="lnkDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkBu
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RecID" HeaderText="RecID" InsertVisible="False" ReadOnly="True"
SortExpression="RecID" Visible="False" />
<asp:BoundField DataField="RegionID" HeaderText="RegionID" SortExpression="RegionID"
Visible="False" />
<asp:BoundField DataField="CountryID" HeaderText="CountryID" ReadOnly="True" SortExpression="CountryID"
Visible="False" />
<asp:TemplateField HeaderText="CompanyCode" SortExpression="CompanyCod
<EditItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CompanyCode") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("CompanyCode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="Country">
<EditItemTemplate>
<asp:DropDownList ID="ddlCountryEdit" runat="server" AppendDataBoundItems="True
DataSourceID="dsrcCountryC
<asp:ListItem>-- Select Country --</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Region" SortExpression="Region">
<EditItemTemplate>
<asp:DropDownList ID="ddlRegionEdit" runat="server" AppendDataBoundItems="True
DataSourceID="dsrcNewRegio
<asp:ListItem>-- Select Region --</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Region") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ModelName" SortExpression="ModelName"
<EditItemTemplate>
<asp:TextBox ID="txtModelNameEdit" runat="server" Text='<%# Bind("ModelName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("ModelName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TranslateModel
<EditItemTemplate>
<asp:TextBox ID="txtTransModelEdit" runat="server" Text='<%# Bind("TranslateModel") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("TranslateModel") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<EditItemTemplate>
<asp:TextBox ID="txtPriceEdit" runat="server" Text='<%# Bind("Price") %>' Width="60px"></asp:TextBox
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Price", "{0:0.00}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Updated" SortExpression="LastUpdate
<EditItemTemplate>
<asp:Label ID="lblUpdatedOn" runat="server" Text='<%# Bind("LastUpdatedOn", "{0:d}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("LastUpdatedOn", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="By" SortExpression="LastUpdate
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastUpdatedBy") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("LastUpdatedBy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle ForeColor="White" />
<AlternatingRowStyle BackColor="#EFF3FB" />
</asp:GridView>
<asp:SqlDataSource ID="dsrcGridViewMain" runat="server" ConnectionString="<%$ ConnectionStrings:ControlT
DeleteCommand="DELETE FROM dbo.AARP_DATA_D1_BillMatri
SelectCommand="SELECT RecID, CompanyID, RegionID, CountryID, CompanyCode, CompanyName, AppID, CountryCode, CountryName, RegionCompanyID, Region, ModelName, TranslateModel, Price, LastUpdatedOn, LastUpdatedBy FROM dbo.AARP_DATA_D1_BillMatri
UpdateCommand="UPDATE dbo.AARP_DATA_D1_BillMatri
<DeleteParameters>
<asp:Parameter Name="RecID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="RegionID" />
<asp:Parameter Name="CountryID" />
<asp:Parameter Name="ModelName" />
<asp:Parameter Name="TranslateModel" />
<asp:Parameter Name="Price" />
<asp:Parameter Name="RecID" />
</UpdateParameters>
</asp:SqlDataSource>
ASKER
It still gets the old values, when I loop thru the Dictionary and in debug step thru it I see it still gets the original values not the new values that the user enters when they are changing each item.
// Iterate through the NewValues collection and HTML encode all
// user-provided values before updating the data source.
foreach (DictionaryEntry entry in e.NewValues)
{
e.NewValues[entry.Key] = Server.HtmlEncode(entry.Va lue.ToStri ng());
}
// Iterate through the NewValues collection and HTML encode all
// user-provided values before updating the data source.
foreach (DictionaryEntry entry in e.NewValues)
{
e.NewValues[entry.Key] = Server.HtmlEncode(entry.Va
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
guru_sami,
sorry it took me so long to get back, I have been off on vacation and just got back, but yes you are right on, I went back and debugged the entire postback and it was rebinding the grid on postback on the page_load event. Thanks good call.
sorry it took me so long to get back, I have been off on vacation and just got back, but yes you are right on, I went back and debugged the entire postback and it was rebinding the grid on postback on the page_load event. Thanks good call.
ASKER
Thank you your analysis was right on the money.
Check two example here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx