I have a custom popup panel in ASP.Net defined in the markup of a custom control (a context menu) and want to move it to a different placeholder which is placed in the parent (hosting) page.
The following code works, at least for the correct rendering.
protected override void OnInit(EventArgs e)
{
if (this.NamingContainer.NamingContainer.NamingContainer.FindControl("placeholder") is PlaceHolder placeholder)
{
placeholder.Controls.Add(popupPanel);
}
base.OnInit(e);
}
I now have the problem that events inside the popupPanel like OnClick() won't work. What do I have to do to get the event firing working?
Related
Asp button click does not fire. I wrote Response.Write("404.aspx"); in to button click event in codebehind. But it does not fire. Here is the page with a fire problem button:
please click here
thanks.
Here is the code behind:
protected void bntTest_Click(object sender, EventArgs e)
{
Response.Redirect("404.aspx");
}
protected void btn2_Click(object sender, EventArgs e)
{
Response.Redirect("404.aspx");
}
It does not work because your submit button is not inside the form tag so it won't be able to post the submitted data and the same time, the click event will not fire as the page does not post, you can see below:
EDIT:
You have added a nested form tag that you can not because form tag can not be nested within another form tag. So just remove the nested form tag and it will work.
Try to restructure your project using these guidelines:
Only add form elements to aspx pages
Add main content to MasterPage from pages
Add any content that needs to be nested within a form to a UserControl that is placed within a page.
Hi and thanks in advance.
I have an issue where I show some links at the top of page (they are not breadcrumbs) and for different circumstances I need to turn some on/off and for the page the user is on highlight that one. When I originally put the page together the call to the function that displays the links was in the Page_Init like so in the master page:
protected new void Page_Init(object sender, EventArgs e)
{
SetTabs(session);
}
protected void SetTabs(session)
{
makeTab.add(session.param, session.param);
makeTab.generate();
}
protected void add(param, param)
{
Tabs.add(new tab() { link = param, name = param });
}
protected void generate()
{
foreach(tab t in Tabs)
{
liTab.Text = string.Format("<li>{1}", t.name, t.link);
}
}
The links that get created are an unordered list of items displayed inline.
And I have a separate method that is applying a style change to highlight the link that is associated with the page that the user is on that is called from the Page_Load of the page itself:
protected void Page_Load(object sender, EventArgs e)
{
Master.makeTabBlue(nameOfTab);
}
However, in order to make hidden tabs reappear correctly, I had to move SetTabs(session) to the Page_PreRender function so that they would be called after the page was saved, when session is updated but now that I have done that the function that makes the tabs blue no longer has an effect. I've read about the page life cycle and I think that the CSS that's rendered in makeTabBlue is only effective during the Page_PreInit or Page_Init cycles but if I move the makeTabBlue to either of those then the page starts throwing additional errors that the controls can't be found which makes sense because they aren't called until PreRender. And calling it in the save function has no effect either. Where should I be calling makeTabBlue from so that it works correctly?
UPDATE:
In this particular situation, the resolution to my issue was to leave the SetTabs in the Page_Init, leave makeTabBlue in the Page_Load and I added a unique class to the LI and I am hiding, showing the links via javascript by storing values in a hidden field and checking that field when the js loads.
The last time you can do something with the page is on the Page_LoadComplete() (the one before PreRender) method. Changes to the markup will still take effect if you do it here. This should allow you to run some code in your init then run stuff later on whilst still affecting the front end.
Another option would be to add a variable in the code behind:
protected string TabColour = "some default colour";
Then on the front end, do something like:
<div style='color:#<%= TabColour %>'></div>
Where the div example above is your tab.
This would allow you to set TabColour on the prerender event but still show the correct colour on the front end as it's evaluated in the Rendering event. I prefer this method as page life cycle can be tricky and this avoids being constrained by it.
I have a user control inside an aspx page, after the aspx page loads, when I click some button inside that user control, I want the focus to come back to user control after that button click action is complete and the aspx page loads again.
You need to have an event in your user control that will allow the .aspx page to subscribe to that event so that it can set focus to the element in the user control after the form posts back, like this:
public class UserControlClass
{
// Define event that will be raised by user control to anyone interested in handling the event
public event UC_Button1ClickEventHandler UC_Button1Click;
public delegate void UC_Button1ClickEventHandler();
// Mechanism to allow event to be raised by user control
private void Button1_Click(System.Object sender, System.EventArgs e)
{
if (UC_Button1Click != null)
{
UC_Button1Click();
}
}
}
Now in your .aspx page, you need to subscribe to the event in the user control and say what method will actually handle the event, like this:
userControl1.UC_Button1Click += Button1_Click;
Finally, the click event handler needs to exist, like this:
public void Button1_Click(object sender, EventArgs args)
{
// Set focus here to user control element, text box for example
((TextBox)userControl.FindControl("TextBox1")).Focus();
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.Net Changes the element IDs
I have the following problem (I'll explain it simplified because otherwise it would get too complicated/long).
I need to create an ASP.net server control that is inherited from Panel. Simple enough. When the custom control renders, it should dynamically create a Button, associate an event handler to it (which will also be defined inside the server control) and add this button to the rendered control. The click event handler defined inside the server control does some job which for the moment isn't interesting.
I already coded an example and that works fine. In the constructor of the server control I create a new button control, give it an ID, associate an event handler, on the OnInit of the server control I add the button to the Panel controls (remember, my control inherits from Panel) and then everything gets rendered. This looks something like the following:
public class MyCustomControl: Panel
{
private Button myButton;
public MyCustomControl()
{
myButton = new Button();
myButton.ID = "btnTest";
myButton.Click += new EventHandler(btnTest_Click);
}
protected void btnTest_Click(object sender, EventArgs e)
{
//do something...
}
//...
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Controls.AddAt(0, myButton);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//...
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
protected override void Render(HtmlTextWriter output)
{
base.RenderContents(output);
}
}
This works quite fine. The button gets rendered correctly and when I click on it, the appropriate handler inside this customer server control is invoked and executed. Now the problem however starts when I would like to add multiple instances of this server control to my page because all of my generated buttons will have the same id "btnTest" which results in a conflict. So what I tried is to associate a random number to the id of the button, s.t. it end up being "btnTest1235512" or something similar. This solves my problem with the multiple IDs, but results in the problem that my event handler when clicking on the button is no more called correctly. I guess this is due to the problem that my button always gets another id when entering in the constructor and so the appropriate callback (btnTest_Click event handler) isn't found.
Can someone give me a suggestion how I could handle the problem. Is there some way for remembering the ID of the button and re-associating it. As far as I know however this has to happen in the OnInit, where the ViewState isn't yet available. So storing the id in the ViewState wouldn't work.
Any suggestions??
Implementing INamingContainer should fix the problem. You can then keep naming all your buttons btnTest.
This is just a marker interface so you don't have to implement any methods or properties.
Inherit from INamingContainer like:
public class MyCustomControl : Panel, INamingContainer
{
}
This is a marker interface and requires no implementation. I just verified with your code and it solves your problem.
Implementing INamingContainer will do it for you. It will automatically name the buttons with unique ID's. Just a note, if you need to use any of those buttons in any JavaScript, you'll need to use the ClientID property:
function getButton() {
var myButton = document.getElementById('<%=btnTest.ClientID %>');
}
I am using an ASP.NET AJAX-Enabled Web application (ASP.NET 2.0 and AJAX Toolkit 1.0)
that contains one button and 2 UpdatePanels (UpdatePanel_1 and UpdatePanel_2)
The button is registered with RegisterAsyncPostBackControl in the ScriptManager object
UpdatePanel_1 is in "Conditional" update mode and contains a TextBox.
UpdatePanel_2 is in "Always" update mode and contains another TextBox
When the button is pressed its handler calls UpdatePanel_1.Update() that updates the value of the TextBox based on a randomly selected value in a list;
Also the UpdatePanel_2's TextBox is being updated automatically , also without page refresh
Based on the value of a boolean ViewState variable I would also like to hide/show the UpdatePanels alternatively but
I get the error :
"Sys.InvalidOperationException: COuld not find UpdatePanel with ID 'UpdatePanel_2' (or UpdatePanel_1).
If it is being updated dynamically then it must be inside another UpdatePanel"
How can it be done without adding extra wrapping UpdatePanels?
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(Button1);
if (!IsPostBack)
{
Visibility = true;
}
UpdatePanel_1.Visible = !Visibility;
UpdatePanel_2.Visible = Visibility;
Visibility = !Visibility;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Panel1.Visible)
UpdatePanel_1.Update();
}
protected bool Visibility
{
get
{
return (bool)(ViewState["Visibility"] ?? true);
}
set
{
ViewState["Visibility"] = value;
}
}
The problem is that invisible controls aren't rendered to the client. So then trying to make them visible isn't going to work because as far as the client is concerned, they don't exist.
Try using style="display:none", or use different CSS classes and styles for visible and invisible panels, rather than setting visible=false;
You can invisible, or visible controls is child of updatepanel, not invisible, visible updatepanel, I try use updatemode = conditional but error, and then I visible controls add to updatepanel. Hopy help you
Thanks everybody post