I have a repeater containing several several panels.
I have noticed that there is no onmouseover attribute of the <asp:Panel>.
I have read that I can add the attribute to the panel by calling in the page_load:
PanelID.Attributes.Add("onmouseover", "Script to Run");
The problem is that I am in a repeater, and the PanelID is generated by asp.net like ContentPlaceHolder_ctl01_myID_0 so not only it is hard to figure out, but VS2010 does not recognise it as a proper object and throws an error on it, plus I have to attach it on every item, so I need to use a for or foreach, but I don't know what to iterate on.
Is there a way like
foreach(childcontrol in Repeater.ChildControlsISpecificallyNeedPossiblyIdentifiedBySomeID)
{
childcontrol.Attributes.Add("onmouseover", "Script to Run");
}
to do this in C# in the Page_Load eventhandler?
I want to run a client side onload, onmouseclick and onmouseout on the panels too, so I want to attach those attributes too.
Thinking outside the box, instead of specifying the onmouseover-attribute inline using ASP.NET, you can attach the javascript event listeners in javascript.
Give the panel a CSS class:
<asp:Panel CssClass="panel"></asp:Panel>
Using jQuery, the syntax to bind the event handlers would be as follows:
$(".panel").mouseover(functionToRun);
If you're not using jQuery, you'll need a bit more code, but I'm sure you get the idea.
panel is represented with div on client side, you can get the client side id by using SomePanel.ClientID however if you are referring to the triggering panel you can always use this in the JavaScript
would not put all the event handlers on each element
Add the attribute to the panel in the event of repeater onitemdatabound and find the control in that, as this event will called for each and every time the row is bound so that you dont have to call it foreach.
You may consider using ItemDataBound event.
protected void myRepeater_ItemDataBound(object source, RepeaterCommandEventArgs e) {
if(e.Item.ItemType == ListItemType.Item){
Panel pnl = (Panel)e.Item.FindControl("myPanel");
pnl.Attributes.Add"onMouseOver", "yourFnctionName()");
}
}
Related
I'm having problems adding an event to an ImageButton. I have to create a set of buttons depending on a selected option from a DropDownList. The buttons are created successfully with Database data, but I'm can't attach the OnClick functionality.
The created buttons must share the same Handler.
protected void cmbServ_SelectedIndexChanged(object sender, EventArgs e) {
ServiceID = cmbServ.SelectedValue.ToString();
ServiceName = cmbServ.SelectedItem.ToString();
DataTable dtFirstTab = new DataTable();
dtFirstTab = mySQLConn.getTable(qryCarry); // LOAD DATA FROM DB
foreach (DataRow row in dtFirstTab.Rows) {
FTabBtn = "btn"+(Convert.ToInt32(row["SKU_Credito"])).ToString();
FTabIconURL = row["SKU_Icon"].ToString();
Panel dvFirstTab = new Panel();
dvFirstTab.CssClass = "col-xs-2";
ImageButton IB = new ImageButton();
IB.ID = FTabBtn;
IB.ImageUrl = FTabIconURL;
IB.Click += new ImageClickEventHandler(btnX_click); // <-- PROBLEM
dvFirstTab.Controls.Add(IB);
pnlIcons.Controls.Add(dvFirstTab); // pnlIcons exists in HTML
}
protected void btnX_click(object sender, ImageClickEventArgs e) {
string Obj = ((ImageButton)sender).ClientID;
Cantidad = Convert.ToInt32(Obj.Substring(3, (Obj.Length) - 3));
txtMonto.Text = "$" + Cantidad.ToString();
}
All the buttons appear correctly, but when I click on them they just fire a "submit" action, acting like there's no OnClick assigned.
No CodeBehid example:
If I add this line in HTML (I removed asp tags)
ImageButton ID="btn10" runat="server" ImageUrl="MontoLogo_10ST.png" OnClick="btnX_click"
It does work as intended.
Any ideas? Thanks a lot!
Creating controls dynamically in ASP.NET webforms usually seems easy at the beginning, but problems are very common when it comes to handling events. Even if you assign your event handler correctly, the event handler is not run in a postback until you re-create all the dynamic controls early in page lifecycle. This explains why the sample with the ImageButton on the ASPX works whereas the dynamically created buttons don't.
See this page for details on creating controls dynamically. The most important part is the warning that basically says: if you need to add dynamic controls, better don't.
Usually you can find a way to create all the necessary controls in markup, for instance using a Repeater control. The big advantage of the repeater is that you have control about the markup that is created.
The following sample outlines the necessary steps:
Place a repeater on your aspx-page. If pnlIcons serves no other purpose than being the container for the dynamically created buttons, substitute it by the repeater. Use the Header- and FooterTemplate properties to add the markup that surrounds the ImageButtons (e.g. the div for dvFirstTab).
Think about which data you need to assign to the image button. In your case, the fields "SKU_Credito" and "SKU_Icon" seem to be required.
Place the Image button in the ItemTemplate of the repeater and bind the properties "Id" and "ImageUrl" to the corresponding fields.
Add a Command event handler and bind the CommandArgument property to a value that helps you discern between the image buttons.
In the command event handler, you can use the CommandArgument to discover which button has been clicked. Add the appropriate code that handles the command.
In the SelectedIndexChanged event handler, read the data from the database and bind the repeater to the result. This creates the rows in the repeater with the ImageButtons.
Ok, I've found the reason. The event handling must be assigned in Page_Load event, so I moved everything inside a method and called it from Page_Load, calling it from "SelectedIndexChanged" doesn't work . It's working now.
Thanks!
I have written a code in VB.NET in which there are around 300 asp controls and all of them are creating dynamically with more than 6 condition per control (like If control = dropdownlist then some code Elseif control = radiobuttonlist then some other code).
Now I want to write events for some controls but due to postback, when event is fired all of the controls are getting flushed.
When I set button1.onclientclick="return false" for button, the page stopped post back but the event also stopped working.
I have an option to save the values of controls in view state then recreating the controls and then refill the values to dynamic controls. This option will increase my line of execution.
Is there any other method though which I can prevent the page to post back on asp control event so that my asp control persists with entered values in it and also my event will work.
this is the Code1
this is the Code2
Create your dynamic controls OnPreInit Event of Page, Hope this solves your problem
override protected void OnPreInit(EventArgs e)
{
CreateDynamicControls();//Function which creates all dynamic controls
}
I have used JavaScript and Ajax to achieve my requirement.
I have called JS function for button's onclick and Textbox's onchange (like: btn1.Attribute.Add("onClick",JSFunction(); return false;) [return false is to prevent postback].
Then I used ajax post method to do my stuff on .vb page.
I am working on a module where I am basically generating all the controls on the page dynamically using XSLT. They are being rendered and added to the mark up right. Here the problem is that I want to write event handling for this dynamically generated controls and I am not sure how to achieve that because in perfect development environment, we normally double click on our control on aspx page and .NET creates a related event for you in the back on the aspx.cs page.
Any ideas?
Dynamically-added controls generally do not survive postback.
No amount of double-clicking, or even typing the expected names of the controls with _selectedIndexChanged is going to get you what you want.
This is ASSUMING (please let us know if I'm right or wrong) that you are adding HTML controls, not ASP.NET controls dynamically.
You'll need to create a method with the appropriate handler and wire it up to your dynamically created controls when you create them.
i.e.
protected void MyHandler(object sender, EventArgs e)
{
//Do some stuff
}
when you create your controls
LinkButton lb = new LinkButton();
lb.ID = "lbexample";
lb.Click += MyHandler;
Page.Form.Controls.Add(lb);
But it's very important that on your postback, you rebuild these controls as they were or the event won't fire. You'll need to recreate the controls first so that the event can be raised, so any data that you'll need to create the controls will have to be available on the post back using whatever state mechanism you're comfortable with.
If you want to execute client-side events, you can specify the name of the method to fire (or the code itself) by adding the appropriate attribute.
For example, if you want to fire myCheckBox_OnClick when the user clicks your dynamically generated checkbox, you can do the following:
myDynamicallyGeneratedControl.Attributes.Add("onclick", "myCheckBox_OnClick");
You could also generate the javascript code that is to be executed and add it to the page through the page's ClientScript.RegisterStartupScript method.
So my problem is that I want to add an event handler to a dynamically created CheckBox. I have already looked at other ways to do this, and decided that creating a dynamic table which contains my CheckBoxes is the best option for me. I have not added these CheckBoxes to the Control Tree because I need to manage the ViewState manually. Either way, my code works in every way except that my CheckBox's CheckChanged Event does not fire. I am adding this eventhandler to my CheckBox in my pageLoad event, however, any page event I try seems to give me the same results:
CheckBox chbxLv1 = new CheckBox();
chbxLv1.ID = "DymanicallyCreatedIDForIdentification";
chbxLv1.AutoPostBack = true;
chbxLv1.CheckedChanged += new EventHandler(this.checkChanged);
/* Way lower in my code */
protected void checkChanged(object sender, EventArgs e)
{
//Some code goes here which never seems to execute... grrr
}
I thought that this may be a problem with the ViewState at first and did quite a bit of research on that. I'm now thinking I am doing something dumb with adding an event handler. I'm not sure why this event never fires, but I'm a little new at adding events to a control. Do I need a delegate here?
--Roman
In order for dynamically loaded controls to be handled properly during the ASP.NET Page Lifecycle, they need to be added to the page during OnInit (or prior to LoadViewState, really) otherwise their state information will not be maintained and you can, in fact, corrupt the viewstate depending on how/where things are added in the page's control graph.
I have some code that modifies a value that several controls in other update panels are bound to. When this event handler fires, I'd like it to force the other update panels to refresh as well, so they can rebind.
Is this possible?
Edit:
To clarify, I have an update panel in one user control, the other update panels are in other user controls, so they can't see each other unless I were to expose some custom properties and use findControl etc etc...
Edit Again:
Here is what I came up with:
public void Update()
{
recursiveUpdate(this);
}
private void recursiveUpdate(Control control)
{
foreach (Control c in control.Controls)
{
if (c is UpdatePanel)
{
((UpdatePanel)c).Update();
}
if (c.HasControls())
{
recursiveUpdate(c);
}
}
}
I had 3 main user controls that were full of update panels, these controls were visible to the main page, so I added an Update method there that called Update on those three.
In my triggering control, I just cast this.Page into the currentpage and called Update.
Edit:
AARRGGGG!
While the update panels refresh, it does not call Page_Load within the subcontrols in them...What do I do now!
What about registering a PostBackTrigger (instead of an AsyncPostBackTrigger) that will refresh every panel when a specific event fires.
Or add the trigger that already refreshes some UpdatePanels to the other UpdatePanels as well.
You can set triggers on the events in the update panel you want updated or you can explicitly say updatepanel.update() in the code behind.
This is a good technique if you want to refresh updatepanel from client side Javascript.
Page.DataBind() kicks off a round of databind on all child controls. That'll cause Asp.Net to re-evaluate bind expressions on each control. If that's insufficient, you can add whatever logic you want to make sure gets kicked off to an OnDataBinding or OnDataBound override in your usercontrols. If you need to re-execute the Page_Load event, for example, you can simply call it in your overridden OnDataBound method.
instantuate both view panels to a third presenter class, Then let the presenter class control both views. for example:
You could just pass over what you need the 'middle class' to do its job for example, in your main you could have;
PresenterClass.AttachInterface(mIOrder);
PresenterClass.DoSomeCalulation();
PresenterClass.drawPanel(1);
PresenterClass.AttachInterface(mIOtherOrder);
PresenterClass.DoSomeCalulation();
PresenterClass.drawPanel(2);
each view will have its own controls. So many differant ways you could do this.. alternitivly you could use the middle class to instantuate both your panels then in each of your panels you could have 'get methods' to retrive the data for processing.