How to change a BoundField's control in asp.net - asp.net

Can you change a BoundField's Control?
<asp:BoundField DataField="week1" HeaderText="week1" SortExpression="beginDate" />

You can't change the control that's created when you use a BoundField - that's chosen automatically based on the data type of the field you're binding to (a CheckBox gets created for boolean fields, a Label for text-type fields).
You need to use a TemplateField if you want to bind your data to a different type of control:
<asp:templatefield headertext="week1">
<itemtemplate>
<asp:label id="weekOneLbl" Text= '<%# Eval("week1") %>' runat="server"/>
</itemtemplate>
</asp:templatefield>
By default, your BoundField will use a Label control (like above). But with the TemplateField, you could change it to, say, a read-only TextBox:
<asp:templatefield headertext="week1">
<itemtemplate>
<asp:TextBox ID="weekOneLbl" Text= '<%# Eval("week1") %>' runat="server" ReadOnly="True" />
</itemtemplate>
</asp:templatefield>

Related

How to add a tooltip to Boundfields in a Detailsview, but only if color of column has changed

I have the following code ...
<asp:DetailsView ID="dvApprenticeship" runat="server" DataSourceID="dsApprenticeship" AutoGenerateRows="false" BackColor="#E0E8F0" GridLines="None" CellPadding="2"
DataKeyNames="ProgramID, ProgramName, OrganisationName, StudyYearID, Workgroup, Pathway, FinishDate" OnDataBound="Apprenticeship_DataBound">
<Fields>
<asp:BoundField DataField="ProgramName" HeaderText="Program:" />
<asp:BoundField DataField="StudyYearName" HeaderText="Study Year:" />
<asp:HyperLinkField DataTextField="OrganisationName" HeaderText="Apprenticeship: " NavigateUrl="Apprenticeships.aspx" />
<asp:BoundField DataField="Workgroup" HeaderText="Workgroup:" />
<asp:BoundField DataField="Pathway" HeaderText="Pathway:" />
<asp:TemplateField HeaderText="Nominal Completion: ">
<ItemTemplate>
<asp:Label ID="labEndDate" runat="server" Text='<%# Eval("FinishDate","{0:d/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FooterTemplate>
<asp:LinkButton ID="lbAddProgramUnits" runat="server" OnClick="AddProgramUnits_Click" ForeColor="White" Font-Bold="true"
OnClientClick="return confirm('Import the Program Units listed - this may overwrite unit dates. Are you sure?');">Import from Program</asp:LinkButton>
Help
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" BackColor="LightSlateGray" />
</asp:DetailsView>
I want to be able to show a tooltip whenever one of the above Boundfields has changed color.
In my C# codebehind, I have code that changes the color of these boundfields depending on certain conditions of the data. This is working fine.
But what I want is to be able to give the users a tooltip when ever they hover their mouse over these Boundfields and ONLY if that field is coloured differently, in my case
color.Yellow
.
And to answer my own question, with something else I found, which was overlooked: the convert BoundField to TemplateField option.
From this ...
<asp:BoundField HeaderText="Claim Type ID" ..etc../>
To This ...
<asp:TemplateField HeaderText="Claim Type ID">
<EditItemTemplate>
<asp:Label ID="lblClaimTypeID" runat="server" Text='<%# Eval("ClaimTypeID") %>' ToolTip="Enter a numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtClaimTypeID" runat="server" Text='<%# Bind("ClaimTypeID") %>' ToolTip="Enter a numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="itClaimTypeID" runat="server" Text='<%# Bind("ClaimTypeID") %>' ToolTip="A numerical value that conforms to the UserChoice Policy document (ie: 65 for GAT)."></asp:Label>
</ItemTemplate>
</asp:TemplateField>
This is sweet, because in the Design mode of ASPX, you can select your DetailsView, select the Edit Fields option and select the fields that are BoundFields and convert them straight into TemplateFields. The beauty with that, is that it converts the BoundFields to neat Labels or TextBoxes allowing you to directly code in a ToolTip property! And no code behind! Microsoft got something right there for once.
If you are setting the color to yellow in the DetailsView DataBound event based on some criteria, you can set the tooltip in that same block:
DetailsViewRow.Cells[indexofyellowfield].ToolTip = "some help from code-behind";

Setting value to hidden field in gridview

I have gridview with textbox and one Update button outside gridview.
When i click on button whatever data changed in textbox those values will update in database.
Here is My Gridview templatefield which is having Textbox
<asp:TemplateField HeaderText="SeqNo" HeaderStyle-CssClass="gridViolation-status"
ItemStyle-CssClass="gridViolation-status" ItemStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="White" SortExpression="STATUS">
<ItemTemplate>
<asp:TextBox ID="txtSeqNo" runat="server" Width="40%" Text='<%# Bind("VIOLATIONCODESEQNO") %>'
onchange="SetValue()"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
and button outside gridview
<asp:Button ID="btnAdd" runat="server" Text="Update"
CssClass="submitbtn" OnClick="btnAdd_Click" />
I just need to update the value that is in textbox to database without using Foreach

How to set ItemTemplate text value to combobox DataTextField instead of DataValueField

I have a telerik radgrid with a GridTemplateColumn as seen below in a C# ASP.NET 4.0 webform.
As you can see, in the EditItemTemplate I am using a RadComboBox with an id number for DataValueField and a human readable text for DataTextField.
My question is, how can I change the Text in my ItemTemplate to show the human readable value instead of the Id? The value Alias1 comes from the grid datasource and it is the Id.
Thank you for any help you can provide!
<telerik:GridTemplateColumn UniqueName="Alias1" Display="true" DataField="Alias1" HeaderText="Alias1" SortExpression="Alias1">
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Bind("Alias1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox16" runat="server" Skin="Outlook" Height="150" DataSourceID="SqlDataSourceAliasOptions" DataTextField="aliasText" DataValueField="aliasid" SelectedValue='<%#Bind("Alias1") %>'>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
You need to change ItemTemplate binding like this:
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Eval("aliasText") %>'></asp:Label>
</ItemTemplate>
Of course your binded entity must have "aliasText" property. If you are binding something like DataTable make sure that contains "aliasText" column.

GridView not returning databound rows when OnClick event fires

I have a GridView that is made up of two database fields populated via a stored procedure and then three fields for user input (two checkbox controls and one textbox) when I click on the save button I can get the information from the three controls but nothing from the two that were populated via the database. How can I get the first two fields?
<asp:GridView ID="gvA1" runat="server" AutoGenerateColumns="false" DataKeyNames="CodeNo" AutoGenerateSelectButton="false" EnablePersistedSelection="True" Visible="false">
<Columns>
<asp:TemplateField Visible="false" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="true" ItemStyle-Width="400px" HeaderText="Violation">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbPool" runat="server" Text="Pool:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbSpa" runat="server" Text="Spa:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Comments" Visible="true">
<ItemTemplate>
<asp:TextBox ID="A1Accordion_tb" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void SaveAndCollapseA1(object sender, EventArgs e)
{
//good stuff
CheckBox myCheckBox_1 = gvA1.Rows[0].FindControl("A1Accordion_cbPool") as CheckBox;
CheckBox myCheckBox_2 = gvA1.Rows[0].FindControl("A1Accordion_cbSpa") as CheckBox;
TextBox myTextBox = gvA1.Rows[0].FindControl("A1Accordion_tb") as TextBox;
//not so good stuff
String myString1 = gvA1.Rows[0].Cells[0].ToString();
String myString2 = gvA1.Rows[0].Cells[1].ToString();
}
I figured it out but I haven't been hear long enough to post the solution via the links but if you change the columns as so:
<asp:label ID="lblA1CodeNo" runat="server" text='<%# Eval("CodeNo") %>'></asp:label>
then the values are available...
Try:
<%# Bind("CodeViolationCited") %>
Instead of
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
Eval is one way, bind is two-way.
See Microsoft's documentation for data-binding expressions

Gridview : Hyperlink and description in the same column cell

Apologies for the newbie question. My client wishes me to make a small change to the gridview on his http://www.flogitdonegal.com/SearchPage.aspx page.
Note the way the Title column is a hyperlink to view more information. This comes from a 'BriefDescription' field in the database.
How can I add 250 chars from the 'FullDescription' underneath the Title in the same cell, but I dont want it be a hyperlink.
Essentially it will be 2 fields coming into the same column.
Thanks in advance for all help.
John
If this is using a GridView you are most likely using a TemplateField as it is to display the HyperLink.
Within the ItemTemplate of the TemplateField you can specify an additional Label underneath using something as follows:
<asp:Label runat="server" id="FullDescLabel" Text='<%# DataBinder.Eval(Container.DataItem, "FullDescription") %>' />
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwCompounds" runat="server" DataSourceID="objItemsFromYourDB">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Title">
<asp:HyperLink runat="server" ID="Hperlink1" NavigateUrl='<%# Eval("BriefDescriptionUrl") %>' Text='<%# Eval("BriefDescription") %>' />
<br />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("FullDescription") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>

Resources