Display an image for a TreeNode - c#

I try to display Images at a node in a Treeview. The Treeview is in an ascx control cause i need it multiple times.
I use the following code:
<asp:TreeView ID="FolderTree" runat="server" BackColor="White">
<NodeStyle ForeColor="Black" ImageUrl="~/App_GlobalResources/folderClosed.png" />
<ParentNodeStyle ImageUrl="~/App_GlobalResources/folderOpen.png" />
<RootNodeStyle ImageUrl="~/App_GlobalResources/folderOpen.png" />
</asp:TreeView>
Thsi code semms to be right for me, and its also displayed right in the designer.
But when i start the page, not a single picture is shown. Only the default collapsebuttons are visible.
What am i doing wrong?
Many thanks in advance.
Edit: I added the code how i populate the control
private void Page_Load(object sender, System.EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
var devData = Foo.GetDevices(); // returns a list with all devices
foreach (string temp in devData)
{
this.FolderTree.Nodes.Add(new TreeNode() { Text = temp, ImageUrl = "~/App_GlobalResources/folderOpen.png" });
}
}
}
catch(Exception error)
{
//errorhandling
}
}
Edit 2: I assume i have that bug: forums.asp.net/t/943367.aspx but the suggested Solution isnt working! (see code above, i already set the value via code. Does anybody know a solution/ a hotfix (to use in an webusercontrol(.ascx))?
I also tried (on my base site):
protected override void OnSaveStateComplete(EventArgs e)
{
foreach (TreeNode tmpNode in this.LeftSelectControl.AccessFolderView.Nodes) //AccessFolderView = var, which contains the treeview of the ascx (to access it)
{
tmpNode.ImageUrl = "~/App_GlobalResources/folderOpen.png";
}
}

Use the DHTMLX Tree View a Open source javascript library that combines well in C# Fluidly. It will produce nice tree view with images.
Its very simple to use in C# aspx page.
Click Here for Dhtmlx tree

Never done the way you styled your TreeView.
But this is how i style my Treeview. Hope it helps you.
<asp:TreeView ID="treeView" runat="server" NodeIndent="20" ExpandDepth="0" NodeStyle-HorizontalPadding="2"
ShowLines="true" ExpandImageUrl="../images/Open_Folder.png" CollapseImageUrl="../images/Close_Folder.png"
ForeColor="Black">
</asp:TreeView>

I found a Solution: Dont use App_GlobalResources.
I wont mark this as answer cause this was an stupid mistake by me. You can access App_* Folders only serverside, not clientside.

Related

OnClick button to refresh textbox not working

I have the following code in the code behind. Goal is to reset the value of the textbox to blank. When I click the button, however, nothing happens.
protected void btnReset_Click(object sender, EventArgs e)
{
Label srch_Title = (Label)FindControl("srch_Title");
srch_Title.Text = String.Empty;
}
Here is the textbox code from the main page:
<asp:TextBox ID="srch_Title" AutoPostBack="true" runat="server" />
Here is the button code from the main page:
<asp:Button ID="btnResetSearch" runat="server" OnClick="btnReset_Click" Height="35px" Text="Reset Search" Width="120px" />
I am a novice / enthusiast programmer and this is my first post. Guessing the problem is obvious and I am just not seeing it.
You should define your control as TextBox not Label , like this :
TextBox srch_Title = (TextBox )Form.FindControl("srch_Title");
srch_Title.Text = String.Empty;
You need to find the srch_Title control as a TextBox instead of as a Label, like this:
// The as operator avoids a cast exception,
// returns null if cast cannot be successfully performed
TextBox theSrchTitleTextBox = Form.FindControl("srch_Title") as TextBox;
// Verify that we found the text box before we try to use it,
// to avoid null reference exception
if(theSrchTitleTextBox != null)
{
theSrchTitleTextBox.Text = String.Empty;
}
Figured this out by adding another FindControl. First FindControl referred to the master and used the results of that to setup a second FindControl pointed at the text box. I did not post enough code when I asked the original question as I think an experienced programmer would have quickly spotted the solution. Thanks to all who contributed.

ASP.Net Post User control Post to new page

I am creating a custom user control that will act as a search feature. I want to easily be able to add this to multiple pages without having to modify much code.
I thought the best method to do this would be to create a simple user control that I can inject anywhere with one line of code and then have this control postback to a different URL. So wherever the search function is, it will always post back to the same page. My control looks like this:
<asp:TextBox ID="searchTextBox" runat="server" MaxLength="350"></asp:TextBox>
<asp:Button ID="submit" runat="server" Text="Search" PostBackUrl="~/myPostBackPage.aspx" />
myPostBackPage.aspx.cs looks like this, but it isn't grabbing the text.
protected void Page_Load(object sender, EventArgs e)
{
content.InnerHtml = ((TextBox)PreviousPage.FindControl("searchTextBox")).Text;
}
But it isn't pulling anything from the searchTextBox field and I get:
Object reference not set to an instance of an object.
Is there a better way to do this or how should I fix my code? Thanks!
I don't know where the TextBox is declared, if you for example use MasterPages the NamingContainer of it would be the ContentPlaceHolder of the master instead of the Page. Therefore just cast the PreviousPage property to the correct type:
YourPageType page = PreviousPage as YourPageType;
if(page != null)
{
content.InnerHtml = page.SearchText;
}
You have to provide a public property since the TextBox is protected(best-practise anyway):
public string SearchText
{
get { return searchTextBox.Text; }
}
I tested your code and PreviousPage is null. Try switch to Page.

Adding style to CommandItemSettings RadGrid button

I am trying to add a red asterisk after the 'AddNewRecordText' button with in RadGrid. Here is the code. The red asterisk should show after 'Add Award' text. Could you please let me know if there are any suggestions? Thank you.
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="false" Width="99%"
runat="server" MasterTableView-AllowAutomaticInserts="true" GridLines="None" Skin="Default"
ShowFooter="false" ShowStatusBar="false">
<MasterTableView Width="100%" DataKeyNames="c_id" CommandItemDisplay="Top"
CommandItemSettings-ShowAddNewRecordButton="true">
<CommandItemSettings AddNewRecordText="Add Award *" ShowAddNewRecordButton="true" />
RadGrid prerenders AddNewRecordText as a <a> element. and you cannot add different styles to one element.
To change that default behavior, you will have to use CommandItemTemplate.
This demo should be a good start for you.
Another simple approach, is that you always can change any style on your page with jquery.
this should work for you (works for me, probably you will have to change ids):
$('a[id$="InitInsertButton"]') //this means: find all <a> elements that have id ending with "InitInsertButton"
.first() //Get the first element to be sure.
.append('<span style="color:red"> *</span>') //Add text inside a attribute.
You will have to add this code on document.ready().
Here's a small jsfiddle to see it in action: http://jsfiddle.net/rXhnM/
So I'm late to the party again, but you can get a reference to that control on the server side:
protected void HandleGridItemCreated(object sender, GridItemEventArgs e)
{
if(e.Item is GridCommandItem)
{
var button = e.Item.FindControl("SaveChangesIcon") as Button;
var link = e.Item.FindControl("SaveChangesButton") as LinkButton;
button.Text += "<span class='red'>*</span>";
}
}
I needed to add a bit of javascript to run when clicked so this is how I managed to pull it off. I imagine adding * and appending to the text property yield the result you're looking for. We're on the 2016 version of the Telerik control set.

PostBackTrigger for FileUpload functionality inside a GridView

I have an asp:GridView control inside a .aspx page that the user can add several rows of data to. The user must also be able to attach a file to each row of data added.
For this I use the following inside the GridView:
<asp:TemplateField HeaderText="Upload" HeaderStyle-Width="120px">
<EditItemTemplate>
<asp:FileUpload ID="fuUploadLocation" runat="server" Width="98%" TabIndex="18" />
</EditItemTemplate>
</asp:TemplateField>
Then to save the location of the file upload I use the RowUpdating event in code-behind to set the value etc.
The problem is I can't register a PostBackTrigger for the control on the html as it doesn't pick it up as it's inside the GridView. I've tried setting it dynamically from other examples but can't seem to get this to work, the result being my FileUpload's FileName is always empty and the file then doesn't save correctly.
Any suggestions would be awesome.
Thanks
On Gridview row databound you have to find the file upload control and then add it to your UpdatePanel postback trigger.
I've not found a solution to my problem but I did work around it (sort of). I know just add a link to the grid, when user clicks it sends ID through via querystring, a new page opens that handles the entire upload process and returns the url of the saved document.
Not ideal but it works and saved me hours of frustration.
In the OnItemDataBound event handler of the grid need to call ScriptManager.GetCurrent(this).RegisterPostBackControl(ControlID);
This is an old post but for anyone that is stuck with it, you need to add the following to your control as stated in the other answers.
protected void ItemDataBound(object sender, EventArgs e)
{
Button myButton = (Button)e.Item.FindControl("myButton");
if (myButton != null)
ScriptManager.GetCurrent(Page).RegisterPostBackControl(myButton);
}
You also need to change the enctype in your form tag to the following e.g.:
protected void Page_PreRender(object sender, EventArgs e)
{
Page.Form.Attributes.Add("enctype", "multipart/form-data");
}
It should then work without an issue.

Setting content Page.Title dynamically from blog post title

I'm building a tiny blog app, and what I need to do is show blog post title for dynamically generated content page Item.aspx in page title.
In other words, i need data that is bound to this label in FormView to also be shown in page title when page loads.
<asp:Label ID="lblPostTitle" runat="server" Text='<%# Eval("PostTitle") %>' />
I'm using ObjectDataSource to get the data.
Tried a bunch of things, tried this (http://goo.gl/zWz1) to access Eval in code behind, but nothing worked.
Edit:
OK, i got the value from returned DataTable, it's easy
protected void odsItem_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
DataTable dt = (DataTable)e.ReturnValue;
string postTitle = dt.Rows[0]["PostTitle"].ToString();
}
But when I pass it to Page.Title nothing happens.
Please help.
Thank you.
Ok, i found the problem. Earlier, I added this code to Master page code-behind.
protected override void Render(HtmlTextWriter writer)
{
Page.Title = "Site name" + Page.Title;
base.Render(writer);
}
Once I commented it out, i was able to pass dynamic page title value to Page.Title.
This code worked great for appending the page title when content page Title was read from aspx file, but now i have to find another way to append the title.

Categories