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.
Related
I am working on an asp.net project where I have a gridview on the default.aspx page databound to my sql database. When I click on a specific row it navigates to a details.aspx page where you can see and edit the details of the row clicked.
On that details.aspx page is another gridview where I exposed the footer and added and "Insert" linkbutton and the appropriate controls for each column to add a new record into the database. I have the entire project coded, but unfortunately nothing happens when I click on "Insert" linkbutton (Except the text in the controls all clear). No exceptions are thrown, but no record is added to the database. Here is the gridview code:
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" DataKeyNames="ClientId" DataSourceID="SqlDataSource1" ShowFooter="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="ClientId" InsertVisible="False" SortExpression="ClientId">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ClientId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClientId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbInsert" runat="server" CommandName="insert" CausesValidation="false">Insert</asp:LinkButton>
</FooterTemplate>
</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>
<FooterTemplate>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MiddleName" SortExpression="MiddleName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MiddleName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("MiddleName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMiddleName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Gender") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddGender" runat="server">
<asp:ListItem>M</asp:ListItem>
<asp:ListItem>F</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DateOfBirth" SortExpression="DateOfBirth">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("DateOfBirth") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("DateOfBirth") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ZipCode" SortExpression="ZipCode">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("ZipCode") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("ZipCode") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtZIP" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
and here is the codebehind:
Private Sub grid1_RowCommand1(sender As Object, e As GridViewCommandEventArgs) Handles grid1.RowCommand
If e.CommandName = "insert" Then
SqlDataSource1.InsertParameters("FirstName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtFirstName"), TextBox).Text
SqlDataSource1.InsertParameters("MiddleName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtMiddleName"), TextBox).Text
SqlDataSource1.InsertParameters("LastName").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtLastName"), TextBox).Text
SqlDataSource1.InsertParameters("Gender").DefaultValue = DirectCast(grid1.FooterRow.FindControl("ddGender"), DropDownList).SelectedValue
SqlDataSource1.InsertParameters("DateOfBirth").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtDOB"), TextBox).Text
SqlDataSource1.InsertParameters("ZipCode").DefaultValue = DirectCast(grid1.FooterRow.FindControl("txtZIP"), TextBox).Text
SqlDataSource1.Insert()
End If
End Sub
Thanks for any assistance in sorting this out!
John
DefaultValue doesn't set the value of what should be inserted, but rather provides resolution for null values for types that don't allow them (Integer, Long, etc). See this link for more information on what that Property does.
What you'll want to do is use Control Parameters in your SqlDataSource to bind the values of your TextBoxes to the parameters used by the InsertCommand. Use the following as an example:
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:YourConnection%>"
insertcommand="INSERT INTO SomeTable (FirstName, MiddleName) VALUES (#FirstName, #MiddleName)">
<insertparameters>
<asp:controlparameter name="FirstName" controlid="txtFirstName" propertyname="Text"/>
<asp:controlparameter name="MiddleName" controlid="txtLastName" propertyname="Text"/>
</insertparameters>
</asp:sqldatasource>
Then your grid1_RowCommand1 method becomes much cleaner:
Private Sub grid1_RowCommand1(sender As Object, e As GridViewCommandEventArgs) Handles grid1.RowCommand
If e.CommandName = "insert" Then
SqlDataSource1.Insert()
End If
End Sub
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());
}
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>";
}
}
}
I am using a data grid in an ASP.NET page to display a data table.
I am not using paging.
If I click "update" the page reloads, changing that specific row to update mode. The problem is that I have to scroll back down to the row to enter the data. I want it to automatically jump down to that row.
The same thing happens when submitting the update. It reloads, but stays at the top of the page. Instead, I want it to jump back down to the row that was just updated.
Update: adding code block. I can get the tag to output in each row of the data grid, but not sure where or how to do the script part...
The Form:
<form id="FORMNAME" runat="server">
The DataGrid:
<asp:GridView ID="dataGrid" DataKeyNames="ID" DataSourceID="RESORTS" AllowSorting="True" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:CommandField ShowEditButton="True" ButtonType="Button" HeaderText="" />
<asp:BoundField DataField="ID" HeaderText="KEY" SortExpression="ID" ReadOnly="True" />
<asp:TemplateField HeaderText="NAME" SortExpression="NAME">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("HREFID") %>'></asp:Label>
NAME:<br />
<asp:textbox id="NAME" text='<%# Bind("NAME") %>' runat="server"/>
<br />
SITE:<br />
<asp:textbox id="Textbox1" text='<%# Bind("URL") %>' runat="server"/>
<br />
LOGO:<br />
<asp:textbox id="LOGO_URL" text='<%# Bind("LOGO_URL") %>' runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("HREFID") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent" SortExpression="PARENT_NAME">
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("PARENT_NAME") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("PARENT_NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logo Image" SortExpression="IMGLOGOURL" ItemStyle-CssClass="logoCell">
<EditItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("IMGURL") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("IMGURL") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NOTES" SortExpression="NOTES" ItemStyle-CssClass="textAreaCell">
<EditItemTemplate>
<br />
PARENT:<br />
<asp:DropDownList ID="PARENT_NAME" runat="server" DataSourceID="RESORTS" DataTextField="NAME" DataValueField="ID" SelectedValue='<%# Bind("PARENT_ID") %>'></asp:DropDownList>
<br />NOTES:<br />
<asp:textbox id="NOTES" text='<%# Bind("NOTES") %>' Wrap="true" TextMode="MultiLine" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("NOTESTA") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Thanks,
Gary
Try: MaintainScrollPositionOnPostback="true" on the page directive above, it should work!
If MaintainScrollPositionOnPostback="true" doesnt work for you then add a anchor to each row and use javascript to navigate to it.
e.g:
<a name="row1" />
and use javascript like
window.location.hash="#row1"
I am using a grid view in my asp.net application. In one column i need to display description(minimum 5 characters. Maximum 255 characters).i am using a label to hold description in that grid view.
But my problem is that if the description is larger it stretches in the browser and show it in one line. I want to display description in multi line (like a paragraph)
I hope some one help me . the entire grid view code is shown below
<asp:GridView ID="gv_View_Documents" runat="server" AllowSorting="true" DataKeyNames="DocumentName,Description" SkinID="customGridview" AutoGenerateColumns="false" OnSorting="gv_View_Documents_Sorting" OnRowCancelingEdit="gv_View_Documents_RowCancelingEdit" OnRowCommand="gv_View_Documents_RowCommand"
OnRowEditing="gv_View_Documents_RowEditing" OnRowUpdating="gv_View_Documents_RowUpdating" >
<Columns>
<asp:TemplateField HeaderText="Document Name" HeaderStyle-Width="200" HeaderStyle-CssClass="GridHeaderStyle" SortExpression="DocumentName" >
<ItemTemplate>
<asp:LinkButton CommandName="ViewDocument" CssClass="GridHeaderStyle" ID="hlnk_View_Document" runat="server" CommandArgument='<%# Bind("DocumentName") %>' Text='<%# Bind("DocumentName") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="200" HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lbl_gv_DocumentDescription" runat="server" Text='<%# Bind("Description") %>' ></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_gv_EditDescription" MaxLength="250" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="50" HeaderStyle-CssClass="GridHeaderStyle" ShowHeader="False" >
<EditItemTemplate>
<asp:LinkButton ID="Bttn_Update_Description" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="Bttn_Cancel_Settings" ForeColor=" #555555" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton></EditItemTemplate><ItemTemplate>
<asp:LinkButton ID="Bttn_Edit_Description" ForeColor=" #555555" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit" ></asp:LinkButton></ItemTemplate><ControlStyle CssClass="edit" />
</asp:TemplateField>
</Columns>
</asp:GridView>
try this... working properly...for wrapping text in Gridview
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;width:100px");
}
}
}
Sometimes ItemStyle Wrap="true" doesn't work. To be certain your text wraps, surround the Label in a and set a width on the surrounding div.
EDIT
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div style="width:100px;">
<asp:Label ID="Label2" runat="server" Text="<%# Eval("Name") %>"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="<%# Eval("Age") %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can set the ItemStyle of the TemplateField to true like this:
<ItemStyle Wrap="true" Width="100px" />
Complete gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle Wrap="true" Width="100px" />
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Screenshot:
Try like this by applying the css class
.paraGraphtext
{
white-space: pre-wrap;
}
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="<%# Eval("ID") %>" CssClass="paraGraphtext"></asp:Label>
</ItemTemplate>