Display text in multiline inside a grid view in asp.net - asp.net

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>

Related

passing label templatefield value to vb.net function

I have this gridview with a templateField like this
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Font-Size="10px" CssClass="ControlStyleUpperCase" Text='<%# Bind("age") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
and I have this html hyperlink that suppose to pass name and Age to both onServerClick function
<asp:TemplateField HeaderText="">
<ItemTemplate>
<a id="btn_Approve" onserverclick="Approve_Click" style="font-size:12px;color:Green;text-decoration: none;" runat="server" href='#'>Approve</a>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<a id="btn_Update" onserverclick="Update_Click" style="font-size:12px;text-decoration: none;" runat="server">Update</a>
</ItemTemplate>
</asp:TemplateField>
can anyone guide me on how can I pass those labelId to code behind function
thanks.

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.

multiline text in gridview not working

Not giving multiline text in gridview cell
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" ShowFooter="true" AllowPaging="True" AllowSorting="True"
PageSize="5" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="CategoryID" InsertVisible="False" SortExpression="CategoryID">
<ItemTemplate>
<asp:Label ID="lblCategoryID" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
<EditItemTemplate>
<asp:TextBox ID="txtCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDesc" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDesc" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
None of the textboxes have TextMode set to Multiline.
Set the TextMode property to Multiline on the textbox that you want to be displayed as textarea.
e.g:
<EditItemTemplate>
<asp:TextBox ID="txtCategoryName" Columns="50" Rows="5" runat="server" TextMode="MultiLine" Text='<%# Bind("CategoryName") %>'></asp:TextBox>
</EditItemTemplate>
EDIT
And to make the label wrap, try adding an ItemStyle to it, like this:
<ItemStyle Wrap="true" Width="200" />

'GridView1' fired event PageIndexChanging which wasn't handled

I am using a gridview and I want to use paging. I have already set allow paging to true and page size to 5. I can see the numbers at the base of my gridview, but when i click on a number to move to respective page, it throws an error saying:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
Code:
<asp:GridView ID="GridView1" runat="server" CellPadding="5"
AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="contact_id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="5">
<Columns>
<asp:TemplateField HeaderText="contact_id">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("contact_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("address") %>'></asp:Label><br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
<asp:Label ID="Label7" runat="server" Text='<%# Eval("state") %>'></asp:Label><br />
<asp:Label ID="Label8" runat="server" Text='<%# Eval("pincode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="email">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mobile">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="context">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("context") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>PENDING</asp:ListItem>
<asp:ListItem>OK</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemStyle CssClass="button" />
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
You will have to handle the PageIndexChanging event for the grid
Something like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Bind grid
}
It will not work if you enable paging. You also need to write the event handler for PageIndexChangeEvent. Check this link: http://forums.asp.net/post/1177923.aspx
You would need to code "PageIndexChanging" event to make it work. Add an event handler for the PageIndexChanging where you set the GridView.CurrentPage = e.NewPage...
Add one more event in HTML mark up for pagging.
OnPageIndexChanging="GridView1_PageIndexChanging"
Now handle same event from code behind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Your code
}

After updating row in a DataGrid control, how can I make the page jump back down to that row?

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"

Resources