Using Conditional Statement in ASP.NET Control Attribute - asp.net

I have a DropDownList on my page and I would like the DataTextField value to be based on a condition (language). I'm using Model Binding and this DropDown is nested in a bound FormView.
This is what I'm trying to do:
<asp:DropDownList ID="DropDownList1" ItemType="BLL.HelperClasses.ItemForList"
DataValueField="id" DataTextField="<%#: (this.IsEnglish) ? "en" : "fr" %>" SelectMethod="DropDownList1_GetData"
SelectedValue="<%#: Item.Claim.CurrencyID %>" runat="server"></asp:DropDownList>
.NET is complaining that my DataTextField "server tag is not well formed". IsEnglish is a boolean property in my base page.
Anyone know how to do this without using the code-behind?

I think you have to have "Eval" inserted so for it to actually compare them don't you?
<asp:label id="Label1" runat="server" text="<%# Eval("StReply") == true ? "yes" : "no" %>" xmlns:asp="#unknown">
</asp:label>
Code was used from http://www.codeproject.com/Questions/653888/using-If-statement-in-Bind-Eval

Problem was with the surrounding double-quotes. Changed them to single-quotes and everything works perfectly.
Final solution:
<asp:DropDownList ID="DropDownList1" ItemType="BLL.HelperClasses.ItemForList"
DataValueField="id" DataTextField='<%#: (this.IsEnglish) ? "en" : "fr" %>' SelectMethod="DropDownList1_GetData"
SelectedValue="<%#: Item.Claim.CurrencyID %>" runat="server"></asp:DropDownList>

Related

Use of Resources in Eval syntax

I'm wondering how I can use the Resources within the below Label - I'm not entirely sure.
The original line with a replacement "Yes" or "No":
<asp:Label runat="server" ID="someId" Text='<%# (Boolean.Parse(Eval("BooleanValue").ToString())) ? "Yes" : "No" %>'></asp:Label>
What I believed I could do with the resources:
<asp:Label runat="server" ID="someId" Text='<%# (Boolean.Parse(Eval("BooleanValue").ToString())) ? '<%$ Resources:language, Yes%>' : '<%$ Resources:language, No%>' %>'></asp:Label>
Any help would be appreciated, Thanks.
Okay, I got around this by declaring the variables in the code behind
protected string yes;
protected string no;
Then updating them once my page had loaded based on the culture the user set.
yes = language.ResourceManager.GetString("Yes",CultureInfo.CurrentCulture);
no = language.ResourceManager.GetString("No", CultureInfo.CurrentCulture);
Then using those in the statement
<asp:Label runat="server" ID="someId" Text='<%# (bool)Eval("BooleanValue") == false ? no.ToString() : yes.ToString() %>'></asp:Label>

If statement for drop down list

So I have two drop down lists in my code where the data is retrieved from a SQL table.
I can't figure out an IF statement to show an error message if both of these two drop down lists have not been selected. I have added a list item in to tell the user to select a date/time.
I tried an if statement but this would not work.
If Statement:
If DropDownList2.SelectedValue And DropDownList3.SelectedValue = Nothing Then
warninginfo.Visible = True
Else
Response.Redirect("ConfirmationBooking.aspx")
End If
Code:
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2" DataTextField="Time" DataValueField="Time" AppendDataBoundItems="True" width="162px" Height="31px">
<asp:ListItem>-- Please Select a Time --</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="auto-style2" DataSourceID="SqlDataSource1" DataTextField="Date" DataValueField="Date" AppendDataBoundItems="True" Width="162px" Height="30px">
<asp:ListItem>-- Please Select a Date --</asp:ListItem>
</asp:DropDownList>
Your if statement is wrong. It should be (changed the AND to Or)
If DropDownList2.SelectedValue Is Nothing Or DropDownList3.SelectedValue Is Nothing Then
Another option is to make those two fields required using a RequiredFieldValidator.
<asp:RequiredFieldValidator id="reqFavoriteColor" Text="(Required)"
InitialValue="none" ControlToValidate="DropDownList2" Runat="server"
/>

getting error expression expected in asp.net, when trying to check radiobutton using db value

There are 4 readiobutton inside the repeater and i'm trying to show the checked radiobutton from database value.
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IIF(Eval("ANSWER")==1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
Second approach
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# Eval("ANSWER")==1 ? true : false %>'
Text='<%# Eval("OPTION1")%>' runat="server" />
and so on for the rest radiobutton. But, it showing the error Expression Expected error. Need help. !!
It looks like you've got your C# and VB.Net mixed. Your first example looks like VB, the second like C#. However, you've got a few problems in your VB implementation:
The equality operator in VB is =, not ==
You should use the IF operator, not the IIF function, which is obsolete
The correct code should be as follows:
<asp:RadioButton ID="rb_option1" GroupName="answer" CssClass="frm_label"
Checked='<%# IF(Eval("ANSWER")=1,true,false) %>'
Text='<%# Eval("OPTION1")%>' runat="server" />

JavaScript dateFormat error?

I am trying to get a field named BASVURUTARIHI(Date) from DataSet. But if I only bind it with Eval, it shows dd/mm/yyyy HH/MM/SS. I don't want the hours and minutes. So, I am trying to change the dateformat but it gives me the error:
CS0103: The name 'dateFormat' does not exist in the current context
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# dateFormat(Eval("BASVURUTARIHI"),"dd/mm/yyyy") %>'></dx:ASPxLabel>
I don't know a lot about JavaScript. Can you help me with this please?
Try this:
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "BASVURUTARIHI", "{0:dd MMM yyyy}")%>'></dx:ASPxLabel>
This is ASP.NET code, try to have this instead:
Text='<%# ((DateTime)Eval("BASVURUTARIHI")).ToString("dd/mm/yyyy") %>'>
Edit: to avoid error when the value is null, change to:
Text='<%# (Eval("BASVURUTARIHI") == null) ? "" : ((DateTime)Eval("BASVURUTARIHI")).ToString("dd/mm/yyyy") %>'>
You can use this code:
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# date("j/ n/ Y",Eval("BASVURUTARIHI")) %>'>
</dx:ASPxLabel>

How to update an ASP.NET label with SqlDataSource return value

On an ASP.NET page, I have a SqlDataSource configured with the following SELECT command:
SELECT AVG(Rating) FROM [Ratings] WHERE ([AlbumID] = #AlbumID)
How would I place that average value into a label?
You need to use FormView control to put the Label in.
Something like this:
<asp:formview id="formview1" runat="server" datasourceid="your-datasource-id">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# Eval("column-name") %>' />
</itemtemplate>
</asp:formview>
Replace your-datasource-id and column-name in the above code.
PS: you might need to alter the query to have a column-name for that one value:
SELECT AVG(Rating) AS "average" FROM [Ratings] WHERE ([AlbumID] = #AlbumID)

Resources