Binding Image URL with RadImageButton - c#

I asked on Telerik's site, but there is apparently not enough traffic there to generate answers.
I am trying to convert an asp ImageButton to a RadImageButton because there are some useful features that would make my life a bit easier. However, the binding for the image URL won't work - at least the way I am doing it.
So this works (both are in the same ListView):
<asp:ImageButton ID="im" runat="server" ImageUrl='<%# Eval("ImgUrl") %>' />
But this:
<telerik:RadImageButton runat="server" ID="itemImageButton" '>
<Image Url='<%# Eval("ImgUrl") %>' />
</telerik:RadImageButton>
gives me an error something like Telerik.Web.UI.ButtonBase.ButtonImage does not have a DataBinding event.
If someone could point me in the right direction to get this done with the RadImageButton control (if possible), I would appreciate it.

I was recently facing exactly the same issue. Apparently child objects/properties in some controls are not DataBound-able.
You have to do the binding for the image URL inside the telerik:RadImageButton tag itself, so instead of using <Image Url="" /> section, place it in the Image-Url attribute:
<telerik:RadImageButton runat="server" ID="itemImageButton" Image-Url='<%# Eval("ImgUrl").ToString() %>'>
</telerik:RadImageButton>
You can ommit .ToString() part though.
One more "issue" that I've got when switching from asp:ImageButton to telerik:RadImageButton was the necessity to set the width and height explicitly. Without it, it didn't work ouf of the box. Image (icon) was malformed, even though it was like 24x24 sized. I ended up setting width="" and height="" attributes, but adding CssClass and using styles should work too.

Related

ImageUrl not finding images in project folder

I want to create a website which will store 2 images in the "home" page, which will lead to 2 different pages. In the case, I'm using the Image object from the toolbox. When I'm trying to assign the ImageUrl, it don't appear to find anything at all, regardless of the format. I've tried with multiple photos, different folders, there's the result:
I've also tried to change it programatically in C#, not working, as well:
Image1.ImageUrl = #"Images/left.jpg";
Should I use the usual <img src=""/> for finding images? Thank you very much!
Right click on that image shown and choose "include in project" and then try to give imageUrl
If you are sure that your path value contains the appropriate path from the ImageFiles directory, then you should be able to use the following :
<asp:Image ID="Image1" runat="server" ImageUrl='~/ImageFiles/<%# Eval("path")%>' />
or use the integrated string formatting of the Eval method as shown here :
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("path", "~/ImageFiles/{0}") %>' />
From the code behind you could do something like below including application base URL some thing like below-
Image1.ImageUrl = #"~/Images/left.jpg"
Please refer the below discussion for more information -
Image isn't showing in Image and ImageButton
ASP.NET will automatically replace the ~ with your application's base
URL, because Image1 is a server control

How to change background of CKeditor from code behind C#

Simple question really. How can I change the background color of the CKeditor from C#?
How do I get an instance of CKeditor in C#? Probably I cannot?
I have a gridview with lots of textareas (asp:textbox) controls all using the CKeditor, via the CSSclass property, and it works great! But now I want to dynamically change one or two, or all of their background colours to something like LightYellow.
I've tried to directly change the background of the asp:textbox, but it doesn't work of course because that's "hidden" from the CKeditor itself.
Any other tips please?
UPDATE
I've downloaded the CKEditor for ASP.net and it too does not work, as it also creates a textarea element in the background automatically - effectively the same as using the CKeditor natively with CSSclass="".
Referencing the control in C#, I can do that now, which is great so I can get the data and use it in my database, but I still cannot change the CKeditor's background. The CKeditor's BODY element (tested via FireBug), is the one I need to change, but how from C#?
Thanks again
First, be sure you have installed both CKEditor and CkeditorForASP.NET packages via Nuget.
Afterwards, create an editor.css file which will contain only editor related styles such as:
.lightYellow {
background-color: lightyellow;
}
On your grid view, bind to OnRowDataBound event and specify base path of CKEditor scripts correctly.
<asp:GridView ID="EditorGridView" runat="server" OnRowDataBound="EditorGridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<CKEditor:CKEditorControl ID="Editor" runat="server" BasePath="~/Scripts/ckeditor" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then you will be able to change the body color as follows:
if (e.Row.RowType == DataControlRowType.DataRow)
{
CKEditorControl editor = (CKEditorControl)e.Row.FindControl("Editor");
editor.BodyClass = "lightYellow";
editor.ContentsCss = ResolveUrl("~/Content/editor.css");
}

postback when scrolling ListBox in chrome

with scrolling list box, the page will refresh(unwanted).
this problem is only in chrome (Version 27). In other browsers it works properly.
.aspx file:
<asp:Label runat="server" ID="label1" ></asp:Label>
<asp:ListBox ID="ListBox1" runat="server"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
DataValueField="f1" DataTextField="f2" DataSourceID="SqlDataSource1"
Rows="15" AutoPostBack="true" >
</asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="sp1" SelectCommandType="StoredProcedure"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>">
</asp:SqlDataSource>
.cs file :
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = ListBox1.SelectedItem.Text;
}
We've noticed this unfortunate bug only recently, on a page that had been working without issue for a very long time. It is specific to Google Chrome version 27, and I am currently using version 26.
The bug:(clicking anywhere inside of the control - the scroll bar being the focus of the issue - causes a complete postback [provided you set the AutoPostBack attribute to true])
The bug could be at a higher level of scripting, and I'm not sure it affects all of our listboxes. It seems unlikely as we have many, on multiple pages, and we would be getting calls if all of them exhibited this behavior.
Our solution contained two options, with another option being less classy:
1) Impractical: to wait for an update for Google Chrome, or use version 26 explicitly. This is impractical for a large userbase which doesn't have permissions for installation or the ability to roll back to a previous version. It also doesn't work if you, for whatever reason, absolutely must test against the latest version of Chrome.
2) We have access to Telerik controls which enable us to use RadListBox instead, slightly more viewstate overhead which may not be a good solution for you, if it's an option at all. This was the option we chose, as the RadListBox escapes the problem behavior.
A distant third, substantially less appealing solution: Find some other alternative for displaying your data, such as a dropdown list, possibly with a secondary subselecting control if you're dealing with a particularly large set of information. It's more work, in the interim, and you would likely wish to revert your changes when a fix was made.
I know all of these are mediocre solutions, but they're possible workarounds. Sorry if this isn't much help.
This is a bug in some Chrome versions (as others have noted). I was getting the same behaviour on Chrome on an earlier v27 release.
You should upgrade Chrome to the latest version: my version is currently v 27.0.1453.116 m and the problem appears to be fixed in this release.
This is an issue in v27 of Chrome, updating to the latest version should fix this.
http://googlechromereleases.blogspot.co.uk/2013/06/stable-channel-update_18.html
The JavaScript function mypostback doesn't work if the listbox has SelectionMode="Multiple"
Disable the AutoPostBack for the ListBox, use the onClick attribute of the ListBox to run a javascript doing __doPostBack for it. It is a work around. I think Google should fix this Chrome bug (ver 27, and 28, ...). It, AutoPostBack True of ListBox, works fine at all other browsers. TY Pien.
<script type="text/javascript">
function mypostback(id, parameter)
{
__doPostBack(id, parameter)
}
</script>
<asp:ListBox ID="lstbox_id" runat="server" onclick="mypostback('lstbox_id','')">
</asp:ListBox>
It's definitely a bug in Chrome (e.g. v.27.0.1453.110 m). See this answer too.

Text with RegEx Validator not working

I have an issue with the following text display:
<asp:RegularExpressionValidator ID="Password_RegularExpValidate" runat="server"
Text="TEST!"
Display="Dynamic"
BorderStyle="None"
ControlToValidate="txtNewPass"
ValidationExpression="(?=^.{8,255}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*"
meta:resourcekey="Password_RegularExpValidateResource1" /></td>
The pattern by itself is:
(?=^.{8,255}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*
The text initially had some stuff in it as the ValidationExpression was different. I've changed the regex expression and that works, but when I write something in Text= it doesn't update on the page. I've restarted IIS, cleard the IE chache... everything I could think of. The old text keeps appearings (ie. "TEST!" doesn't show up when the validation fails as it should).
Any help would be appreciated.
Edit:
The code for txtnewpass:
<asp:TextBox ID="txtNewPass" runat="server"
TextMode="Password"
MaxLength="256"
meta:resourcekey="txtNewPassResource1"></asp:TextBox>
Also, it's worth noting that I can remove an entire table from a page that it disappears when I reload the page. But when I change text values from controls or anything that runat="server" and the page doesn't seem to update with the text....
And the code behind doesn't edit the field that displays, the validator validates the text in the textbox and uses that value later.
Edit 2: Same thing happening with -
<asp:Label ID="Label1" runat="server"
Text="Change Password!!!!!"
meta:resourcekey="Label1Resource1"></asp:Label></td>
I've added the exclamation marks (!!!!!) and that's not showing up when I refresh the page either.....
Edit 3: As I've noted in one of the comments, if I delete a table from the page and reload the page, that table disappears, so I know the page is reloading properly. The runat="Server" property, does that work a certain way where it caches text or something? I'm out of ideas....
Like Kirill said, use ErrorMessage instead of Text.
But the main problem is, I think, your localization, which is handled through the meta:resourcekey tag and resources.
Here is a good explanation:
ASP.NET meta:resourcekey
If you set automatically or manually the resource localization file and change something afterwards, for example a Label Text property, then you need to do it in the resource file too. Because there should be your initial value, which is loaded at runtime.
There are a lot of possibilities for the source of the problem, and there's not really enough info to tell which one it is. It sounds to me like one of these:
1) An external issue with your application or page (perhaps your ViewState isn't being set up properly, or the validation is getting called before PostBack).
2) You should be using RegularExpressionValidator.ErrorMessage instead of Text, as Kirill suggested. You said that you had changed that, but I wonder if you've reloaded the page (you could try rebuilding the app or something if it's getting cached somehow).
3) Your regex might not be doing what you think it is. The pattern is extremely long, and it seems strangely written. Adding some whitespace, we find that it looks like this:
(?=^.{8,255}$)
(
(?=.*\d)(?=.*[A-Z])(?=.*[a-z])
|
(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])
|
(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])
|
(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9])
)^.*
Which simplifies down to something like:
^(
(?=.*[A-Z])(?=.*[\W_])(?=.*[a-z\d])
|
(?=.*[a-z])(?=.*\d)(?=.*([A-Z\W_]))
).{8,255}$
Is this what you intended? I see where you're going with the password rules, but it may be easier to just simplify them and require one capital, one lowercase, one number, and one special character. You could also try to get validation working with a simpler regex pattern, and then add complexity once everything else is working.
Try to use RegularExpressionValidator.ErrorMessage instead of Text.
Try restarting VS. Sometimes incredible bugs disappear after that.
Try removing and regenerating your ..aspx.designer.cs file (to
regenerate right click the aspx/ascx file and choose "convert to web
application").
Agree with others, you should indeed check your resource file.

AJAX.NET Reorderlist Control - "It does not a DataSource and does not implement IList."

I keep getting this error when trying to re-order items in my ReorderList control.
"Reorder failed, see details below.
Can't access data source. It does not a DataSource and does not implement IList."
I'm setting the datasource to a DataTable right now, and am currently trying to use an ArrayList datasource instead, but am discouraged because of this post on the internet elsewhere. The control exists within an update panel, but no other events are subscribed to. Should there be something special with the OnItemReorder event? Just confused as to how it works.
My question is, does anyone have any direct experience with this issue?
I figured it out. I converted the DataTable to an ArrayList then bound to the control. Thanks everyone for reading!
I got the same error message. In my case it happened because I was trying to set the SortOrder field to a non-numerical field. The control can sort the list only by a field whose type is integer (or similar). Not a string or date.
I've used it successfully in the past without much issue (binding to a List). Could you post some snippets of what you have in your front-end and code-behind?
<cc1:ReorderList id="ReorderList1" runat="server" CssClass="Sortables" Width="400" >
<ItemTemplate>
<div class="sortablelineitem">
<a href="#" class="albmCvr" id="song13">
<img src="/images/plalbumcvr.jpg" alt="Name of Album" class="cvrAlbum" width="10"
height="10" />
Song 1 <span>by</span> Artist 1 </a>
<asp:ImageButton ID="ImageButton13" runat="server" ImageUrl="/images/btn_play_icon.gif"
ToolTip="Play Clip" CssClass="playClip" />
</div>
</ItemTemplate>
<EditItemTemplate>
<h1>WHOA THE ITEM IS BEING DRAGGED!!</h1>
</EditItemTemplate>
<ReorderTemplate>
<div style="width:400px; height:20px; border:dashed 2px #CCC;"></div>
</ReorderTemplate>
<DragHandleTemplate>
<div style="height:15px; width:15px; background-color:Black;"></div>
</DragHandleTemplate>
<EmptyListTemplate>
There are no items in this playlist yet...
</EmptyListTemplate>
</cc1:ReorderList>
</ContentTemplate>
</asp:UpdatePanel>
is my Front end, and in the back end I'm just getting a datatable object and binding it OnLoad of a Non postback...
ReorderList1.DataSource = ds.Tables[1];
ReorderList1.DataBind();
Do I need to set the datasource again when the items are reordered?
I found the same error caused when the table I was trying to sort had no initial values allocated to the DataKeyField. This had me tearing my hair out as it worked in my test environment, but not when I pushed it live. I'd also note that it threw a dialog box with the same message ON MY WEB SERVER CONSOLE. This had an abort/retry/ignore button set so effectively killed everything. Now that's just rude!
The solution was just to consecutively number the field values before using the control.

Categories