itemtemplate width not working. i make width="50" bit it is never 50but is always more then 50. is it possible to make with on td that gridview create?
<asp:GridView ID="gwTemporaryCities" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Ime">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"
Text='<%# StripHTML(Eval("Name")) != "" ? StripHTML(Eval("Name")) : "/" %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelete" runat="server" Text="Odstrani" CommandName="DeleteTemporaryCity" Width="50"
CommandArgument='<%# Eval("idTemporaryCities") %>'
OnCommand="lnkBtnDelete_Command" CausesValidation="False"></asp:LinkButton>
<asp:ConfirmButtonExtender ID="cbeDelete" ConfirmText="Ali ste prepričani, da želite odstraniti mesto?"
runat="server" TargetControlID="lnkBtnDelete">
</asp:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView
You're not setting the width of the ItemTemplate, but of one of the controls contained within the item template
On top of that, the control you're setting the width on is a LinkButton, which is effectively the same as saying:
Odstrani
But depending on your font, the text "Odstrani" is probably wider than 50 pixels, so this width setting is likely to be ignored as there's no way to break that one word under 50 pixels.
Related
I have an example from this gridview:
<asp:GridView ID="GridView3" runat="server">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("nome")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And I would like to add a button for ordering the data in each column.
Somebody can help me find the best way to do this?
Sorting is built into the GridView control. There is a control Property called AllowSorting, set this to True and you can sort columns by clicking on the header row titles.
There is no need to add extra buttons, even if you want to do some type of special sorting, you'd still Handle the Sorting event and implement your own methods
I have a gridview with a hyperlink:
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False"
CssClass="table table-hover table-striped" GridLines="None" >
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="EmployeName">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("EmployeName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID" SortExpression="EmployeID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("EmployeID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
However, it should only appear as a hyperlink if the employeeID is that of the logged in employee.
I can do all that, but what I do not know is how to make the hyperlink look like a Label. It's easy to have it not link anywhere, but I do not know how to make it look like a label.
Thanks
I believe if you set Enabled="false" it does. If it does not, then the only way to do that is put both a HyperLink and Label in the cell, and show the link when appropriate, and the label when appropriate, hiding the other one (which can be easily done in RowDataBound event).
I am databinding a gridview and one of the columns is defined as shown below. What I would like to do is to colour the text depending on whether the text says "Yes" or "No". If the text is "Yes" I'd like to set it to red else set it to green. Can this be done and if so should it be done via css or can I add some code to the line?
<asp:TemplateField HeaderText="Validated" ItemStyle-HorizontalAlign="Center" SortExpression="Product">
<ItemTemplate>
<asp:Label ID="lblValidated" runat="server" Text='<%# Bind("Validation") %>' />
</ItemTemplate>
</asp:TemplateField>
The below should do what you want.
ASP.NET
<asp:TemplateField HeaderText="Validated" ItemStyle-HorizontalAlign="Center" SortExpression="Product">
<ItemTemplate>
<asp:Label ID="lblValidated" runat="server" Text='<%# Bind("Validation") %>' CssClass='<%# SetColor(DataBinder.Eval(Container.DataItem, "Validation")) %>' />
</ItemTemplate>
C#
public string SetColor(string Text)
{
return Text.ToUpper == "YES" ? "GreenClass" : "RedClass"
}
CSS
.GreenClass{color:green;}
.RedClass{color:red;}
I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.
I want to show a thumbnail image inside a gridview instead of the text. This is what I am trying:
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Image") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="thumbnail" runat="server" ImageUrl="<%# Bind("Image") %>" />
</ItemTemplate>
</asp:TemplateField>
What is the syntax i should be using?
Try using Eval instead of Bind for the ImageUrl - this is one way binding.
If you are still having problems, using single quotes instead of double quotes around the property might help: <asp:Image ID="thumbnail" runat="server" ImageUrl='<%# Eval("Image") %>' />