Asp.Net Treeview Selecting same node doesn't fire - asp.net

I have an asp.net treeview which uses the OnSelectedNodeChanged event and works ok, but if you click the same node again it doesn't fire, any ideas how to get round this?
Treeview:
<asp:TreeView ID="tvSOWASP" runat="server" ImageSet="Arrows"
ShowLines="True" OnTreeNodePopulate="PopulateNode" OnSelectedNodeChanged="SelectNode">
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<Nodes>
<asp:TreeNode Expanded="True" ImageUrl="~/tree2/icons/book.gif"
SelectAction="None" Text="Schemes Of Work" Value="Schemes Of Work">
</asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>
Code-Behind:
protected void SelectNode(Object sender, EventArgs e)
{
// Code here, ok when select any node, select same node and this code is not hit
}

Hey Please try this one.
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){
// Do whatever you're doing
TreeView1.SelectedNode.Selected = false;
}
Hope it helps you

According to me OnSelectedNodeChanged event of any control will be the ID of that control with the event name like your control name is tvSOWASP so it's event would be tvSOWASP_SelectedNodeChanged not SelectNode so change your SelectedNodeChanged event with my code like
protected void tvSOWASP_SelectedNodeChanged(object sender, EventArgs e)
{
// Your code...
}
so remove your OnSelectedNodeChanged="SelectNode" from your code and also it's click event and try to make a new event as per i mentioned.
Hope it understand and worked for you.

It will not fire when you click the same node again because the second time the selection is not changing so the selectednodechanged event wouldn't fire.
Please refer this link

I found the deselecting the current node at the end of the SelectedNodeChanged event caused problems later on during the page cycle. When the control itself rendered it wasn't showing the selected node.
Instead I added some code to the Page_Load event to clear out the current selection.
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack) {
string eventTarget = Page.Request["__EVENTTARGET"];
if(eventTarget == TreeView1.ClientID.Replace('_', '$')) {
// The TreeView is posting back a selection change
// Clear out any existing selection so that selecting the same node
// will trigger the change event.
TreeView1.SelectedNode.Selected = false;
}
}
}

Use e.Node.Collapse(); in the treeView_TreeNodeExpanded

Related

An incorrect even gets triggered if the browser back button is clicked

Here's my aspx page
<asp:Label runat="server" ID="lbl" Text="suff goes here" /><br />
<asp:Button runat="server" OnClick="btnClick" ID="btn" Text="Click me!" />
<asp:DropDownList runat="server" ID="ddl"
OnSelectedIndexChanged="ddlChanged"
AutoPostBack="true">
<Items>
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</Items>
</asp:DropDownList>
And here's the code behind
protected void btnClick(object sender, EventArgs e)
{
lbl.Text = DateTime.Now.Ticks.ToString();
}
protected void ddlChanged(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx");
}
When I click something in the dropdown, I get redirected to Page2, I then click the browser back button, and click the "Click Me!" btn. Instead of the btnClick even getting fired, it fires ddlChanged first, and does the redirect again.
Can this be solved without javascript?
You can use a session variable and store the last value in the dropdown, so when it fires you are like double-checking that effectively the dropdown.SelectedIndex change
protected void ddlChanged(object sender, EventArgs e)
{
// If the var is NULL then it is the first time the event gets triggered
if (Session["lastDropDownValue"] != null)
//If it has a value, and also is the same selected index, then it really doesn't change
if (Convert.ToInt32(Session["lastDropDownValue"].ToString()) == ddl.SelectedIndex)
return;
//Set your variable for future validations
Session["lastDropDownValue"] = ddl.SelectedIndex;
Response.Redirect("Page2.aspx");
}

ComboBox generate ICallbackEventHandler not found

Im using Devexpress ASP.NET AJAX ComboBox and trying to filter the result using callback events. This is the ASP markup for the ComboBox control
<dx:ASPxComboBox runat="server"
EnableViewState="false"
Width="100%" ID="Project"
ValueField="Value" TextField="Text"
CallbackPageSize="10"
EnableCallbackMode="true"
IncrementalFilteringMode="Contains"
OnItemsRequestedByFilterCondition="Project_ItemsRequestedByFilterCondition"
OnCallback="Project_Callback"
DropDownRows="10"
TextFormatString="{0} {1}">
<Columns>
<dx:ListBoxColumn Caption="Projekt" FieldName="Value" Name="value" />
<dx:ListBoxColumn Caption="Kund" FieldName="Text" Name="text" />
</Columns>
</dx:ASPxComboBox>
And here is the code behind
protected void Project_ItemsRequestedByFilterCondition(object source, DevExpress.Web.ListEditItemsRequestedByFilterConditionEventArgs e)
{
var data = handler.GetProjectList(e.Filter);
Project.DataSource = data;
Project.DataBind();
}
protected void Project_Callback(object sender, DevExpress.Web.CallbackEventArgsBase e)
{
}
Every time i try to write in the combobox the page says that the IEventCallBackHandler is not found. Please see the attached image.
I found the problem. The page is inherit our own base class that has a custom implementation of FindControl.

Click Event for ImageButton Inside RadGrid

I have an asp.net ImageButton inside a RadGrid (in a column) that when clicked opens a popup window. I can also expand this same RadGrid to reveal a nested grid. I have a button inside here that I need to assign a click event to such that it opens the same popup. How do I fire off an ImageButton click that's housed inside a RadGrid?
Here is the aspx and the "imgEdit" is what I need to fire off:
<MasterTableView DataKeyNames="SuggestionID">
<EditFormSettings CaptionFormatString="Edit Suggestion: {0}" CaptionDataField="Title"
InsertCaption="Add Suggestion" EditFormType="WebUserControl" PopUpSettings-Width="655px"
UserControlName="~/CommonUserControls/SuggestionControl.ascx">
</EditFormSettings>
<Columns>
<telerik:GridTemplateColumn UniqueName="EditAction" HeaderStyle-Width="32px" ItemStyle-Wrap="false"
Resizable="false">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CommandName="Edit" Resizable="false"
ImageUrl="/ESDNET/Images/Icons/pencil.png" ToolTip="Edit Suggestion" Visible='<%# Eval("CanEdit") %>' />
You can achieve the same in different ways:
1) Directly attach the button click event of imagebutton
protected void imgEdit_Click(object sender, ImageClickEventArgs e)
{
// your code
}
2) Using CommandName:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
if(e.CommandName=="Edit")
{
}
}
3) If the commandname is Edit ,then it will autometically fire EditCommand
protected void RadGrid1_EditCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
}

Two Buttons updating one item of a listview

I have two buttons in a ListView. With a click on the first Button I want to update the ListView-Item. With a click on the second button i want to update the ListView-Item and redirect to a different page. Both Buttons have a property CommandName="Update". I wanted to solve my problem with the CommandArgument-Property and the OnItemUpdated-Event, but I do not know how to get the value of this Property in the event.
<asp:ObjectDataSource ID="ods" runat="server" SelectMethod="Select" UpdateMethod="Update">
<SelectParameters>
<asp:Parameter ..... />
</SelectParameters>
<UpdateParameters>
<asp:Parameters .... />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ListView ID="lv" runat="server" DataSourceID="ods" DataKeyNames="ID" OnItemUpdated="lv_OnItemUpdated">
<ItemTemplate>...</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" CommandName="Update"/>
<asp:Button ID="btnUpdate2" runat="server" CommandName="Update"/>
</EditItemTemplate>
</asp:ListView>
And in codebehind:
protected void lv_OnItemUpdated(object sender, ListViewUpdateEventArgs e)
{
...
}
Is it possible to decide in lv_OnItemUpdated which Button the user clicked?
I don't believe there is a way to distinguish which control issued the Update command, since sender is the ListView itself.
A workaround would be for you to give one button the CommandName "Update", and the other "UpdateRedirect".
The "UpdateRedirect" button will fire the ListView_ItemCommand event, and from there you can call ListView.UpdateItem, keeping your updating logic in there, and then redirect next.
Why do you insist on using the OnItemUpdated event?
Well there are 2 to 3 ways of doing it: One is of CommandArgument as:
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>
and than on server side you can have:
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
if(e.CommandName == "Sort")
//do you work and so on
}
or you can cast the sender as button and take it ID to see, which button was it:
((Button)sender)).ID
or you can get button ID as:
String ButtonID = Request["__EVENTTARGET"];
I hope it will help you in fixing your problem.
so you can have like:
protected void lv_OnItemUpdated(object sender, ListViewUpdateEventArgs e)
{
// either use e.CommandName
// or user ((Button)sender)).ID
}
Give each button a distinct name. Have the same event handling method in your code behind handle both button click events. Then check which button called the method.
EDIT: A workaround would be to use javascript to put the name of the clicked button into a hidden field on the form BEFORE it goes server side (using a client side script). Then in your Listview you could check the value of the hidden field to see which button was clicked.
Ok, yes, the itemcommand is what you want:
The lifecycle for an update or insert triggers both their native events AND the itemcommand event. The itemCommand event will occur prior to the itemUpdating or the itemInserting events.
So, you can create a boolean variable called called "bSecondButtonClicked" for example Add the command argument to both buttons with the Commandname='UPDATE". the e.command argument can be evaluated at the itemcommand event point. There set your Boolean variable (or however you implement it) to true. Then, at the itemupdating event, trigger your code based on the bSecondButtonClicked.
You need to get into the ItemCommand event of your ListView
protected void lstvw_ItemCommand(object sender, ListViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "new":
try
{
//e.CommandArgument
//e.CommandSource
// do your stuff here
}
catch (Exception ex)
{
}
break;
default:
break;
}
}

How to get Postback Data on LinkButton Click Event?

I have a LinkButton in aspx page.
<asp:TextBox ID="textBoxNote" runat="server" />
<asp:LinkButton ID="linkButtonUpdateNote" Text="Update" OnClick="ButtonUpdateNoteClicked" runat="server" />
the click event handler has the following code
protected void ButtonUpdateNoteClicked(object sender, EventArgs e)
{
var note = textBoxNote.Text;
}
On Postback textBoxNote.Text is empty. I don't get the posted value. How to get the value?
It seems like you are possibly resetting the value in your Page_Load.
Check that you are using IsPostback check in the Page_Load function. see - http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
private void Page_Load()
{
if (!IsPostBack)
{
DoThisOnce();
}
DoThisOnEachPostback();
}

Resources