How do I get the old values in a gridview row in the row_updating event?
I'm using a sqldatasource control to populate the grid and want to write the update code in the RowUpdating event.
There are 10 fields displayed in the grid.
I can get the value of the first field using
string old_Category = e.OldValues[0].ToString();.
But it doesn't work for index items greater than 0. This doesn't work
string old_Category = e.OldValues[1].ToString();
it returns an error message
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
even though there are 10 columns in the grid.
========================================
Here's the gridview markup
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
BorderWidth="0px"
BorderStyle="None"
Width="100%"
CellPadding="2"
PageSize="25"
OnRowDeleting="ProjectListGridView_RowDeleting"
onrowdatabound="GridView1_RowDataBound"
onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing" >
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="WeekEnding" SortExpression="WeekEnding">
<ItemTemplate>
<asp:Label ID="lblWeekEnding" runat="server" Text='<%# Bind("WeekEnding") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project" SortExpression="Project">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownList1"
runat="server"
DataSourceID="sdsDdlProjectsEdit"
DataTextField="ProjectName"
DataValueField="ProjectID"
AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
>
</asp:DropDownList>
<asp:SqlDataSource
ID="sdsDdlProjectsEdit"
runat="server"
ConnectionString="<%$ ConnectionStrings:ttuser %>"
SelectCommand="SELECT ProjectID,ProjectName FROM dbo.aspnet_starterkits_Projects">
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Project") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<EditItemTemplate>
<asp:DropDownList
ID="DropDownList2"
runat="server"
DataSourceID="sdsDDL2"
DataTextField="CategoryName"
DataValueField="CategoryID">
</asp:DropDownList>
<asp:SqlDataSource
runat="server"
ID="sdsDDL2"
ConnectionString="<%$ ConnectionStrings:ttuser %>"
SelectCommand="SELECT [CategoryID], [CategoryName],[ProjectID] FROM dbo.aspnet_starterkits_ProjectCategories WHERE ([ProjectID]=#ProjectID)">
<SelectParameters>
<asp:Parameter Name="ProjectID" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sun" SortExpression="Sun">
<EditItemTemplate>
<asp:TextBox ID="txtSunEdit" runat="server" Text='<%# Eval("Sun") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSun" runat="server" Text='<%# Bind("Sun") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mon" SortExpression="Mon">
<EditItemTemplate>
<asp:TextBox ID="txtMonEdit" runat="server" Text='<%# Eval("Mon") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Mon") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tue" SortExpression="Tue">
<EditItemTemplate>
<asp:TextBox ID="txtTueEdit" runat="server" Text='<%# Eval("Tue") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Tue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wed" SortExpression="Wed">
<EditItemTemplate>
<asp:TextBox ID="txtWedEdit" runat="server" Text='<%# Eval("Wed") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Wed") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Thu" SortExpression="Thu">
<EditItemTemplate>
<asp:TextBox ID="txtThuEdit" runat="server" Text='<%# Eval("Thu") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Thu") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fri" SortExpression="Fri">
<EditItemTemplate>
<asp:TextBox ID="txtFriEdit" runat="server" Text='<%# Eval("Fri") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Fri") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sat" SortExpression="Sat">
<EditItemTemplate>
<asp:TextBox ID="txtSatEdit" runat="server" Text='<%# Eval("Sat") %>' Columns="2"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Sat") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and here is the sqldatasource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ttuser %>"
SelectCommand="SELECT
#WeekEnding WeekEnding
,Project
,ProjectID
,Category
,CategoryID
,isnull([1], 0) Sun
,isnull([2], 0) Mon
,isnull([3], 0) Tue
,isnull([4], 0) Wed
,isnull([5], 0) Thu
,isnull([6], 0) Fri
,isnull([7], 0) Sat
from
(select P.ProjectName Project,P.ProjectID, CAT.CategoryName Category,CAT.CategoryID, Datepart(dw, TE.TimeEntryDate) DOW, TE.TimeEntryDuration Hours
FROM
dbo.aspnet_starterkits_TimeEntry TE inner join
dbo.aspnet_starterkits_ProjectCategories CAT on
TE.CategoryID=CAT.CategoryID inner join
dbo.aspnet_starterkits_Projects P on
CAT.ProjectID=P.ProjectID
Where (TE.TimeEntryDate between dateadd(dd, -6, #WeekEnding) and #WeekEnding) AND
TE.TimeEntryUserID=(SELECT UserId FROM dbo.aspnet_Users WHERE UserName=#UserName)) Source
pivot (max(Hours)for DOW in ([1],[2],[3],[4],[5],[6],[7]) ) as pvt" OldValuesParameterFormatString="original_{0}"
<SelectParameters>
<asp:ControlParameter ControlID="WeekEnding2" Name="WeekEnding"
PropertyName="Text" />
<asp:ControlParameter ControlID="UserList" Name="UserName"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Just set Enableviewstate property to false of gridview you can get new value
Reference:
http://www.codeproject.com/Questions/139076/not-getting-new-entered-values-on-grid-view-row-up
Just a long shot really. Have you tried setting the DataKeyNames property?
<asp:gridview id="GridView1"
...
DataKeyNames="ProjectID"
/>
From msdn
Use the OldValues property
(dictionary) to access the original
values of the fields in the row to
update. This dictionary contains all
fields in the row except the key
fields. The key fields are defined in
the DataKeyNames property of a
GridView control.
Related
I have been working on this already for hours but no result. Here is my code:
Dim first_name As String = (TryCast(row.FindControl("txtfirst_name"), TextBox)).Text
Dim last_name As String = (TryCast(row.FindControl("txtlast_name"), TextBox)).Text
Dim birth_date As DateTime = (TryCast(row.FindControl("txtbirth_date"), TextBox)).Text
Dim gender As String = (TryCast(row.FindControl("txtgender"), TextBox)).Text
And here my items in the gridview for the textboxes and labels
<asp:TemplateField HeaderText="First Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblfirst_name" runat="server" Text='<%# Eval("first_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtfirst_name" runat="server" Text='<%# Eval("first_name") %>' Width="140"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lbllast_name" runat="server" Text='<%# Eval("last_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtlast_name" runat="server" Text='<%# Eval("last_name") %>' Width="140"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birthday" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblbirth_date" runat="server" Text='<%# Eval("birth_date") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtbirth_date" runat="server" Text='<%# Eval("birth_date") %>' Width="140"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblgender" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtgender" runat="server" Text='<%# Eval("gender") %>' Width="140"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
I'm using this to update gridview in ASP.NET. I have already entered new values in the textboxes but the code above still returns the old value. Please help!
I have a checkbox on my griview and its able to be clicked before the edit button is clicked, this doesn't make sense to me and its not something I have came across before. In theory the checkbox should be greyed out until a user clicks the edit button.
Nothing can be updated but it just doesn't make any sense as to why this functionality would be available. I have set up Gridviews before using checkboxes and never came across this. Below is my code:
<asp:GridView ID="gvLeagues" runat="server"
AutoGenerateColumns="False"
onpageindexchanging="gvLeagues_PageIndexChanging"
onrowcancelingedit="gvLeagues_RowCancelingEdit"
onrowdatabound="gvLeagues_RowDataBound"
onrowediting="gvLeagues_RowEditing"
onrowupdating="gvLeagues_RowUpdating"
onsorting="gvLeagues_Sorting" EnableModelValidation="True"
CssClass="footable"
EditRowStyle-CssClass="table table-bordered" >
No Data Found.
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("Name")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created Date" SortExpression="CreatedDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly="true" Text='<%# Bind("CreatedDate")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label2" runat="server" Text='<%# Bind("CreatedDate")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Day" SortExpression="Day">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Day")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label3" runat="server" Text='<%# Bind("Day")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Season" SortExpression="Season">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Season")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate><asp:Label ID="Label4" runat="server" Text='<%# Bind("Season")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enabled" SortExpression="Enabled">
<EditItemTemplate>
<asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' />
</EditItemTemplate>
<ItemTemplate><asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
When using the ItemTemplate, you are the one specifying what and how you want to see it. Just set that checkbox as disabled.
<asp:TemplateField HeaderText="Enabled" SortExpression="Enabled">
<EditItemTemplate>
<asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' />
</EditItemTemplate>
<ItemTemplate><asp:CheckBox ID="chkEnabled" runat="server" Checked='<%# Eval("Enabled")%>' enabled="False"/></ItemTemplate>
</asp:TemplateField>
I am trying to build a simple GridView.
I have a Products table and it contains ProductID and ModelName.
I am using LINQ to SQL
My GridView code is
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Eval("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my datasource code is
<asp:ObjectDataSource ID="objData" runat="server" SelectMethod="GetAll" UpdateMethod="UpdateProduct"
DataObjectTypeName="TestLINQProto.tblProduct" TypeName="TestLINQProto.ProductsList"></asp:ObjectDataSource>
I have my update method written like this
[DataObjectMethod(DataObjectMethodType.Update,true)]
public void UpdateProduct(tblProduct product)
{
EshopDataAccess.UpdateProduct(product);//This will call a LINQ function to update the product
}
The issue is that the tblProduct that the UpdateProduct contain original values. It is not getting the updated values.
I think the issue is that you are using Eval method which only supports reading/fetching of data. Use Bind Method in your grid aspx code. Like this
<asp:GridView ID="GridView1" runat="server" DataSourceID="objData" AutoGenerateColumns="false" AutoGenerateEditButton="true" DataKeyNames="ProductID,ModelName">
<Columns>
<asp:TemplateField HeaderText="Product ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ProductID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ModelName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#Bind("ModelName") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am getting this exception at runtime which says it cannot find a column 'M' in datasource, but i am not using 'M' anywhere. I am trying to bind data to dropdownlist inside gridview.
I need to do this in .aspx page rather than code behind.
Here's the code i am using:
<asp:GridView ID="grdDrpDownlistSample" runat="server" AutoGenerateColumns="false" DataSourceID="sqlDS1">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name").ToString()%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logged In Status">
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server" Checked='<%# Eval("LoggedIn") %>'></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sex">
<ItemTemplate>
<asp:DropDownList ID="drpSex" DataSourceID="sqlDS1" runat="server" DataTextField='<%# Eval("Sex") %>' DataValueField='<%# Eval("id") %>' ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlDS1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select * from Users"> </asp:SqlDataSource>
It's working now.
I have got this working by removing Eval from dropdownlist, as it was expecting just the column name not the Eval expression.
Simple question? I have a gridview with an insert row in the footer and i have validation controls for that row and the edittemplate how do get it to only validate for that row because when i do an edit it validates the insert row aswell. I just wnat to validate the insert row or edit row.
<asp:GridView ID="PageSettings" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
DataKeyNames="Id"
DataSourceID="ObjectDataSourcePages"
OnLoad="PageSettings_Load"
OnRowDataBound="PageSettings_DataBound"
OnRowCommand="PageSettings_RowCommand"
OnRowCreated="PageSettings_RowCreated"
OnRowEditing="PageSettings_RowEditing"
OnRowCancelingEdit="PageSettings_RowCancelingEdit"
OnRowUpdating="PageSettings_RowUpdating"
OnPageIndexChanging="PageSettings_PageIndexChanging"
OnSorting="PageSettings_Sorting"
OnSorted="PageSetting_Sorted"
PageSize="2"
ShowFooter="True"
ShowHeaderWhenEmpty="True">
<Columns>
<asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireName" runat="server"
ControlToValidate="Name"
CssClass="gridview-error"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireInsertName" runat="server"
ControlToValidate="InsertName"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left" SortExpression="Path">
<ItemTemplate>
<%# Eval("Path") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequirePath" runat="server"
ControlToValidate="Path"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertPath" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireInsertPath" runat="server"
ControlToValidate="InsertPath"
Display="Dynamic"
SetFocusOnError="true">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left" SortExpression="RouteValue">
<ItemTemplate>
<%# Eval("RouteValue") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertRouteValue" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left" SortExpression="RegExp">
<ItemTemplate>
<%# Eval("RegExp") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="InsertRegExp" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use a
Validation Group
Define a separate group for each operation
SaveRecords
&
EditRecords
. You have to specify each validation group on the Save/Edit buttons & all corresponding validation controls to fire them for that event.
MSDN
After a long Time
but you must add in footer requiredfieldvalidator a validationgroup with a unique name and add this validationgroup to add button