ASP.NET GridView not saving when textbox is empty - asp.net

I have a GridView that is editable with an entitydatasource, if my users want to delete the text in one of the fields they cannot. They can replace the text with a space which then saves, but if the textbox is completely empty the save operation doesn't do anything.
How do I combat this?
Thanks
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=CS3Entities" DefaultContainerName="CS3Entities"
EnableFlattening="False" EnableUpdate="True"
EntitySetName="ProductAlertStatuses"
Where="it.AlertType != '0'"
OrderBy="it.Department DESC, it.AlertType DESC"/>
<br/>
<asp:GridView runat="server" ID="ActionPlanGV"
DataSourceID="EntityDataSource1"
OnDataBound="ActionPlanGV_OnRowDataBound"
CssClass="mGrid visibleGrid"
AutoGenerateColumns="False"
DataKeyNames="ProductStatusID">
<Columns>
<asp:TemplateField SortExpression="Department" ItemStyle-CssClass="center">
<HeaderTemplate>
<asp:Label ID="DepartmentColHeader" ToolTip="Moulding or Finishing Department" runat="server" Text="Dept" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="DepartmentColtxt" runat="server" Text='<%# Eval("Department") %>' />
</ItemTemplate>
<ItemStyle CssClass="center"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField SortExpression="AlertType" ItemStyle-CssClass="center">
<HeaderTemplate>
<asp:Label ID="AlertTypeColHeader" ToolTip="Alert Type" runat="server" Text="Alert" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="AlertTypeColtxt" runat="server" Text='<%# ItemDAL.GetAlertStatusColourFromNumber(Eval("AlertType").ToString()) %>' />
</ItemTemplate>
<ItemStyle CssClass="center"></ItemStyle>
</asp:TemplateField>
<%--<asp:BoundField DataField="DateAlertRaised" DataFormatString="{0:d}" HeaderText="Date Alert Raised" ReadOnly="True" SortExpression="DateAlertRaised" />--%>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="ItemIDHeader" ToolTip="Item Code" runat="server" Text="Item" />
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink ID="ItemID"
runat="server"
Text='<%# Eval("ItemID") %>'
NavigateUrl='<%# Url.GetUrl("ViewItem", Eval("ItemID")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scrap /Parts" ItemStyle-Width="7%" SortExpression="ActionPlanScrapParts">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" TextMode="MultiLine" Width="75" Height="75" runat="server" Text='<%# Bind("ActionPlanScrapParts") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ActionPlanScrapParts") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Defect Issue / Description" ItemStyle-Width="15%" SortExpression="ActionPlanDefectDescription">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" TextMode="MultiLine" Width="100" Height="75" runat="server" Text='<%# Bind("ActionPlanDefectDescription") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ActionPlanDefectDescription") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Root Cause" ItemStyle-Width="15%" SortExpression="ActionPlanRootCause">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" TextMode="MultiLine" Width="100" Height="75" runat="server" Text='<%# Bind("ActionPlanRootCause") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ActionPlanRootCause") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Corrective Action<div style='font-size:8px;line-height:8px;'>FN16, FN17, FN03, FN05,<br/>Change Note Number</div>-Who? Due By?" ItemStyle-Width="20%" SortExpression="ActionPlanCorrectiveAction">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" TextMode="MultiLine" Width="100" Height="75" runat="server" Text='<%# Bind("ActionPlanCorrectiveAction") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ActionPlanCorrectiveAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preventative Action<br/>-Who? Due By?" ItemStyle-Width="15%" SortExpression="ActionPlanPreventativeAction">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" TextMode="MultiLine" Width="100" Height="75" runat="server" Text='<%# Bind("ActionPlanPreventativeAction") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("ActionPlanPreventativeAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reviewed & Closed" ItemStyle-Width="10%" SortExpression="ActionPlanReviewedClosed">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" TextMode="MultiLine" Width="75" Height="75" runat="server" Text='<%# Bind("ActionPlanReviewedClosed") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("ActionPlanReviewedClosed") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ItemStyle-CssClass="noprint" HeaderStyle-CssClass="noprint">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update" Text="Save"/>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" Text="Cancel"/>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbEdit2" runat="server" CommandName="Edit" Text="Edit"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There is one codebehind method, but that doesn't touch the data:
protected void ActionPlanGV_OnRowDataBound(object sender, EventArgs eventArgs)
{
foreach (GridViewRow row in ActionPlanGV.Rows)
{
var alertTypeColtxt = (Label)row.FindControl("AlertTypeColtxt");
if (alertTypeColtxt != null)
{
if (alertTypeColtxt.Text == "Red")
alertTypeColtxt.Text = "<svg width='18' height='18'><circle cx='9' cy='9' r='8' fill='red' /></svg>";
if (alertTypeColtxt.Text == "Amber")
alertTypeColtxt.Text =
"<svg width='15' height='15'><rect x='0' y='0' height='15' width='15' fill='orange' /></svg>";
if (alertTypeColtxt.Text == "Green")
alertTypeColtxt.Text = "<svg width='18' height='18'><circle cx='9' cy='9' r='8' fill='green' /></svg>";
}
}
}

Related

Updating gridview row NOT finding data is gridview row

I have looked for a couple of days now and for some reason I am unable to solve my issue. I have a gridview control and wanting to update the row that is selected. I am trying to populate vidInformaiton class, passes to my stored procedure.
<asp:GridView ID="gvVideos" CssClass="gvVideosClass" runat="server"
AutoGenerateColumns="False" DataKeyNames="CustomerId"
OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records has been added.">
<Columns>
<asp:TemplateField HeaderText="Customer">
<EditItemTemplate>
<asp:TextBox ID="customerId" runat="server" Text='<%# Bind("customerId")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcustomerID" runat="server" Text='<%# Bind("customerId")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Fid" ItemStyle-Width="50">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
<input type="hidden" name="vidId" value='<%# Eval("fId")%>' />
</ItemTemplate>
<ItemStyle Width="50px"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("typeContent")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("typeContent")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Youtube ID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ytid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("ytid") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("descvid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("descvid") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("thumbnail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("thumbnail") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="SubmitDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("submitdate") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("submitdate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server" Checked='<%# Eval("active")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Categories">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("categories") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("categories") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Sort order">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("sortord") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("sortord") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150"/>
</Columns>
</asp:GridView>
<br />
</div>
My code behind look like this. not sure what I am missing. I can't seem to get the values from the row. I have tried everything, DirectCast, FindControl.
Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim updVid As New VidInformation
Dim updVidRecord As New vidController
Dim row = gvVideos.Rows(e.RowIndex)
updVid.customerId = row.Cells(1).Text.ToString
gvVideos.EditIndex = -1
gvVideos.DataSource = updVidRecord.UpdateVidRecord(updVid)
bindGridview()
End Sub
Converting series of helpful comments to an answer
The correct way to get new values posted to edited GridView row is to use NewValues property of the even args object:
Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
...
Dim updVid As New VidInformation
updVid.customerId = e.NewValues("customerId")
...
End Sub
As to why initial approach did not work. Important thing to realize is that GridView row in question was in edit mode during the previous load of the page. During the post back which edit button triggered new values are posted to the server, but the row is no longer in edit mode, so there are no more edit controls to be found in this row. Thus the method described above is the only by-design way to obtain new values for the row.

Failed to load viewstate - dynamic controls in ASP.NET using nested GridView

In my project I am working on showing Task Group and Task (i.e. Parent-child) in one screen with the ability to Add/Edit/Delete the parent (Task Group) record and also to Add/Edit/Delete associated Task. For this I am using nested Grid View (parent-child).
I am implementing the sample application mentioned in the below link to my ASP.NET web based project.
http://www.codeproject.com/Articles/20047/Editable-Nested-GridView-All-in-One
my .ASPX looks as mentioned below:
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Unicorn.Master" CodeBehind="TaskAndTaskGroup.aspx.cs" Inherits="Vipassi.Web.TaskGroup.TaskAndTaskGroup"%>
<asp:Content ID="Content1" ContentPlaceHolderID="phContent" runat="server">
<script language="javascript" type="text/javascript">
function expandcollapse(obj, row) {
var div = document.getElementById(obj);
var img = document.getElementById('img' + obj);
if (div.style.display == "none") {
div.style.display = "block";
if (row == 'alt') {
img.src = "../../Resources/images/minus.gif";
}
else {
img.src = "../../Resources/images/minus.gif";
}
img.alt = "Close to view other TaskGroups";
}
else {
div.style.display = "none";
if (row == 'alt') {
img.src = "../../Resources/images/plus.gif";
}
else {
img.src = "../../Resources/images/plus.gif";
}
img.alt = "Expand to show Tasks";
}
}
</script>
<div>
<asp:GridView ID="GridView1" AllowPaging="True" BackColor="#f1f1f1"
AutoGenerateColumns="false" DataSourceID="ObjectDataSource1" DataKeyNames="task_group_id"
ShowFooter="true" Font-Size="Small"
Font-Names="Verdana" runat="server" GridLines="None" OnRowDataBound="GridView1_RowDataBound"
OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" BorderStyle=Outset
OnRowDeleting="GridView1_RowDeleting" OnRowDeleted="GridView1_RowDeleted"
OnRowUpdated="GridView1_RowUpdated" AllowSorting="true">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:expandcollapse('div<%# Eval("task_group_id") %>', 'one');">
<img id="imgdiv<%# Eval("task_group_id") %>" alt="Click to show/hide Tasks for TaskGroup <%# Eval("task_group_id") %>" width="9px" border="0" src="../../Resources/images/plus.gif" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TaskGroup ID" SortExpression="task_group_id">
<ItemTemplate>
<asp:Label ID="lblTaskGroupID" Text='<%# Eval("task_group_id") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTaskGroupID" Text='<%# Eval("task_group_id") %>' runat="server"></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskGroupID" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task Group" SortExpression="task_group">
<ItemTemplate><%# Eval("task_group") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskGroup" Text='<%# Eval("task_group") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskGroup" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fiscal Year" SortExpression="Fiscal_Year">
<ItemTemplate><%# Eval("Fiscal_Year") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFiscalYear" Text='<%# Eval("Fiscal_Year") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFiscalYear" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="Active_Flag">
<ItemTemplate><%# Eval("Active_Flag")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtActiveFlag" Text='<%# Eval("Active_Flag") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtActiveFlag" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaskGroup" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTaskGroup" CommandName="AddTaskGroup" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="div<%# Eval("task_group_id") %>" style="display: none; position: relative; left: 15px; overflow: auto; width: 97%">
<asp:GridView ID="GridView2" AllowPaging="True" AllowSorting="true" BackColor="White" Width="100%" Font-Size="X-Small"
AutoGenerateColumns="false" Font-Names="Verdana" runat="server" DataKeyNames="task_group_id" ShowFooter="true"
OnPageIndexChanging="GridView2_PageIndexChanging" OnRowUpdating="GridView2_RowUpdating"
OnRowCommand="GridView2_RowCommand" OnRowEditing="GridView2_RowEditing" GridLines="None"
OnRowUpdated="GridView2_RowUpdated" OnRowCancelingEdit="GridView2_CancelingEdit" OnRowDataBound="GridView2_RowDataBound"
OnRowDeleting="GridView2_RowDeleting" OnRowDeleted="GridView2_RowDeleted" OnSorting="GridView2_Sorting"
BorderStyle="Double" BorderColor="#0083C1">
<RowStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#0083C1" ForeColor="White" />
<FooterStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Task ID" SortExpression="task_id">
<ItemTemplate>
<asp:Label ID="lblTaskID" Text='<%# Eval("task_id") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTaskID" Text='<%# Eval("task_id") %>' runat="server"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task" SortExpression="task_name">
<ItemTemplate><%# Eval("task_name")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTask" Text='<%# Eval("task_name")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTask" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fiscal Year" SortExpression="fiscal_year">
<ItemTemplate><%# Eval("fiscal_year")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskFiscalYear" Text='<%# Eval("fiscal_year")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskFiscalYear" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="Active_Flag">
<ItemTemplate><%# Eval("Active_Flag")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTaskActiveFlag" Text='<%# Eval("Active_Flag")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTaskActiveFlag" Text='' runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="True" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="linkDeleteTaskGroup" CommandName="Delete" runat="server">Delete</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkAddTask" CommandName="AddTask" runat="server">Add</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource SortParameterName="sortExpression" EnablePaging="true" ID="ObjectDataSource1"
MaximumRowsParameterName="maxRecords" TypeName="Vipassi.BLL.Classes.TaskGroup.TaskGroupBLL"
SelectMethod="ListforTaskGroup" SelectCountMethod="ListCountforTaskGroup" StartRowIndexParameterName="startIndex"
runat="server">
<SelectParameters>
<asp:SessionParameter SessionField="orgId" Name="orgId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
Option 1:
You can disable the VIEWSTATE of ASPX page /User Control :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="~/TaskAndTaskGroup.aspx.cs" Inherits="Vipassi.Web.TaskGroup.TaskAndTaskGroup" EnableViewState="false"%>
Option 2:
You can also change your source code to make sure the controls added during a post-back must match the type and position of the controls added during the initial request.
Hope this will help you.

Modifying edited field of a DetailsView before inserting it to database when in "insert mode"

On one of my final year project page I have a DetailsView that I am using to update/edit a GridView.
I have a password field that must show the hashed password.
When I am in "edit" or "insert" mode in the DetailsView and inserting a new password in the field (or editing the field) I would like to hash directly the password before showing it in the GridView/DetailsView and of course before inserting/updating it in the DataBase.
My GridView & DetailsView are both in the same UpdatePanel and I am using a ObjectDataSource.
That a partial code:
<asp:DetailsView ID="DetailsViewSingleUsr" runat="server" Height="50px"
Width="125px" DataSourceID="SingleUserObjectDataSource" DataKeyNames="id"
onitemdeleted="DetailsViewSingleUsr_ItemDeleted"
oniteminserted="DetailsViewSingleUsr_ItemInserted"
onitemupdated="DetailsViewSingleUsr_ItemUpdated" AutoGenerateRows="False"
oniteminserting="DetailsViewSingleUsr_ItemInserting">
<Fields>
<asp:TemplateField HeaderText="User ID" InsertVisible="False"
SortExpression="id">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName" SortExpression="username">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="Username may only contain alphanumeric characters"
ValidationExpression="^[a-zA-Z0-9_]*$" SetFocusOnError="True"></asp:RegularExpressionValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="UserName already exist." SetFocusOnError="True" Display="Dynamic"
ControlToValidate="TextBox1" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lastname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lastname") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("lastname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="pass">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pass") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pass") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("pass") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Score" SortExpression="currentScore">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("currentScore") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("currentScore") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("currentScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Best Score" SortExpression="maxScore">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("maxScore") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("maxScore") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("maxScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True"
CommandName="Update" ImageUrl="~/images/update.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="~/images/cancel.png" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True"
CommandName="Insert" ImageUrl="~/images/insert.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="~/images/cancel.png" />
</InsertItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False"
CommandName="Edit" ImageUrl="~/images/edit.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="New" ImageUrl="~/images/new.png"/>
<asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False"
CommandName="Delete" ImageUrl="~/images/delete.png"/>
</ItemTemplate>
<ControlStyle Height="20px" Width="20px" />
</asp:TemplateField>
</Fields>
</asp:DetailsView>
I am able to access the password textbox in the code behind in the "ItemInserting" Event with that code:
protected void DetailsViewSingleUsr_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
DetailsView myDetailsView = (DetailsView)sender;
if (myDetailsView.CurrentMode == DetailsViewMode.Insert)
{
Label9.Text = ((TextBox)myDetailsView.FindControl("TextBox4")).Text;
}
}
But how can I modify? Am I on the right way? Any suggestions?
Thx a lot guys!
ListViewInsertEventArgs contains property Values, in which you can find all of inserting values, obtained from controls. You may just modify those data. For example
e.Values["password"] = "newpassword";
Thanks! I've finally end up with this code:
protected void DetailsViewSingleUsr_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["pass"] = LoginSecurityLogic.Hash_Password_to_MD5(e.Values["pass"].ToString());
}
protected void DetailsViewSingleUsr_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
e.NewValues["pass"] = LoginSecurityLogic.Hash_Password_to_MD5(e.NewValues["pass"].ToString());
}

GridView going out of page asp.net

GridView is going out of page... it goes further out in edit mode... here's the code for the gridview :
How do I get it to stay within the page (even in edit mode)...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" DataKeyNames="EmployeeID" Visible="False"
Width="900px">
<Columns>
<asp:TemplateField HeaderText="EmployeeID" SortExpression="EmployeeID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("EmployeeID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB" SortExpression="DOB">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("DOB") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("DOB") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="Address">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ContactNo" SortExpression="ContactNo">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ContactNo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("ContactNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmailID" SortExpression="EmailID">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("EmailID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("EmailID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DesignationID" SortExpression="DesignationID">
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("DesignationID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("DesignationID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation" SortExpression="Designation">
<EditItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Designation") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Designation") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department" SortExpression="Department">
<EditItemTemplate>
<asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Department") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DepartmentID" SortExpression="DepartmentID">
<EditItemTemplate>
<asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("DepartmentID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("DepartmentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOJ" SortExpression="DOJ">
<EditItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("DOJ") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label13" runat="server" Text='<%# Bind("DOJ") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ReportsTo" SortExpression="ReportsTo">
<EditItemTemplate>
<asp:TextBox ID="TextBox12" runat="server" Text='<%# Bind("ReportsTo") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("ReportsTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's the screenshot
any suggesstions that might work?
This is what worked:
<div style="width:90%;overflow:auto">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" DataKeyNames="EmployeeID" Visible="False" style="width:100%;overflow:auto"
HorizontalAlign="Center" >
Thnx...
Set Width="100%" .Dont use pixel .
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" DataKeyNames="EmployeeID" Visible="False"
Width="100%">

ASP.NET how to hide template field control based on another template field value in GridView

here my code:
<asp:GridView ID="_gvPLCs" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="4" DataSourceID="_dsPLCs" ForeColor="Black" GridLines="Horizontal"
onrowcommand="_gvPLCs_RowCommand" onrowdatabound="_gvPLCs_RowDataBound">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbnView" runat="server" CausesValidation="false"
CommandName="view" Text="View manual"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLCName" SortExpression="PLCName">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PLCName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PLCName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PLCManual" SortExpression="PLCManual">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("PLCManual") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("PLCManual") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LabName" SortExpression="LabName">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LabName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LabName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MachineName" SortExpression="MachineName">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("MachineName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("MachineName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MaxAllowedToResDay"
SortExpression="MaxAllowedToResDay">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("MaxAllowedToResDay") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server"
Text='<%# Bind("MaxAllowedToResDay") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
code behind:
protected void _gvPLCs_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
if (e.Row.Cells[2].Text == " ")
{
LinkButton lbnview = (LinkButton)e.Row.FindControl("lbnView");
lbnview.Visible = false;
}
}
this doesn't work with me..plz help
what i need is : when there is no a manual;the view manual link disappear
i tested it using this code :
if (e.Row.Cells[2].Text == string.Empty)
{
LinkButton lbnview = (LinkButton)e.Row.FindControl("lbnView");
lbnview.Visible = false;
}
but it always give me "" and always manaul field disappears
try this:
<asp:LinkButton ID="lbnView" runat="server" CausesValidation="false" CommandName="view" Text="View manual" Visible='<%# !string.IsNullOrEmpty(Bind("PLCName").ToString()) %>'></asp:LinkButton>

Resources