Is there some way I can use a data format string in a bound field which appends % to my value ?
Example :
For rupee we can do this :
<asp:BoundField DataField="PassPercent" ItemStyle-Width="7%" HeaderText="Pass Percent" DataFormatString="{0:c}"
I tried using a template field too but that didn't work :
<asp:TemplateField HeaderText="Pass Percent" ItemStyle-Width="5%" >
<ItemTemplate>
<asp:Label runat="server" DataValueField="PassPercent" DataTextField="PassPercent" />
<asp:Label Text="%" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Try this:-
DataFormatString="{0:p}"
But, please note percentages are stored as decimals in this case so you need to adjust your values accordingly. Check the formatting's here on MSDN.
Or you can simply hard-code it:-
DataFormatString="{0}%"
Related
I use PNM Sequence. And I need to make one grid column as the mandatory field.
I know how to make it with any separate control. E.g. I can type:
<sq8:GridBoundColumn DataField="txtField" HeaderText="txtField"
SortExpression="txtField" UniqueName="txtField" FilterControlAltText="">
<ColumnValidationSettings>
<RequiredFieldValidator ForeColor=""></RequiredFieldValidator>
</ColumnValidationSettings>
</sq8:GridBoundColumn>
And I can use this Validator for the TextBox:
<sq8:Label runat="server" Text="Field:" ID="Label1" Width="100%"></sq8:Label>
<nobr>
<sq8:TextBox runat="server" ID="txtField" Width="100%"></sq8:TextBox>
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtField"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
</nobr>
<sq:BindableControl runat="server" TargetControlID="txtField"
DataField="txtField"></sq:BindableControl>
And it works. User can't send the form because he gets an error - the field is empty.
But I need to do the same with grid.
When I open "Edit columns" in Grid Wizard I can't see any property as "mandatory" or something like this.
And the code with RequiredFieldValidator doesn't work with a grid column. If I try to use it:
<Columns>
<sq8:GridBoundColumn DataField="txtFieldGrid" HeaderText="txtFieldGrid"
SortExpression="txtFieldGrid" UniqueName="txtFieldGrid"
FilterControlAltText="">
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtFieldGrid"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
<sq:BindableControl runat="server" TargetControlID="txtFieldGrid"
DataField="txtFieldGrid"></sq:BindableControl>
</sq8:GridBoundColumn>
</Columns>
In this case, I have an error:
Is there some method for grid column validation? Or it's impossible with a grid?
Maybe I can use some javascript?
So I have gridview pulling fields from a table and my hyperlinkfield is used to go to a specific page for that row to get more detailed data. Everything seems to work great except when the field used in the hyperlinkfield has an ampersand. I assume it is reading the ampersand as something else and so it doesn't bring up the proper info because the ampersand is in the name in the database.
Hyperlinkfieldcode:
<asp:HyperLinkField HeaderText="Name" Text="{0}" DataNavigateUrlFields="Name" DataNavigateUrlFormatString="item.aspx?name={0}" DataTextField="Name" />
Example:
A clicking on the name "test item" would take you to mysite.com/item.aspx?name=test%20item and this works.
However, clicking on "test & test item" it takes you to mysite.com/item.aspx?name=test%20item%20&%20item which does not work. It just pulls up a page with blank info.
What can I do to fix this?
Update:
I converted the hyperlinkfield to a hyperlink inside a template field, but the url is now coming out weird.the url now comes out like mysite.com/item.aspx?name=System.Int32%5b%5d
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%#Eval("Name") %>' DataNavigateUrlFields="Name" NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode({0}.ToString())%>' DataTextField="Name" />
</ItemTemplate>
</asp:TemplateField>
My original answer was incomplete (due to not being able to see the entire code). This answer will contain the missing elements.
The main object is to UrlEncode the data field Name, so that it can be used as (part of) a url link .
1 - First we must ensure that the field "Name", is listed as DataKeyNames for the GridView as follows:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name" ...
2 - Secondly (if using a navigateURL) create a TemplateField i.e. (asp:TemplateField ) and use Eval() method to access the DataField in conjunction with UrlEncode() .
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="NameLink" runat="server" Text='<%# Eval("Name") %>' NavigateUrl='<%# "name.aspx?name=" + HttpUtility.UrlEncode(Eval("Name").ToString())%>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Option 2 Alternatively you can use a HyperLinkField and DataNavigateURL but you should still designate Name as a DataKeyNames .
Hope this works with no problems. Let me know if any clarification is needed.
Happy coding and Cheers,
I have a SQL stored proc where I am creating a column ("Certified") dynamically based on two other columns. The value from this column is a '0' or '1'. The SQL stored proc query is:
, CASE WHEN
(StartMiles < EndMiles)
AND (StartTime < EndTime)
AND (bcd.Status != 'C')
THEN '1' ELSE '0' END
AS Certified
On the front end in my aspx page, I have a telerik radgrid that will display a checkbox (enabled if value is 1, disabled if value is 0). The aspx code is:
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# !bool.Parse(Eval("Certified").ToString()) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I am getting an error on the aspx page String was not recognized as a valid Boolean
To resolve the error, how can I set a datatype in the stored proc?
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified" Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# !Convert.ToBoolean(Convert.ToInt32(Eval("Certified").ToString())) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I would recommend using a code-behind method to do this instead of putting logic into the markup via embedded code blocks, like this:
Markup:
<telerik:GridTemplateColumn DataField="Certified" HeaderText="Certified"
Visible="true">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true"
OnCheckedChanged="CheckBox2_CheckedChanged"
Enabled='<%# IsCertified(Eval("Certified").ToString()) %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
Code-behind:
protected bool IsCertified(string certifiedValue)
{
return !bool.Parse(certifiedValue);
}
Note: To be safer, I would recommend using the Boolean.TryParse() method instead of just the Parse(), as that will eliminate the chance of a string that cannot be parsed into a bool from throwing an exception. Read Boolean.TryParse Method documentation for more information.
This provides two advantages, in my opinion, over the OP code:
Simplified markup, because you do not have conditional logic in the markup, but now just a call to the method.
You can leverage the power of Visual Studio's IntelliSense, compiler to catch syntax errors at compile-time instead of run-time and the debugger itself.
I have a few fields that look like this in a website that uses ASP and VB (the data is displayed in a gridview):
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server"
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
And what I'm trying to do is display nothing in the field if the data is empty, and display the string you see in the Text property if there is data. Currently it displays the hyphen used in the Text string when there is no data. I tried several methods of formatting the Eval that I found online but was unable to find a working solution. I also tried using the
EmptyDataText
property however this seemed to have no effect.
I am new to ASP so that could be user error. Any help is greatly appreciated.
You can also use eval for visible and check for data
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server" visible='<%# If(String.IsNullOrEmpty(Eval("CDE_CMPT")), false, true)'
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
I haven't used VB.net is a while, so the syntax might be off.
I'm trying to bind a GridView HyperLinkField such that the bound column is used as a parameter value in the URL. Pretty standard stuff - nothing fancy, but the binding fails when the bound column contains a colon, i.e. :. I'm my particular case, this value is a string representing a duration of time, e.g. "14:35", or "1:07:19".
Here's my GridView, with the time value bound to the HyperLinkField url.
<asp:GridView ID="ResultsGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="ResultsDataSource" EnableModelValidation="True"
AllowPaging="True">
<Columns>
<asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
<asp:HyperLinkField DataNavigateUrlFields="RunTime"
DataTextField="RunTime" HeaderText="Hyperlink"
DataNavigateUrlFormatString="LinkedPage.aspx?param={0}" />
<asp:BoundField DataField="RunTime" HeaderText="Time"
SortExpression="RunTime" />
<asp:BoundField DataField="FullName" HeaderText="Name"
SortExpression="FullName" ReadOnly="True" />
</Columns>
</asp:GridView>
It produces HTML like this. Note that the <a> tags have no href attribute.
<tr>
<td>2010</td><td><a>34:58</a></td><td>34:58</td><td>Joe Schmoe</td>
</tr><tr>
<td>2010</td><td><a>35:30</a></td><td>35:30</td><td>Rod Krueger</td>
</tr><tr>
<td>2010</td><td><a>35:38</a></td><td>35:38</td><td>Mike Johnson</td>
</tr>
But if I switch the bound field from RunTime to Year, i.e. to a column that doesn't contain a colon in the values, it works as expected. Take the GridView above, and change the DataNavigateUrlFields attribute of the HyperLinkField, like so:
<asp:HyperLinkField DataNavigateUrlFields="Year"
DataTextField="RunTime" HeaderText="Hyperlink"
DataNavigateUrlFormatString="LinkedPage.aspx?param={0}" />
And now the HTML output is correct, like this:
<tr>
<td>2010</td><td>34:58</td><td>34:58</td><td>Joe Schmoe</td>
</tr><tr>
<td>2010</td><td>35:30</td><td>35:30</td><td>Rod Krueger</td>
</tr><tr>
<td>2010</td><td>35:38</td><td>35:38</td><td>Mike Johnson</td>
</tr><tr>
So the nut of my question is this: how do I bind a data column with values that contain a colon to the URL of a HyperLinkField? Or, failing that, create the same bound hyperlink by another method?
Changing the format of the data to not include a colon would be a last resort, because LinkedPage.aspx expects the parameter value in that format, and it's already written, tested and in use.
<asp:TemplateField HeaderText="Hyperlink">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("RunTime", #"LinkedPage.aspx?param={0:hh\:mm}") %>'
Text='<%# Eval("RunTime", #"{0:hh\:mm}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Wow, very strange, worse comes to worse, as a very last step, you can always tap into RowDataBound, and set the cell text to hyperlink HTML yourself, but in the meantime, try tapping into RowDataBound and examining the results there. Maybe you can encode the value at binding time, so that if there is an issue with :, encoding probably will resolve it?
You may also want to submit that as a bug to connect.microsoft.com...
HTH.