Can you truncate results of a databound templatefield in a gridview? - c#

I've got a field that retrieves a folder path. However it makes the column width too wide. I was wondering if there was a way to truncate the returned value so I can show just the file name without it's path.
<asp:TemplateField ControlStyle-CssClass="columnWidth"
HeaderText="File Name " SortExpression="FileName">
<EditItemTemplate>
<asp:Label ID="Label12" runat="server"
Text='<%# Bind("FileName")%>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="download"
CausesValidation="false" OnClick="download_Click"
CommandArgument="<% %>" CommandName="Download"
Text='<%#Bind("FileName")%>'></asp:LinkButton>
</ItemTemplate>
<HeaderStyle CssClass="gridHeader columnWidth" ForeColor="#A1ADBB" />
</asp:TemplateField>

.aspx file
<asp:Label ID="Label12" runat="server"
Text='<%# GetFileName( Convert.ToString( Eval("FilePath") ) ) %>'
</asp:Label>
.cs file
using System.IO;
protected string GetImagePath( string FilePath)
{
return Path.GetFileName(FilePath);
}

Related

Specific Label Value From GridView

I am trying to delete rows from ASP.NET GridView and for demo purpose, I am trying to retrieve names from the rows right now. In the ItemTemplate of the GridView, I've included Label that has id as well name to show. Finally added Button control in the GridView to delete specific rows and tried the following:
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ProductId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Button" OnClick="btnShow_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
In the code-behind, I've iterated the GridView with a loop to get the individual values, say name of the row as follows:
protected void btnShow_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in grdData.Rows)
{
string val = ((Label)row.FindControl("lblName")).Text;
lblMsg.Text = val;
}
}
But unfortunately I get the last name every time I click any row button and here is the screen-shot that I am trying:
Whenever I click any button, it shows the last name Ice-Cream every time. Anything that I missed here?
<asp:GridView ID="grdData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("ProductId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnShow" runat="server" Text="Button" OnClick="btnShow_Click" CommandArgument='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
protected void btnShow_Click(object sender, EventArgs e)
{
lblMsg.Text = (sender as Button).CommandArgument;
}

File Upload control inside GridView

I have two file upload controls inside GridViewControl for uploading files, I have a button for uploading files inside gridview, I am using its RowCommandEvent for uploading files. How can I get values of fileupload controls in RowCommandEvent?
This is my GridView
<asp:GridView ID="DgFiles" runat="server" AutoGenerateColumns="false" OnRowCommand="DgFiles_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Crime" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="category" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Category") %>' ></asp:Label><br/>
<asp:FileUpload ID="file1" runat="server" /><br />
<asp:FileUpload ID="file2" runat="server" />
<asp:Button ID="btnupload" runat="server" CommandName="upload" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my RowCommandEvent
protected void DgFiles_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "upload")
{
}
}
I have to display records in GridView like structure, with file upload control inside it, If there is alternate way I can do it, kindly suggest.
First,
You have to add a command column with command name upload
second,
You have to find the control inside the row by using parent property.
FileUpload fl1 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File1")
FileUpload fl2 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File2")
Now You can use fl1 and fl2 as your filecontrols.

ASP.NET-C#: Inserting const into TextBox in Gridview

I have some problems inserting a const "1" into a textbox which is gridview.
The gridview code:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" EnableViewState="False">
<Columns>
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" >
<ItemStyle CssClass="price"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="ProductID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary">
<ItemTemplate>
<asp:Label ID="lblSum" runat="server" Text='<%# Eval("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="picPath">
<ItemTemplate>
<asp:Label ID="lblPic" runat="server" Text='<%# Eval("picPath") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "quantity">
<ItemTemplate>
<asp:TextBox ID="lblquantity" runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
All the information is populated from the session of the previous page, beside this textbox which doesn't comes from anywhere, it's a quantity textbox which the user should enter. And I want it to have a default value of "1".
I don't actually know how to insert into a textbox which is in the gridview.
Please help me.
Thanks
This may be because code is also checking for Header and footer template..
just put a null check before settings value...
TextBox tb = (TextBox)e.Row.FindControl("lblquantity");
if(tb!=null)
tb.Text = Convert.ToString(123);
This will surely work...
<asp:TemplateField HeaderText = "quantity">
<ItemTemplate>
<asp:TextBox ID="lblquantity" runat="server" Text='<%# Eval("quantity") == DBNull.Value ? "1" : Eval("quantity").ToString()' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
if quantity value is null in the table, then Text property will be default 1. Otherwise it will be quantity column in the table.
You can put chis code in your GridView's RowDataBound event...
TextBox tb = (TextBox)e.Row.FindControl("lblquantity");
tb.Text = Convert.ToString(123);
I hope this helps...

Referencing Controls inside am ajax Tab

I have an ajax tab which inside has a grid, on this grid there is a check box where the user has the ability to check it and assign it to a technician, but for some reason im unable to reference the control, the grid has a total of 4 rows so there is data there, this is my code which is run of a button click
protected void btnAllocate_click(object sender, EventArgs e)
{
foreach (System.Web.UI.Control s in tbHappyCall.Controls)
{
foreach (GridViewRow Row in gvHappyCall.Rows)
{
CheckBox chkAllocate = (CheckBox)Row.FindControl("chkAllocate");
if (chkAllocate.Checked)
{
HyperLink lblOrderID = (HyperLink)Row.FindControl("hyperOrderID");
HappyCallManager objHappyCallManager = new HappyCallManager();
objHappyCallManager.HappyCallAllocated(Convert.ToInt64(lblOrderID.Text), objWebuser.ShortAbbr, ddlAllocateTo.SelectedValue);
//Update database with person details thats are assigned to the Welcome Call
}
}
}
}
when it goes on to the Foreach gridviewrow etc the count is 1 even though there is 4 rows displaying information ?
Can any one shed any light on this? or even better a solution?
Thank you for your time.
UPdate
<ajaxToolkit:TabPanel ID="tbHappyCall" runat="server" HeaderText="Welcome Calls" TabIndex="10">
<ContentTemplate>
<%--OnRowDataBound="gvConfirmOrder_rowDataBound"--%>
<div id="divHappyCall">
<asp:GridView ID="gvHappyCall" runat="server" AutoGenerateColumns="false" class="tablesorter"
EmptyDataText="There is no Record to display." DataKeyNames="OrderID" EnableViewState="false"
OnRowDataBound="gvHappyCall_RowDataBound">
<AlternatingRowStyle CssClass="NormalTextVNC" BackColor="#E2E9E7"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Account Manager">
<ItemTemplate>
<asp:Label ID="lblAccountManager" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AccountManager") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:HyperLink ID="hyperOrderID" runat="server" ForeColor="White" NavigateUrl='<%#Eval("OrderGuid","/Documents/HappyCall.aspx?OrderID={0}") %>'
Text='<%#Eval("OrderID") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="lblCustomerName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="lblCompanyName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MobileNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dispatch Date">
<ItemTemplate>
<asp:Label ID="lblConnectionDate" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DespatchDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField headertext="Date Called" dataformatstring="{0:dd/MM/yyyy}" datafield="EditedDate" HeaderStyle-Width="100px" />
<asp:TemplateField HeaderText="Allocated To">
<ItemTemplate>
<asp:Label ID="lblAllocatedTo" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AllocatedTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Last Action">
<ItemTemplate>
<asp:Label ID="lblLAstAction" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LastAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Check">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAssignWelcomeCall" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:DropDownList runat="server" ID="ddlAllocateTo" CssClass="floatright">
<asp:ListItem>Name of Admin to Allocate</asp:ListItem>
</asp:DropDownList>
<div class="WhiteSpace"></div>
<asp:Button runat="server" ID="btnAllocate" class="floatright" Text="Allocate" OnClick="btnAllocate_click" />
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
For clarification: you don't need to iterate the control collection of your TabPanel to find your GridView, it is available directly and don't confuse it's Controls.Count with the number of GridView.Rows.Count.
Is ViewState disabled somewhere?
Edit: I see that the GridView's ViewState is disabled. I assume that it works when you enable it.

ASP GridView Problem

Im working on the front end development for a website built in .net, This is the first time I've done this and I'm having trouble trying to alter a table.
The code which outputs my table is...
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle CssClass="col-b" />
</asp:TemplateField>
<asp:TemplateField HeaderText=" Concept Version" ItemStyle-Width="" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div class="conceptstd">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" BorderStyle="None"
DataSource='<%# Bind("DS_Version")%>'>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:DataList>
</div>
</ItemTemplate>
<HeaderStyle CssClass="col-c" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
The html equivalent of this is something similar too...
<table>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
However I need 5 columns not 2, I've tried reading up on the syntax but thought I'd ask here also, Thanks for any help.
I'm not exactly sure how your dataset is structured, but do you need that datalist in there or can you just put the controls in their own ItemTemplate in the GridView? This would give you 5 columns:
<asp:GridView ID="GV_Concepts" runat="server" AutoGenerateColumns="False" DataKeyNames="ConCatID"
BorderStyle="None" GridLines="None" ShowHeader="False" BorderWidth="0px" CssClass="DashBoard_Concepts">
<Columns>
<asp:TemplateField HeaderText="Catalog">
<ItemTemplate>
<asp:Label ID="LB_Cata" runat="server" Text='<%# Bind("ConCatalog") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HL_ConcLoc" runat="Server" CssClass="linkage" NavigateUrl='<%# Bind("FileName") %>'
Target="_blank" Text='<%# Bind("Ver") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_remove" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_removecon_Click" CssClass="link-btn">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LB_sign" runat="server" CommandArgument='<%# Bind("ConceptID") %>'
OnClick="LB_signcon_Click" CssClass="sign-off-btn" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?true:false %>'>Sign Off</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="Lit_SignedCon" Visible='<%# SignedCheck(DataBinder.Eval(Container.DataItem,"SignOff")) ?false:true %>'
runat="server"><b>Signed Off</b></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Currently no concepts
</EmptyDataTemplate>
</asp:GridView>
Of course, this ruins your binding to your datasource, but I'm not sure if I can accurately help you with fixing that :(
the number of column is automatically generated according to asp:templatefield
when you have two templatefield, the only two column will be generated, and the number
of rows depend on data.
so if you need 5 columns you have to put 5 templatefield inside the girdiview
Why do you "need" 5 columns? For layout? Perhaps a GridView isn't the right solution given what you're trying to accomplish. Could a Repeater that generates your content accomplish the same thing?

Categories

Resources