Conditionally set CssClass for TemplateField - c#

I need to conditionally set the CssClass for a Template Field - not for the controls in the Template Field - but for the Template Field itself - so that it renders as:
<td class="fred">
or
<td class="jim">
I have been trying things like:
<asp:TemplateField>
<ItemTemplate><%# Eval("ProductName")%></ItemTemplate>
<ItemStyle CssClass='<%# Convert.ToBoolean(Eval("ProductType")) == true ? "fred" : "jim" %>' />
</asp:TemplateField>
and various variations - like trying to set the CssClass dynamically in the tag - but an error is reported that TemplateField and ItemStyle do not support databinding expressions.
How can I dynamically set the CssClass of a TemplateField?
enter code here

I presume that you are using a GridView control... if this is the case, you would apply the styles within the "RowDataBound" event handler.
Waiting for more information...

Related

Check Box in ASP.NET page is not working correctly

I am using Grid View and inside grid view template defined for checkbox. I have used this criteria on various pages. But on some asp pages check box is not true as i have checked them it will always return property Checked = false.
<ItemTemplate>
<asp:CheckBox ID="chkBox_ID" runat="server" Checked='<%# Convert.ToBoolean( Convert.ToInt32(DataBinder.Eval(Container.DataItem, "COLUMN_NAME"))) %>' />
</ItemTemplate>
Template is defined above.
I am using foreach loop to get rows then cell and then FindControl() method to find the checkbox.
This is code behind how i access it.
CheckBox chkRow = (row.Cells[GetColumnIndexByName(Grv_h, "Authorize")].FindControl("chkBox_ID") as CheckBox);

How to set a default template in a ListView control

I have a ListView control with an ItemTemplate, and an EditItemTemplate. By default, the ItemTemplate is rendered; how can I change that? More clearly, on Page_Load, I would like my EditItemTemplate to be the selected template.
<asp:ListView ID="lvExample" runat="server" SelectMethod="GetStuff" UpdateMethod="UpdateStuff">
<EditItemTemplate>
...
</EditItemTemplate>
<EditItemTemplate>
...
</EditItemTemplate>
</asp:ListView>
You can use this approach.
(1) Write your Edit logic/Edit Template in your inside you item template.
(2) Then you can put your ItemTemplate content into another template.
since the default load template is ItemTemplate you get your edit template load first since the content of your itemteplate is your edit template

Set C# Inline Expressions in ASP.NET Controls On Server-Side

Is it possible to programatically insert C# Inline Expressions as the values for ASP.NET Controls in your server-side code?
I'm currently using a DataList to display a lot of data in a table. I want to be able to dynamically change the columns of that table, so I need to be able to edit what controls are in its ItemTemplate.
However, in addition to editing the controls in the ItemTemplate, I need to be able to alter the value that is binded to each control in the template, because I want to dynamically change what is being displayed.
So what I currently have is a static table that doesn't change:
<asp:DataList ID="dataList" runat="server" OnSelectedIndexChanged="dataList_OnSelectedIndexChanged" DataSourceID="peopleData">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="NameLink" OnClientClick="onPageUpdate()" CommandName="Select" Text='<%# Eval(this.Name) %>' ForeColor='<%# Eval("this.NameColor") %>' runat=Server" />
</td>
<td>
<asp:Label Text='<%# Eval("this.Values[\"Age\"]") %>' ForeColor='<%# Eval("this.ValueColors[\"Age\"]") %>' runat="Server">
</td>
// OTHER COLUMNS WITH DIFFERENT DATA
</tr>
</table>
</ItemTemplate>
</asp:DataList>
// OBJECT DATA SOURCE CODE
I know how to dynamically add Controls to the ASPX web page. However, I don't know how to add the inline expressions.
I've tried doing the following but it doesn't work because of the type mismatches:
Label label = new Label();
label.Text = "<%# Eval(\"this.Values[\\\"Age\\\"]\") %>";
label.ForeColor = "<%# Eval(\"this.ValueColors[\\\"Age\\\"]\") %>";
Is there a way of achieving this or doing something similar?
My only other option that I can think of right now is to scrap using the DataList and ItemTemplate and just generate each row myself.. That's a lot more coding versus just using the ItemTemplate.
Not sure if this would help you, but look at http://msdn.microsoft.com/en-us/library/system.web.ui.databinder(v=vs.100).aspx
It is showing using of the hand-written exposure of properties. If your property were always call "magic" and you return the appropriate value for magic within your code would that get you what you need?

Dynamic textbox ID in .aspx.cs

I have a question on C#. I use the GridView ItemTemplate to add a textbox to a whole column. I add the ID to the drop down list in ItemTemplate. Therefore, the generated ID of drop down list is 'GridViewID_dropdownListID_number' in each row when I launch the project.
However, I cannot set the drop down list to .Visible = true and .Visible = false in .aspx.cs file. I try to type the 'dropdownListID' and 'GridViewID_dropdownListID_number' to 'Page_Load' function. However, it displays the error message which is under light the statement.
'The name 'GridViewID_dropdownListID_0' does not exist in the current content'
Can I set the drop down list visible to true and false in .aspx.cs?
P.S I can retrieve the row number by GridViewRow
you can use FindControl
DropdownLIst tvSeries = (DropdownLIst)tableOfTVSeries.Rows[0].Cells[2].FindControl("tvSeriesTableCategoryDropdownLIst");
Here is an example how to do this in the item template of a repeater -- this is typically how this problem is solved:
<asp:DataList Runat="server" ...>
<ItemTemplate>
<asp:Label runat="Server" Text='<%# Container.DataItem("data") %>'
Visible='<%# Container.DataItem("makevisible") %>'/>
</ItemTemplate>
</asp:DataList>

DataBind DataSource Result to ASP Labels

I have an AccessDataSource TestSummaryADS. I can easily view the results in a GridView or DropDownList by setting its DataSourceID property.
How do I bind a value from the results TestSummaryADS to the text of a label?
I'm just trying to populate labels on my page with results from the DB entry.
C# or VB.NET answers okay.
If you only have a single record, use a FormView
<asp:FormView ID="FormViewTestSummary" runat="server"
DataSourceID="TestSummaryADS"
DefaultMode="Edit">
<EditItemTemplate>
<fieldset>
<asp:TextBox ID="txtMyText" Text='<%# Bind("ProjectNumber") %>' runat="server" />
Use Repeater control that contains Label controls to display the values you want. Bind your DataSource control to the repeater.
Also you can use TemplateColumn with GridView and add your labels into this template column.
But IMO, Repeater control is simpler to customize your view by templates.

Categories

Resources