Concat Two Bind Fields in DropDownList SelectedValue - c#

I am trying to Concat to fields in the SelectedValye property of the Dropdownlist. I want to use Bind and not Eval. This is what I am using but it's not working. Is this possible to do?
<asp:DropDownList ID="RsmList" runat="server" DataSource="<%# ddRSM.DataSource %>" SelectedValue='<%# Bind("RSMLast") + '', '' + Bind("RSMFirst") %>'>
</asp:DropDownList>
Thanks!

I dont believe you can without using Eval because Bind translates actually to two separate method calls. You need to either use Eval() or do it before you databind. But - happy to be proven wrong : )

I don't think you need your DataSource inside binding tags -- I don't set this stuff up in markup, but I believe your reference should just be DataSource="ddRSM.DataSource".
You should also be able to set your SelectedValue property like so:
SelectedValue='<%# Bind("RSMLast") %>, <%# Bind("RSMFirst") %>'

Related

DataBinder.Eval Issue

I have a some controls inside an EditItemTemplate within a RadListView control:
<telerik:RadComboBox ID="cbCategoryTypeTueET" runat="server" Skin="Office2010Black"
SelectedValue='<%# DataBinder.Eval(Container.DataItem, "CategoryTypeID") %>'
TabIndex="1" Width="100%" EmptyMessage="--Select Category Type--" DataSourceID="edsCatTypeTueET"
DataTextField="CategoryName" DataValueField="CategoryTypeID" AutoPostBack="True"
OnSelectedIndexChanged="cbCategoryTypeTueET_SelectedIndexChanged" AccessKey="t" AppendDataBoundItems="True">
</telerik:RadComboBox>
<asp:EntityDataSource ID="edsCatTypeTueET" runat="server" ConnectionString=""
DefaultContainerName="ATITimeEntry" EnableFlattening="False" EntitySetName="TimeTrackingCategoryTypes"
Select="it.[CategoryTypeID], it.[CategoryName]"
Where="it.deletedFlag = false AND it.activeFlag = true" >
</asp:EntityDataSource>
The Entity datasource does have a connection string - I am using a new code generation template - so this is not an issue.
My problem is I want the combobox to bind on edit. But if activeFlag is false or deletedFlag is true (or both) the Radlistview will not go into edit mode. Is there an elegant way to do this with markup or some elegant query?
My understandig is, you want to forbid edit on some conditions.
You have to subscripe the ItemDataBound Event from the RadListView. There you can cast DataItem to your object and check the condition (Get Data being bound to ListView).
Then you can access your controls and manipulate them (like hide them)...
Conditionaly disable command button

Dynamically resolve a resource key

I would like to insert a string (key) into a '<%$ Resources:resFile, someKey %>' statement dynamically at runtime.
Right now I have this:
<asp:Label runat="server" id="lbl" Text='<%$ Resources:resFile, someKey %>'/>
which is useless to me, because the key "someKey" is static and will always converge to the same x translations.
I've tried doing things like this:
<asp:Label runat="server" id="lbl" Text='<%$ Resources:resFile, '<%#Eval("someProperty")%>' %>'/>
But this doesn't quite compile. I'm not sure if you can have nested <%%> statements, so thats why i'm asking here.
I've also tried a variety of things with GetGlobalResourceObject() but that doesn't do what i want either.
The thing is that i am binding a list to a repeater, and one of the items in the datasource will contain the key that will match a key in the resource files.
If anyone knows a way how to achieve this, be it with nested <%%> statements or be it any other way, i'd be happy to try it out.
You could do this in Code-Behind on the ItemDataBound Event.
Label lv = (Label)e.Item.FindControl("lbl");
ResourceManager resMngr = new ResourceManager(typeof(SupertextCommon.Default));
lv.Text = resMngr.GetObject(someProperty, culture);
This has worked for me in a GridView using a property of its datasource:
<%# GetGlobalResourceObject("Resource", Eval("SomeProperty").ToString()) %>
You can also use concatenation to build the key, e.g.:
<%# GetGlobalResourceObject("Resource", "SomePrefix" & Eval("SomeProperty").ToString()) %>

Why can't I programatically set the value on my hidden field on a repeater?

I've had this strange problem come up. I have a RowDataBound event handler for a repeater control.
I have the code:
HiddenField hfIpAddressRangeId = (HiddenField)e.Row.FindControl("hfIpAddressRangeId");
hfIpAddressRangeId.Value = .IpAddressRangeId.ToString();
But when I look at the html the value of that control is not set. However when I set the value using inline C# such as
<asp:HiddenField runat="server" Value='<%# Eval("IpAddressRangeId ") %>' ID="hfIpAddressRangeId" ViewStateMode="Enabled" />
The value is being set. I'm not really sure why this won't work when I bind each datarow?
It looks like IpAddressRangeId is a part of your datasource, hence the use of Eval in the second example. Have you tried this?:
HiddenField hfIpAddressRangeId = (HiddenField)e.Row.FindControl("hfIpAddressRangeId");
hfIpAddressRangeId.Value = DataBinder.Eval(e.Row.DataItem, "IpAddressRangeId").ToString();

Why can't I set the asp:Label Text property by calling a method in the aspx file?

Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}

Label text in datarepeater

I am trying to use a label in my datarepeater, when I am able to bind data and write to me html page.
<asp:Label ID="lblID" runat="server"><%# DataBinder.Eval(Container.DataItem, "ID")%></asp:Label>
which works fine.
When I try to get text value I get "".
Label lblcurrentID = ri.FindControl("lblID") as Label;
result:
lblcurrentID.text = ""
this same code works fine for the dropdownlist that I have in the datarepeater. I am wondering if this has anything to do with the label being converted to a span tag.
<span id="template6_middlecontent1mAzoaNominationApproval0_dataReaper_ctl01_lblID">2009040100000888213</span>
I've ran into this issue before, I don't recall actually finding a solution to the problem, as a work around, I used an <asp:HiddenField> to hold onto the information for me:
<asp:HiddenField runat="server" id="hiddenId" value='<%# Eval("Id") %>' />
Note the ' instead of " wrapping the Eval statement btw, .NET is fussy when you're assigning values to server controls.
you need bind lblD.Text with the value not like
<%# DataBinder.Eval(Container.DataItem, "ID")%>

Categories

Resources