how to set the OnSelectedIndexChanged in C# codebehind instead of ASP.net - c#

So in ASP.NET I would simply do this:
<asp:ListBox OnSelectedIndexChanged="UpdateModels" runat="server" > </asp:ListBox>
But my listbox is inside an .ascx file. which is registered and included in my actual webpage massupdate.aspx like this
<%# Register TagPrefix="mass" TagName="make" Src="~/DynamicData/Make.ascx" %>
<mass:make id="makeControl" runat="server"/>
and in my make.ascx.cs I have this
public ListBox getlistbox()
{
return DropDownList1;
}
So I have access to the original listbox, but I don't know how to replicate the first code snippet in C# in my codebehind for mass.update.aspx .

First of all, the event registered in the ascx should fire even though it is registered as a control onto the page. Events should bubble up (or maybe down?) through all of the controls on a page, that's part of the "magic" of webforms.
Secondly, if you do need to set an event handler in code, you can either google "add event handler .NET control" or you can type something similar below and Visual Studio intellisense should auto complete it for you after you type the "+=" and you can take it from there.
makeControl.getlistbox().OnSelectedIndexChanged += this.Index_Changed
void Index_Changed(Object sender, EventArgs e) {
// whatever you need to do on the event
}

Related

How to call a C# application from outside using JavaScript?

I have a C# application that uses WebForm. I am using the WebForm to display my application content (HTML / JavaScript).
My question is how do I communicate between them (API)?
Example: I like to minimize the program by using HTML buttons, etc....
If you have a web browser control on top of a normal control you could use the navigation event. E.g. make a link like:
Minimize
And in the navigation event of the browser control (I don't know how the event is actually called - just an example):
public void browser_OnNavigate(object sender, NavigateArgs e)
{
if (e.Target == "#MinimizeWindow")
// minimize and cancel event
else
// navigate to target
}
Local or Remote WebForms Application
You can use AJAX if you are trying to communicate with an external (or local) application.
Local WebForms Application
If by "application" you are actually referring to the back-end (code-behinds, etc.) of your WebForms project, then the other thing to look into are "server tags," otherwise known as "bee-stings." Here are just a few exsamples:
<% %>
<%-- --%>
<%# %>
<%= %>
Additionally, you can use event handlers for things like server-side button or anchor clicks, dropdownlist value changes, etc. You can make standard HTML controls server-side by adding the runat="server" attribute, or you can use .NET's WebControls (though they will still have to have the runat="server" attribute). Examples of these would be:
Front End
<button runat="server" onserverclick="btn_click">Click me</button>
...
or
...
<asp:Button runat="server" OnClick="btn_click">Click me</asp:Button>
Back End
protected void btn_click(object sender, EventArgs e)
{
...
}

using PreviousPage functionalities without AutoEventWireup

G'day,
I'm using PreviousPage to get values from controls from another page like this:
sPORyear1 = PreviousPage.year1.SelectedItem.Text.ToString();
It works just fine, however I noticed that the events from C# Code Behind (ex. Button click) are not handled properly. I tried making a breakpoint inside the event and it doesn't stop.
After looking at my aspx I noticed this:
AutoEventWireup="true"
When I set that to false, the buttons work properly, but the PreviousPage functionality doesn't work anymore.
The button code:
<asp:Button ID="export" runat="server" onserverclick="export_Click" Text="Export" />
Thanks!
When the AutoEventWireup is set to "False" Change the Page load Handler signature as below for the another page in which you are trying to access the value of previous page
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
Please note that the access modifier should be protected not Private in case of Autoeventwireup = "False" Otherwise you will not be able to capture the OnLoad event
this fixed it
Button On Click event not firing

BulletedList onClick not firing

Ugh, this is driving me mad
Im trying to build up a dynamic menu from a bulletedList, most menu items are plain links however the log out button needs to perform some cleanup code.
I cant for the life of me get the BullettedLists onclick event to fire.
The BulletedList is inside a user control (if that makes a difference)
Any ideas?
Or - any ideas for an alternative, better solution?
Code below
BulletedList
<asp:BulletedList OnClick="menu_Click" runat="server" CssClass="MainMenu" ID="loggedInMenu" DisplayMode="HyperLink" />
Adding an element
loggedInMenu.Items.Add(new ListItem("Logout", ""));
Click handler
protected void menu_Click(object sender, BulletedListEventArgs e)
{
user.logout();
Response.Redirect("Default.aspx");
}
You're using the wrong DisplayMode for your BulletedList control. You should use a DisplayMode of LinkButton. When you use DisplayMode.HyperLink:
Users can click links to move to
another page. You must provide a
target URL as the Value property of
individual items.
This is from the MSDN docs for this control. (It's about 3/4 of the way down the page.)
When you use a BulletedList control in HyperLink mode, the value of your ListItem is the URL that you're navigating to. So your static page HTML controls would use ListItem.Value as the href attribute of the <a> tag.
Here's what the HTML markup looks like when you use a DisplayMode of HyperLink (it's a plain old HTML anchor tag w/ a href):
<li>One</li>
But since you want to postback, you should set the DisplayMode of your BulletedList control to LinkButton. When you do that, you'll enable a postback back to your page and your event handler will trap the event. You can then process the click appropriately then. The event argument that's passed in (of type BulletedListEventArgs) will have an Index property, and that will tell you what item in your list was clicked.
Here's the updated .aspx code that I used:
<asp:BulletedList ID="bullet" runat="server" DisplayMode="LinkButton"
onclick="bullet_Click">
<asp:ListItem Text="One" Value="1">One</asp:ListItem>
</asp:BulletedList>
Everything else is the same except the DisplayMode, which is set to LinkButton. When I use that, then my bullet_Click event handler is fired when I click a list item.
I hope this helps!!

Creating a clicked event on ascx control when pressed

I have a question about creating and managing events inside an ascx custom web control.
I have created a very stupid control consisting in a div containing a asp:Label control, it is a very simple structure:
<div id="mydiv" runat="server">
<asp:Label id="mylabel" text="Text"... />
</div>
That is, very simple.
I would like to add an event: clicked. I want to let the user add this control on the page and attach handlers to this event so that when this control is clicked it is possible to do something. It might seem a strange solution, it's like i'm trying to invent again the button control, that is: my custom button.
Well to fire the event I would like to add a javascript in my div and call a js function that calls, using ajax mechanism, a server side function.
Well how to call a server side function from here. I posted a question about how to call a server side function from a client side one and got some answers (many of them told me to use PageMethods), well it seems that pagemethod does not work, it compiles but when running and clicking on my control and executing the js (in the line PageMethods.mymethod()) here I have an error --> Java script exception: unrecognized method. It seems not finding the PageMethod.
Well, considering my objective, how can I do?
Ah, a solution like: use the click event in the label is not what I want because the click event must fire when I click in the div, consider that I might set a large padding so that a large empty space can provide a large clickable area.
Thanks in advance.
Create a Event in your User Control
E.g: -
public event EventHandler<CustomEventArgs> Click;
and a handler
public void OnClick()
{
if(this.Click !=null)
{
//what ever else you do
this.Click(new CustomEventArgs(...));
}
}
Also handle the mouse click (mouseleftbuttonup) on the User Control (and fire your event from there). i.e.
protected void OnMouseDown(...){ this.OnClick(); }
From the ASP.NET page, you can register the user control event and handle it:
MyControl.Click += new EventHandler<CustomEventArgs>(myctr_click)
protected myctr_click(object sender, CustomEventArgs e){
//do anything
}
PageMethods uses a web service within the page class; I assume you want a postback to the server and process this click in the page, right?
All controls that postback use a __doPostBack('clientid', '') method to postback to the server. Your label would need to do the same. Within the control, the control needs to implement IPostBackEventHandler to process these events, and raise the click event.
Check out this example: http://www.myviewstate.net/blog/post/2009/05/14/Implementing-IPostBackEventHandler-in-an-ASPNET-Control.aspx
I may have misinterpreted your requirements, but can't you just wrap the div in an asp:linkbutton?
<asp:LinkButton ID="lnkSomething" runat="server" OnClick="lnk_something_Click">
<div id="mydiv" runat="server">
<asp:Label id="mylabel" text="Text"... />
</div>
</asp:LinkButton>
and then on the server side:
protected void lnk_something_Click(object sender, EventArgs e)
{
}
As yellowfrog mentioned in the comments, you could then extend this by further wrapping it in an update panel for an async postback.

FindControl doesn't work with bind server tag, why?

I have a FindControl in OnLoad event to find my button on page i.e.:
protected override void OnLoad(EventArgs e)
{
DataBind();
control button = Page.FindControl("myButton");
}
on my aspx page I have
<asp:Button runat="server" ID="myButton" />
If I only have this, everything works, when I pass in the OnLoad, button is not null and I can execute what I want. The problem is when I add dynamic text in my aspx:
<asp:Button runat="server" ID="myButton" Text='<%# "Here is my dynamic text pulled from a XML" %>' />
Then the FindControl finds nothing and the button is null.
Adding a binding server tag on aspx isn't suppose to delay anything right? When I inspect the Page object I have some controls in Controls collection, but I'm unable to find myButton.
Any idea on what I am doing wrong?
EDIT
People seem to think that my code example is my real code but it isn't, So I use FindControl because I need to since I have nested controls and I cannot access it directly and I use binding because the dynamic text I'm putting is inside a ContentTemplate which I can override in other page aspx.
The question I asked was more specific to the fact that I have traced the problem where my FindControl returns null because a newly implement behaviour which is the binding.
Improving the code example isn't a solution or an explanation to the fact that if I put a <%# %> tag in my aspx page, the FindControl in the OnLoad event return null.
EDIT 2
the bind tag alone seems to not be the culprit but the DataBind() to populate them. Whether or not I have bind tag, putting DataBind() before the FindControl makes the myButton to be null. Correction in code example was made.
In here MSDN says that :
PreRender : Each data bound control whose
DataSourceID property is set calls its
DataBind method.
It looks like you're not using DataSourceID of your data bound control, but moving your FindControl code to PreRender event might help.
The Page.FindControl() method will only search the imediate collection of controls that are associated with Page. It will not recurse down the entire control tree, so if your button is contained within another control it will not be found. You would need to call the FindControl method on the containing control.
If you want to access a button on your page, you can directly refer to the button as -
this.myButton
And as far as assigning values is concerned, you can do it like this in your server code -
this.myButton.Text = "Dynamic Text";
<%# xyz %> is only used when you are databinding the controls, for e.g. in a DataGrid, GridView, etc.
In your override dont you want to call base.OnLoad(e) in your method first ?

Categories

Resources