Asp.NET User Control Postback Issue - c#

I have a aspx page that has a Tab control, a few buttons and a User Control on it.
<dx:TabPage Text="Worker Information" Name="tabWorker">
<ContentCollection>
<dx:ContentControl runat="server" SupportsDisabledAttribute="True">
<uc:WorkerInfo runat="server" OnWorkerLoaded="On_Worker_Loaded" OnWorkerUnloaded="On_Worker_Unloaded" />
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
There are 2 custom events on the user control
public event EventHandler WorkerLoaded;
public event EventHandler WorkerUnloaded;
I have the event handlers in the main aspx page
protected void On_Worker_Loaded(object sender, EventArgs e)
{
btnNext.Enabled = true;
}
protected void On_Worker_Unloaded(object sender, EventArgs e)
{
btnNext.Enabled = false;
}
The On_Worker_Loaded event fires without an issue. I can debug and watch the button become enabled. My problem is when the screen loads the button is still disabled. In my aspx page I disable the button:
<dx:ASPxButton ID="btnNext" runat="server" Text="Next" Theme="MetropolisBlue"
onclick="btnNext_Click" Enabled="false"></dx:ASPxButton>
The 'Next' button is already disabled. This is why you can't really see it. I have framed it with the black outline. When I click that OK button the WorkerLoaded Event fires.
The only other disable is on the On_Worker_Unloaded Event Handler. I don't have it hooked up yet. There is nothing else that causes the button to disable. Any Ideas what I'm missing?
Thanks!
UPDATE
The User Control has a UpdatePanel in it. I don't know if that makes a difference but I thought I would include that. Something else weird happened I noticed. After enabling the button I noticed that if I did a postback the button would be enabled. Does that ring any bells for anyone?

It seems the loaded event is being called before the Enabled=false property is set.
In your debugger check to see whether the Enabled property is already false when the event is called.
Also, what triggers the WorkerLoaded event?

In the end I found a solution that works for me. I put the Next button and the Previous buttons inside their own Update Panel. After I did this everything worked without issue. Thanks for all the ideas and help.

Related

ASP.NET Click event does not work for multi buttons what created in tab button control

I am new in ASP.NET and I have a problem..
In my code, btn.Click event does not work for multi buttons what created in tab button control. Can you guys advice me?
Thanks,
<asp:Button Text="TAB MENU" BorderStyle="None" ID="Tab" CssClass="Initial" runat="server" OnClick="Tab_Click" />
<asp:Panel ID="panel1" runat="server" Direction="LeftToRight" HorizontalAlign="Left"></asp:Panel>
protected void Tab_Click(object sender, EventArgs e) {
foreach (...) {
Button btn = new Button();
btn.Click += Button1_Click;
panel1.Controls.Add(btn);
}
}
protected void Button1_Click(object sender, EventArgs e) {
**some code here! but does not work.**
}
Try this
Button btn = new Button();
btn.Click += new RoutedEventHandler(Button1_Click);
panel1.Controls.Add(btn);
Your problem probably lies along the lines of the fact that the button does not exist when the Button1_Click callback is fired... Asp.net webforms is a somewhat leaky abstraction over HTTP, and therefore it's a bit tough to decipher sometimes, but I expect what is happening is that when the postback from your button click occurs, the button doesn't actually EXIST in the control tree, because that button only gets added to the control tree when a tab control is clicked. So because the button does not exist on the postback, webforms doesn't know what to do with the event, so it ignores it...
If your buttons must be created dynamically, consider making them do some javascript to edit some hidden field or something, the value of which you can inspect in the Page_Load method, and then do whatever you want to do functionality-wise with that value.
If they are NOT dynamic, and instead will be the same for each page load (but different for each tab - ie, when the page is loaded, you're drawing some info from a db to decide what buttons to display, but that will not change from postback to postback of the same page), then consider creating the buttons in the Page_Load event instead of inside the event that is raised when the tab is created.
ALTERNATIVELY, since in your comment you suggest that the buttons will be used to open a new window, why not just make the buttons do that client side, ie with javascript window.open commands. Then you don't even need to postback to the server at all...

Postback Issue With Objects In ASP.NET C#

I have some buttons on a web form in my ASP.NET project. The end user has to interact with these buttons like you would with a calculator. My issue is that ever time the end user clicks a button the page is refreshed because of postback. I tried to turn postback off like so:
if (!Page.IsPostBack)
{
protected void btn7_Click(object sender, EventArgs e)
{
const string stringNumber7 = "7";
var text = txtNumber.Text;
text += stringNumber7;
txtNumber.Text = text;
CheckMaxLength();
}
}
But then the click event will not fire. I have tried using images, image buttons, image maps. I can't get the result I want from those object either. I would like to be able to stop postback, but still have my button click event to fire like the would in a windows app. I have see some forums talking using a update panel. I am not sure how to implement an update panel correctly. I am not familiar with AJAX controls.
Does anyone know how I can accomplish want I want to do using an update panel? Or how to stop postback and still fire server side code?
If the botton is invoking local / client side JavaScript, return false within the control event attribute which will cancel the PostBack.
<button onclick="MyMethod();return false;">Click Me</button>
of if you are using a server side control:
<asp:button runat="server" OnClientClick="MyMethod();return false;" />

ASP.NET browser back firing button event

I have a problem where an event is firing unexpectedly when the user hits the back button on the browser. Here are the steps.
User clicks button (fires btnNewSurvey_Click)
Browser displays popup window
User closes popup and navigates to a second page from a separate link.
Browser displays second page
User clicks back
btnNewSurvey_Click is fired again (this is the problem)
I was wondering why this is occurring. I've looked into asp.net's page life cycle as well as turning off caching for the page but I cannot reach an explanation.
Let me know if anymore information is required. Thanks!
UPDATE: It seems like the button click event is only getting fired when the user clicks back from chrome
aspx
<asp:Button ID="btnNewSurvey" runat="server" Text="New Survey" OnClick="btnNewSurvey_Click" />
aspx.cs
protected void btnNewSurvey_Click(object sender, EventArgs e)
{
rwScreenList.VisibleOnPageLoad = true;
rwScreenList.Visible = true;
ListBox lbox = rwScreenList.ContentContainer.FindControl("lboxScreenSelection") as ListBox;
((Label)rwScreenList.ContentContainer.FindControl("lblScreenError")).Text = "";
lbox.DataSource = DatabaseFactory.GetScreensForProject(ProjectId);
lbox.DataBind();
}
The problem is that this event is being called when the user hits back from another page.
Problem seems to be in your rwScreenList.VisibleOnPageLoad = true;
As i saw this is telerik property and when you use this property you need to go through certain things.
You can do like this also:
rwScreenList.VisibleOnPageLoad = !Page.IsPostBack;
Please refer below discussion which will help you to resolve from the problem.
http://www.telerik.com/community/forums/aspnet-ajax/window/radwindow-reopening-after-closing-it-in-every-postback.aspx

Button Event Handler Not Firing on Homepage Without /default.aspx

I'm at a loss over here. On the root of my application (default.aspx), I have a basic ASP.NET Button control on the page with an event handler wired up via its onClick event.
Since it's the root, hitting either / or default.aspx will bring you to this page. When clicking the button, the event handler is only getting called if the FilePath is in the url (default.aspx).
To be more clear:
whatever.com/default.aspx <------ The event handler gets called when the button is clicked
whatever.com <------ event handler is not getting called when button is clicked.
I should note, this page doesn't have much on it. No jquery files included, no javascript, nothing besides a DropDownList and this Button. The Web.Config doesn't have much in it either, at least nothing I can see that would have any impact on this.
<asp:Button ID="btnSelection" OnClick="btnSelection_OnClick" runat="server" Text="Submit" />
protected void btnSelection_OnClick(object sender, EventArgs e)
{
throw new Exception("Test"); // Only reaches here if /default.aspx is in the url
}
This is probably something small I'm missing. Any guidance would be greatly appreciated.
Check out this link:
Event Handlers Might Not Be Not Raised in a Default Document in IIS 7 or IIS 7.5 Integrated Mode?
If nothing, it should point you in the right direction.

AjaxToolkit Tab Control as Button

I am currently building a tab control with multiple tab panels, and would like to have one of the tabs as a button instead of a template with content. The idea being that when the user clicks on the tab with the button, an event would fire and a C# method would run, and the page would post back.
How would I go about doing this? Preferably this could be something generated in the code behind as all the other tabs are programmatically generated along with the content.
You can handle TabContainer's ActiveTabChanged event. Therefore you need to set AutoPostBack for the TabContainer to true.
<asp:TabContainer ID="TabContainer1" runat="server" AutoPostBack="true" OnActiveTabChanged="ActiveTabChanged">
Codebehind:
protected void ActiveTabChanged(object sender, EventArgs e)
{
if (TabContainer1.ActiveTabIndex == 0)
{
// ...
}
// ...
}

Categories

Resources