ASP.Net TreeView with SiteMap is ignoring Node.Selected - asp.net

I create have a TreeView bound to a SiteMap. It works great.
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
Now I want to change the way the selected node looks.
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
<NodeStyle ImageUrl="~/Images/Page.png" />
<SelectedNodeStyle ImageUrl="~/Images/Page_Hot.png" />
</asp:TreeView>
The thing is, the current page is not automatically selected on the tree (why MSFT, why?). This is not the end of the world. So, I created a little code behind like this:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
TreeView1.TreeNodeDataBound += new TreeNodeEventHandler(TreeView1_TreeNodeDataBound);
}
void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
var _CurrentUrl = Request.Url.AbsolutePath;
e.Node.Selected = (e.Node.NavigateUrl == _CurrentUrl);
}
But still the node is not selected. My gut tells me it's the wrong event.
Any help?
Thanks // Jerry

The AbsolutePath vs the NavigateUrl might be suspect, one is a relative path (NavigateUrl) and the other, as the name implies, is an absolute path. If you run in debug mode can you see that the two values are indeed the same?

Well, this is really annoying. I suppose the "technical" answer to my question is that it worked all along. However, the real issue is that ImageUrl in the SelectedNodeStyle appears to be ignored. I am going to paste a little more of my solution so you can see what I have, but no matter what I try, ImageUrl in the SelectedNodeStyle is ignored.
(PS: .Net4)
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" ShowExpandCollapse="false">
<NodeStyle ForeColor="White" Font-Bold="true" NodeSpacing="5" HorizontalPadding="5"
ImageUrl="~/Images/Page.png" />
<SelectedNodeStyle Font-Bold="true" NodeSpacing="5" HorizontalPadding="5" Font-Underline="true"
ImageUrl="~/Images/Page_Hot.png"/>
<HoverNodeStyle ForeColor="Navy" />
</asp:TreeView>

Related

How to clear telerik radcombo box (or) populate with "Empty Message" property value on a function call?

<telerik:RadComboBox ID="rbt" runat="server" Skin="Office2010Black" AllowCustomText="true" CheckBoxes="True" EnableCheckAllItemsCheckBox="true" Width="125px" TabIndex="7" MarkFirstMatch="true" ToolTip="Select" EmptyMessage="Select" Height="100px" Filter="StartsWith">
<Items>
<telerik:RadComboBoxItem runat="server" Text="High" Value="High" />
<telerik:RadComboBoxItem runat="server" Text="Medium" Value="Medium" />
<telerik:RadComboBoxItem runat="server" Text="Low" Value="Low" />
</Items>
</telerik:RadComboBox>
I tried in code behind:
public void call()
{
rbt.ClearSelection();
rbt.Text = "Select";
}
I could not clear selection with the above code. I want to uncheck selected Items of a radcombo box when call() function is called. Appreciate if anyone can tell if anything is missing.
You can clear the items in the RadComboBox by using Clear server side method.
C#:
RadComboBox1.Items.Clear();
with:
using Telerik.Web.UI;
The following code in code behind resolved my problem of deselecting values of combobox:
public void call()
{
if (rbt.CheckedItems.Count > 0)
{
foreach(RadComboBoxItem rcbItem in rbt.Items)
{
rcbItem.Checked = false;
}
}
}

How to set session variable on a menu item's click in ASP.NET

ASP newbie here, in my website I need to set a session variable when I click the menu item( not on page load or pre init or init).
How can I achieve this, I have a menu control in my master page which has a sitemap file attached to it?
How to know when a particular menu item is clicked?
<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapSource"
StaticDisplayLevels="10" Width="150px">
<StaticSelectedStyle CssClass="menuNodeSelected" />
<LevelMenuItemStyles>
<asp:MenuItemStyle Font-Bold="True" Font-Underline="False" />
</LevelMenuItemStyles>
<StaticMenuItemStyle CssClass="menuNode" />
</asp:Menu>
<asp:SiteMapDataSource ID="siteMapSource" runat="server" ShowStartingNode="False" />
Based on your code and documentation finded on msdn you should have something like this:
On Markup Code (that will result in HTML which will be sent to Client)
<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapSource"
StaticDisplayLevels="10" Width="150px"
OnMenuItemClick="NavigationMenu_MenuItemClick">
<StaticSelectedStyle CssClass="menuNodeSelected" />
<LevelMenuItemStyles>
<asp:MenuItemStyle Font-Bold="True" Font-Underline="False" />
</LevelMenuItemStyles>
<StaticMenuItemStyle CssClass="menuNode" />
</asp:Menu>
<asp:SiteMapDataSource ID="siteMapSource" runat="server" ShowStartingNode="False" />
You should set a method to be called on server side OnMenuItemClick, this will rise the event of menu click. That event is (in our case): NavigationMenu_MenuItemClick.
On Code-Behind you can do whatever you want when an menu item is selected.
void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
{
// Display the text of the menu item selected by the user.
Message.Text = "You selected " + e.Item.Text + ".";
Session["variable"] = e.Item.Text;
}
In e.Item.Text; you find what element has been selected.
Based on: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.menuitemclick(v=vs.110).aspx
The ASP:Menu has an Click event. You can handle this event to set the Session Variable.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.menuitemclick.aspx
Declare it:
<asp:Menu ID="mainMenu" runat="server" onmenuitemclick="NavigationMenu_MenuItemClick" ...
And handle it:
void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)
{
// Display the text of the menu item selected by the user.
Message.Text = "You selected " +
e.Item.Text + ".";
}

How to set a asp:DropDownList SelectedValue to a Session Variable?

There are several articles describing how to do this is code behind however:
Is it possible to set the value of a dropdownlist to a session variable on the aspx page?
I am using SqlDataSource to populate the dropdownlist so do not wish to add code behind if it can be avoided.
<asp:DropDownList ID="ddl" runat="Server" DataSourceID="sqlDS" DataValueField="ID" DataTextField="TEXT" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True" />
</asp:DropDownList>
<asp:SqlDataSource ID="sqlDS" runat="Server" SelectCommand="spDS" SelectCommandType="StoredProcedure" />
Set Session("ID") as selected value on load?
The dropdown list is already populated from the sqldatasource. I just want to set the initial value on page load.
You need a server side code in order to use Session. The following code doesn't requires code behind file, but again the code inside script will run at server side.
<asp:DropDownList ID="ddl" runat="Server"
DataSourceID="sqlDS"
DataValueField="ID"
DataTextField="TEXT"
AppendDataBoundItems="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem Text="" Value="" Selected="True" />
</asp:DropDownList>
<asp:SqlDataSource ID="sqlDS" runat="Server"
SelectCommand="spDS" SelectCommandType="StoredProcedure" />
<script runat="server">
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
Session["SelecteValue"] = ddl.SelectedValue;
}
</script>
Note: Make sure AutoPostBack="True" for DropDownList.
Do not mix code with markup. It makes sense to separate them for many reasons. So ASPX will have just the presentation, and CS/VB just the code logic.
When you compile/deploy your side to production - there will not be "the second page" - only ASPX page will remain. Code will be compiled into a DLL.
you'll need an event for your dropdown list on change. Are you using C# or VB.net for your codebehind?
add to onSelectedIndexChanged="ddl_OnSelectedIndexChanged"
to your code behind add:
{this is C# vb is similar}
protected void ddl_OnSelectedIndexChanged(Object sender, EventArgs e)
{
Session["selectedID"] = ddl.selectedValue;
}
to your page load, add
if (isPostback)
{
ddl.selectedValue = Session["selectedID"];
}

How to find control nested into ASPXGridview on button click

I have a text box and button inside of my ASPXGridview, DetailRow view. So when it's expanded I need to type text and click button and on server side to get value from the textbox.
Here is my code. The problem is somewhere on postback I'm loosing text value. So text box is empty
protected void Button1_Click(object sender, EventArgs e)
{
ASPxGridView grid = EmailGridView;
for (int i = 0; i < grid.VisibleRowCount; i++)
{
var txtDescription = (ASPxMemo)grid.FindDetailRowTemplateControl(i, "txtDescription");
if(txtDescription != null)
{
var text = txtDescription.Text;
}
}
}
<dxwgv:ASPxGridView ID="EmailGridView" KeyFieldName="ThreadId" runat="server" AutoGenerateColumns="False"
OnHtmlRowCreated="EmailGridView_HtmlRowCreated" SettingsDetail-AllowOnlyOneMasterRowExpanded="true"
SettingsBehavior-ConfirmDelete="true" OnHtmlRowPrepared="EmailGridView_HtmlRowPrepared"
OnRowDeleted="EmailGridView_RowDeleted">
<SettingsBehavior ConfirmDelete="True" />
<Columns>
<dxwgv:GridViewDataTextColumn Caption="ID" FieldName="Id" VisibleIndex="0">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewCommandColumn VisibleIndex="5" Caption=" ">
<DeleteButton Visible="True">
</DeleteButton>
</dxwgv:GridViewCommandColumn>
</Columns>
<SettingsDetail ShowDetailRow="true" />
<SettingsBehavior ConfirmDelete="True" />
<SettingsDetail AllowOnlyOneMasterRowExpanded="True" ShowDetailRow="True" />
<Templates>
<DetailRow>
<dxwgv:ASPxGridView ID="EmailSubGridView" Width="750px" OnBeforePerformDataSelect="EmailSubGridView_BeforePerformDataSelect"
runat="server" AutoGenerateColumns="False"
onhtmlrowcreated="EmailSubGridView_HtmlRowCreated">
<Columns>
<dxwgv:GridViewDataTextColumn Caption="MessFrom" FieldName="MessFrom">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="Message" FieldName="Message">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataTextColumn Caption="SendDtm" FieldName="SendDtm">
</dxwgv:GridViewDataTextColumn>
</Columns>
<StylesEditors>
<ProgressBar Height="25px">
</ProgressBar>
</StylesEditors>
</dxwgv:ASPxGridView>
<dx:ASPxMemo ID="txtDescription" runat="server" Width="170px" Height="71px"></dx:ASPxMemo>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Reply" />
</DetailRow>
</Templates>
Conny,
The ASPxGridView has callbacks built into it so I recommend using that approach as it's easier and it gives you a better user experience. The find method you're using requires the visiblerowindex.
Take a look a this code central example to see how to get data from the server:
How to bind the detail GridView to data based on the end-user input
If you want these process when you are expanding your ASPxGridview, you should use DetailRowExpandedChanged event.
This event works when you expand or collapse your ASPxGridview.
Mehul's example is very good actually for this situation. After that, if you still have a problem, you can ask your question in Devexpress Support. They are really helpful and quick.

ASP.NET Wizard Steps: Getting form data to next page?

I can't figure out how to pass form data from a Wizard Steps Control to a new page. I posted THIS POST some days ago, but the answer here didn't really help me because i can't even get the value from a TextBox on the new page.
I put tried to put this insted of the hiddenfield but <asp:TextBox ID="amount" runat="server" Text="tester"></asp:TextBox> but the Request.Form["amount"] is still just null.
How do i pass form data from a wizard steps control to a new page? Should this really be that hard?
For information that we collect in a wizard usually translates into a business object, then we just pass that object around in a Session variable. That way we have access to it on any page.
Session variable seems to be easier to work with:
Default.aspx markup:
<asp:Wizard runat="server" ID="wizAwesome" FinishDestinationPageUrl="~/TestPage.aspx" OnFinishButtonClick="wizAwesome_FinishButtonClick">
<WizardSteps>
<asp:WizardStep ID="stepRock" runat="server" Title="Rock!">
This is a wizard step.
<asp:HiddenField runat="server" ID="hiddenName" Value="Juliet" />
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
Default.aspx.cs
protected void wizAwesome_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
Session["hiddenName"] = hiddenName.Value;
}
TestPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = Session["hiddenName"].ToString();
}
Your HiddenField needs to be located outside of the Wizard like below and you need to add a FinishNavigationTemplate which posts the data to your new page
<asp:Wizard runat="server" ID="wzd_Amount">
<WizardSteps>
<asp:WizardStep ID="step_Amount" runat="server">
This is a wizard step.
</asp:WizardStep>
</WizardSteps>
<FinishNavigationTemplate>
<asp:Button runat="server" ID="btn_Finish" PostBackUrl="~/Labs/TestPage.aspx" />
</FinishNavigationTemplate>
</asp:Wizard>
<asp:HiddenField runat="server" ID="hdf_Amount" Value="Test" />
On the other page you can just Request the data like so
lbl_Test.Text = Request["hdf_Amount"];

Resources