Registering a user control always postback hen the form loaded - asp.net

Hi all I have created a user control and registered it in web.config with some extension and calling it on the page where ever required. When I use that when ever the page get loaded the user control also getting loaded, i mean the post back event of the user control or the page load event occurs as per the operations made in web page.
Is there any way to avoid this in such a way that only the user control page load should enable or load when ever some operations performed on that control when loaded.
For example I have created a user control and registered on my form, this will get loaded on button click of the form, I would like to make the post back of user control only when user click on the button of the web form but not on every operations performed
Edit as per jadarnel27 answer
<%# Register Src="~/UserControl1.ascx" TagName="TimeoutControl" TagPrefix="uc1" %>
<asp:Panel ID="yourPanelControl" runat="server" Height="200px" Width="300px">
</asp:Panel>
<dx:ASPxButton ID="btn" Text="user" OnClick="btn_Click" runat="server">
</dx:ASPxButton>
protected void btn_Click(object sender, EventArgs e)
{
TimeoutControl tc = new TimeoutControl();
yourPanelControl.Controls.Add(tc);
}
But I am unable to see the postback event fires on page load of user control as per ur code

When the ASP.NET Page loads, it recursively calls the Load function of all the controls in the Page.Controls collection (see the "Life Cycle events" section of this MSDN article on The ASP.NET Page Life Cylce, specifically the "Load" event). Including the UserControl in the markup of your page will cause it to be part of this collection, so the load event will fire whenever the Page loads.
If you want to avoid this, you need to add the UserControl to the Page dynamically in response to the Button's click event. You could, for instance, include some kind of Panel in your Page, then add the UserControl to that Panel when you click the Button.
Something like this:
protected void yourButton_Click(Object sender, EventArgs e)
{
// Create your UserControl
yourUserControl uc = new yourUserControl();
// Add it to the Panel you included in your markup
yourPanelControl.Controls.Add(uc);
}
The markup for a Panel would be something like this:
<asp:Panel id="yourPanelControl" runat="server" Height="200px" Width="300px">
</asp:Panel>

Related

Acess a control of a user control in a content page of a master page

I have user control (ascx) in a Master Page.
Then I have a content page (aspx) that uses that Master Page.
Now, in the content page, I want to access a hiddenfield control that is placed in the user control.
How do I do that?
I tried the following but it returns null.
Master.FindControl("MyHiddenField")
Master.FindControl("MyUserControl1_MyHiddenField")
Thanks
In your content page (.aspx) place this code. It will make the Master Page strongly typed for the content page.
<%-- The Page directive goes here --%>
<%# MasterType TypeName="MyMasterClassName" %>
<%-- Rest of your code below.. --%>
In the Master Page code behind, place this code
public UserControlTypeName MyUserControl1
{
get {return myUserControl1; }
set {}
}
This makes the User Control instance public. You'll need to rename the ID of the user control to myUserControl1.
Then in your UserControlTypeName (or whatever the name of your class is for your User Control) you can make the inner controls accessible in the same manner we did on the master page.
public HiddenField
{
get {return myHiddenField;}
set {}
}
Obviously, rename the ID for MyHiddenField to myHiddenField to avoid a conflict.
Finally, this allows your content to access the controls within the user control that are on the master page through strong typing.
public void Page_Load(object sender, EventArgs e)
{
Master.MyUserControl1.MyHiddenField.Value="Hello, world!";
}
For further reference, see Working with ASP.NET Master Pages programmatically on MSDN.
If Microsoft Is Listening...
If for some reason a Microsoft ASP.NET developer is reading this answer, please consider doing something like this:
<asp:Button runat="server" scope="public" id="MyButton1 />
That would make it much easier than having to create wrapper properties to make inner controls publicly accessible.

How to submit data twice on an ASP.NET page

I have a webpage. At the top is a search bar that is inside a <form id="form1" runat="server">.
I also want to add a form on that same page that would allow users to register their details. Problem, ASP.NET only allows one form per page.
How can I achieve my goal? Any workarounds?
You can only have one server side form on a page.
If it is an option, you can have a client side form (without runat="server") with a separate action - this POST can go to a different page, where you will have to accessRequest.Form` to retrieve the values posted.
Another option is to use separate buttons for posting with different event handlers.
You can use the simple HTML form approach but there is the problem of always post the whole page back. or use mvc:
Are multiple forms on a single page supported in asp.net 3.5?
Put the code you want to be called in different button click events. Therefore, if the search button is clicked, only the code in the search buttons click event is run. If the register button is clicked, only the code in the registers click event is run.
Here is an example:
protected void Page_Load(object sender, EventArgs e) {
// Common code
}
protected void btnSearch_Click(object sender, EventArgs e) {
// Search code
}
protected void btnRegister_Click(object sender, EventArgs e) {
// Register code
}
Double clicking the buttons in the designer will create the click events in the code behind.
As already stated in the answer by #Simon, it's easy to have multiple click handlers in your code-behind to process exactly what you need on the page - this is the easiest way to solve the "lack of multiple forms" issue.
Something very useful in this situation is the DefaultButton attribute of the <asp:Panel> control. This means that should you have multiple areas of your page with (for instance) <asp:TextBox> controls, and each of those areas has a specific <asp:Button> associated, if the focus is in one of the textboxes then pressing Return or Enter will result in the DefaultButton being clicked.
For example...
<asp:Panel runat="server" DefaultButton="btnSearch">
Search: <asp:TextBox runat="server" id="txtSearcn"/>
<asp:Button runat="server" id="btnSearch" Text="Search" OnClick="btnSearch_Click"/>
</asp:Panel>
See MSDN for more information on the DefaultButton attribute of <asp:Panel>

ASP.NET: load a User Control (containing UpdatePanel) at Page_Load dynamically for a later rendering

I have a page (with a repater) that loads user controls which contains update panels. I want to load the user controls dynamically when the repeater binds the items (i.e. at ItemDataBound event).
The user controls visually load okay, but the UpdatePanels does not get updated when they are triggered (I can see async postbacks happen for the other UpdatePanels). This is because the script manager manages the update panel info between Load and PreRender of a page, and the repeater's ItemDataBound event happens after that (this is from this answer). So, when I load the user controls at Page_Load or Page_Init, the async postback works fine.
The problem is, I won't know where to insert the user controls until repeater's ItemDataBound event. So, is there any way to resolve this problem? Can I load the user controls at Page_Load and have the script manager register the update panels in the control, and then place the controls at the ItemDataBound event?
So, the code goes like this:
Page:
<asp:Repeater runat="server" ID="repeater" onitemdatabound="repeater_ItemDataBound">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="plhContent" />
</ItemTemplate>
</asp:Repeater>
Page code-behind:
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// dynamically insert the user control in the placeholder
var uc = LoadControl("~/UserControl.ascx");
placeHolder.Controls.Add(uc);
}

Submit form to a non default page

I solved this problem in another instance by making some workaround but I want to get it clear this time as I understand my problem better.
My asp.net net page has a search functionality that searches the database based on 5 fields. The result is displayed in the gridview. The gridview is editable and I can update values. The problem is if I want to update multiple values, gridview won't allow it. So I included an extra column for checkbox. I have added a footer which has link to update all checked records.
Ok so here is the problem? How do I send the whole gridview to another page where I can capture the gridview values?
By default the page is submitted onto itself. If I change the default action page, the whole gridview and search, nothing will work.
So how do I submit the whole page (or part of it) to a different page other than the default action script?
I have not tried this specifically with a gridview, but I think a Cross Page PostBack should work. I have a search feature on my website as well and this is what I use.
Set the PostBackUrl of the Button that is causing the PostBack to the Destination page.
Let’s assume your source page is search.aspx and your destination page is SearchResult.aspx
Inside search.aspx:
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="right"
ValidationGroup = "Search"
PostBackUrl="~/SearchResult.aspx"
onclick="btnSearch_Click"/>
The form will be posted to SearchResult.aspx. Inside SearchResult.aspx, you add this directive:
<%# PreviousPageType VirtualPath="~/Search.aspx" %>
And in the code behind, you can acess any control like this:
PreviousPage.<mycontrol>
Hope this help.
Have you tried using the Session? Just add the DataSource, or the Gridview itself to the Session and load it in the other Page and then Dispose() it.
Good luck!
UPDATE:
I have accomplished this in the past through Cross-Page Posting. This is how I did it now for testing purposes:
Default.aspx:
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" PostBackUrl="SearchResult.aspx" />
SearchResult.aspx.cs:
protected void Page_Init(object sender, EventArgs e)
{
if (PreviousPage != null)
{
if (Page.PreviousPage.FindControl("GridView1") != null)
{
GridView grid = (GridView)Page.PreviousPage.FindControl("GridView1");
grid.ID = "myGrid";
this.form1.Controls.Add(grid);
}
}
}
Hopefully this helps.

Cannot add event handlers to ASP. net page

In visual studio 2008 when I drag and drop asp controlXXX on aspx page. Property page for this control, does not show event handlers button. It can reappear after switching to designer view but then disappears again. Screenshot attached.
Yes, that's annoying, but it works probably only in designer.
But you can still add event handlers manually
in html add onclick property and write name of the method
<asp:ImageButton ID="btnAdd" runat="server" onclick="btnAdd_Click" />
and in code-behind add method with two parameters of types: (object, EventArgs) like this:
protected void btnAddTag_Click(object sender, ImageClickEventArgs e)
{
}
or you can also add event handler in Page_Init method
btnAdd.Click+=new ImageClickEventHandler(btnAdd_Click);
(this will also generate automatically the method, only after += press TAB twice)

Resources