I have an ASP.NET page with an update panel.
Normally, if I use an update panel, there should be postbacks only but no complete page reloads.
In my case, when I click a button, it always makes an page reload. Although this button is in an update panel.
What I found out: When I put
Response.End();
in the Button_Click Method, there is no reload.
My code on the .aspx:
<asp:UpdatePanel ID="upUserDefault" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="btnPrintSelectedArticles" />
</Triggers>
<ContentTemplate>
<cc1:MenuButton runat="server" ID="btnPrintSelectedArticles" OnClientClick="return ShowPrintStickersPopUp();" ButtonText="Print" />
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
Page Load:
btnPrintSelectedArticles.MenuButton_Click += btnPrintSelectedArticles_MenuButton_Click;
Method:
void btnPrintSelectedArticles_MenuButton_Click(object sender, EventArgs e)
{
// Disables reload
Response.End();
}
MenuButton is a user control
Any ideas how to disable the reload of the complete page?
Edit:
This is the ShowPrintStickersPopUp JS from the ClienClick. It simply checks a gridview if there are any checkboxes checked
function ShowPrintStickersPopUp() {
var gridView = document.getElementById('<% =gvArticles.ClientID %>');
//get all the control of the type INPUT in the base control.
var inputs = gridView.getElementsByTagName("input");
for (var n = 0; n < inputs.length; ++n) {
if (inputs[n].type == 'checkbox' && inputs[n].checked) {
// If at least 1 checkbox is checked, open popup
window.open('PrintStickersPopUp.aspx', 'Print Stickers', 'width=700,height=550');
return true;
}
}
return false;
}
Your client method looks good. Did you remember to add a ScriptManager object on your page? A lot of issues relating to UpdatePanels are related to not having one added.
Related
I have an ajax tabcontainer in an updatepanel with all tabpages set visible until you want to add a tabpanel based on the dropdownlist selected value
CODE:
<cc1:TabContainer ID="tabControlParameters" runat="server" CssClass="ajax__tab_xp"
ScrollBars="Both" ActiveTabIndex="15" UseVerticalStripPlacement="True">
<%--EnvironmentTab --%>
<cc1:TabPanel ID="pnlEnvironment" HeaderText="Environment" runat="server" Visible="false">
<ContentTemplate>
//somecontent
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="pnlDatabase" HeaderText="Environment" runat="server" Visible="false">
<ContentTemplate>
//somecontent
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="pnlFirstError" HeaderText="Environment" runat="server" Visible="false">
<ContentTemplate>
//somecontent
</ContentTemplate>
</cc1:TabPanel>
With a button add which is inside the Updatepanel and has a correct async trigger assigned to it.
From C# codebehind I've made a loop to check if the dropdownlist selectedvalue = panel_headertext if so make it visible
CODE:
protected void btnAddParameters_Click(object sender, EventArgs e)
{
String Parameter = ddlParameterTypes.SelectedValue.ToString();
AjaxControlToolkit.TabContainer container = (AjaxControlToolkit.TabContainer)tabControlParameters;
foreach (object obj in container.Controls)
{
if (obj is AjaxControlToolkit.TabPanel)
{
AjaxControlToolkit.TabPanel tabPanel = (AjaxControlToolkit.TabPanel)obj;
if (tabPanel.HeaderText == ddlParameterTypes.SelectedValue)
{
tabPanel.Visible = true;
tabPanel = tabControlParameters.ActiveTab;
container.ActiveTab = tabPanel;
}
}
}
}
Now this works perfectly if the updatepanel trigger is set to fullPostback but it's set to async postback then it only works on the first click even though the event is fired every time I'm clicking on the button. Am I missing something obvious here?
Petar
You have the same value in HeaderText for each of your TabPanels. I think it'll work if you correct the HeaderText attributes.
I have a repeater whose ItemTemplate contains a PlaceHolder that I add input controls (ListBox, TextBox, CalendarExtender etc.) to on ItemDataBound:
<asp:UpdatePanel ID="ReportParameterUpdatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Repeater ID="ReportParameterEditRepeater" OnItemDataBound="ReportParameterEditRepeater_ItemDataBound" runat="server">
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ParameterEntryPlaceholder"></asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
How can I generate an asyncpostback from one of these TextBoxes inside the repeater (on TextChanged)?
The control is created dynamically and I only want to create the postback under certain conditions, so it needs to be done from code behind.
I have tried:
OnItemCommand (but this appears to be just for buttons)
ScriptManager.RegisterAsyncPostBackControl (doesn't seem to do anything on TextChanged)
UpdatePanel.Triggers.Add(new AsyncPostBackTrigger ...) (unable to find the TextBox as it is inside the repeater)
In ReportParameterEditRepeater_ItemDataBound you will need to assign a unique ID to each control, and then bind the text changed event. I then like to store those in session. Then add it to your placeholder.
Below is how I did it in my site for a button click event:
TemplateControls_Headline ctrl = (TemplateControls_Headline)LoadControl("~/Controls/TemplateHeadline.ascx");
ctrl.ID = "MyCtrl_" + CMSSession.Current.AddedTemplateControls.Count;
ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);
MySession.Current.AddedTemplateControls.Add((Control)ctrl);
PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;
ph.Controls.Add(ctrl);
Then, in your OnInit of the page, you have to re-bind everything from the viewstate since you are creating them dynamically, this is where the unique id you created comes in (this is mainly for postbacks):
protected override void OnInit(EventArgs e)
{
PlaceHolder ph = accAddTemplates.FindControl("phAddTemplateControlsArea") as PlaceHolder;
int counter = 0;
foreach (UserControl ctrl in MySession.Current.AddedTemplateControls)
{
ctrl.ID = "MyCtrl_" + counter;
ctrl.Remove.CommandArgument = counter.ToString();
ctrl.Remove.Click += new EventHandler(RemoveItem_OnClick);
counter++;
ph.Controls.Add(ctrl);
}
base.OnInit(e);
}
I am not able to visible my button on another button click event.
.aspx
<asp:Button ID="btnActivate" runat="server" SkinID="skinLoginButton"
Text="Activate" ToolTip="Activate" CausesValidation="true"
ValidationGroup="UserAuthentication" onclick="btnActivate_Click" />
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
.cs
#region btnActivate_Click
protected void btnActivate_Click(object sender, EventArgs e)
{
this.btnhomepage.Visible = true;
}
#endregion
I use this.btnhomepage.Visible = true; in .cs file.
what's wrong in my code or declearation?
<asp:Button ID="btnhomepage" Visible="false" runat="server"
Text="Goto Homepage" CssClass="cssLoginButton" onclick="btnhomepage_Click"/>
when using visible attribute in the mark-up you are forcing your control to be visible=false and stay false forever. asp.net engine render asp.net controls into html control in asp.net page life cycle at Render stage. even you had changed the control property in any code behind event
Solution: Don't use makup attribute when setting control behaviour dynamicllay
page life cycle link:
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx
http://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle
Remove the visible property from the btnhomepage button and make it invisible from Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.btnhomepage.Visible = false;
}
}
Try this
btnhomepage.Visible = true;
btnhomepage.Enabled = true;
btnhomepage.Style.Add("display", "block");
I have a custom usercontrol that has some Asp.net code. I would like to write the same code but with C#.
The problem is that i don't know how to put a repeater and some buttons into the ContentTemplate.
The Asp.net Code :
<asp:UpdatePanel runat="server" ID="up">
<ContentTemplate>
<n2:Repeater ID="rpt" runat="server">
<ItemTemplate></ItemTemplate>
</n2:Repeater>
<asp:LinkButton runat="server" ID="btnFirst"
Visible="false" Enabled="false" Text="<<" OnClick="btnFirst_Click" />
</ContentTemplate>
</asp:UpdatePanel>
So how could I write this chunk in C# code? To be precise, how could I insert the repeater and the Linkbutton into the ContentTemplate.
Note : I don't want to use the LoadTemplate to do it.
Edit
I have tried ContentTemplateContainer.Controls.Add():
private UpdatePanel up = new UpdatePanel();
private Repeater rpt = Repeater();;
public Paging{
//Add repeater to updatePanel
up.ContentTemplateContainer.Controls.Add(rpt);
AsyncPostBackTrigger apb3 = new AsyncPostBackTrigger();
apb3.ControlID = "btnFirst";
apb3.EventName = "Click";
//Add Triggers to updatePanel
up.Triggers.Add(apb1);
//Create buttons
btnFirst = new LinkButton();
btnFirst.Visible = false;
btnFirst.Enabled = false;
btnFirst.Text = "<<";
btnFirst.Click += new EventHandler(btnFirst_Click);
//Add buttons to update panel
up.ContentTemplateContainer.Controls.Add(btnFirst);
}
protected void Page_Load(object sender, EventArgs e)
{
rpt.ItemTemplate = LoadTemplate("~/UI/Templates/NewsEvent.ascx");
....
}
I have an error caused by the first line on Page_Load :
Databinding methods such as Eval(), XPath(), and Bind() can only be used in controls contained in a page.
This is NewsEvent.ascx:
<img src='<%# Eval("ImageThumbnail") %>' alt="" />
you cant do this with ITemplate types ... i had a similar problem trying to clone a tabpanel in a tabcontainer control ... i already had a hidden tabpanel and all i wanted to do was create a new tabpanel and basically instantiate the ITemplate from the hidden one in the new one.
The problem is ITemplate ... it's not very dynamic for code behind interactions i would suggest putting that markup on the page as you already have and setting visible = false on the parent then when you need to databind and show the hidden panel.
getting the initial bind to work isn't the problem ... its the postback handling ...
Ajax TabContainerTabPanels Break postbacks
I have an image button on a pop up page which is opened by another page
<asp:ImageButton
ID="Button_kalem_islemikaydet"
runat="server"
CausesValidation="False"
ImageUrl="~/images/butonlar/buyuk/Kaydet.jpg"
meta:resourcekey="Button_kalem_islemikaydetResource1"
OnClick="Button_ust_islemikaydet_Click"
OnClientClick="f2()"
Width="100" />
f2() is
<script type="text/javascript">
function f2() {
opener.document.getElementById("TextBox1").value = "hello world";
opener.document.getElementById("HiddenField1").value = "hello world";
window.opener.location.href = window.opener.location.href;
}
</script>
And Button_ust_islemikaydet_Click is another method implemented in aspx.cs file and it updates the database tables which are shown in the parent page in a GridView.
What I am trying to do is to doPostBack I mean refresh the opener(parent) page.And with these above codes refresh is working.However, parent page still shows the same data before the refresh.And the reason is that OnClientClick works before OnClick method
So my question is that is there any way I can run the method on OnClick and finish it and then run the OnClientClick method?
<form id="aspnetForm" runat="server">
<asp:Button Text="Click Me" ID="ClickMeButton" OnClick="ClickMeButton_OnClick" runat="server" />
<asp:HiddenField runat="server" ID="UpdateOpenerHiddenField" Value="false" />
<script type="text/javascript">
//1st approach
var updateOpenerField = window.document.getElementById("<%= UpdateOpenerHiddenField.ClientID %>");
if (updateOpenerField.value === "true") {
f2();
updateOpenerField.value = "false";
}
// for the 2nd approach just do nothing
function f2() {
alert("Hello, opener!");
}
</script>
</form>
protected void ClickMeButton_OnClick(object sender, EventArgs e)
{
//1st approach
UpdateOpenerHiddenField.Value = "true";
// 2nd approach
ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", "f2();", true);
}
No, you can't run server side code (OnClick event handler) before client side. OnCLientClick event was added to perform some validation before post back. There is only one way to do it - update the f2 method and post data on server via ajax
You can put your javascript inside a PlaceHolder tag which you make visible in your server-side OnClick handler.
aspx code:
<asp:PlaceHolder id="refreshScript" visible="false" runat="server">
window.opener.location.href = window.opener.location.href;
window.close();
</asp:PlaceHolder
cs code:
protected void button_Click(Object sender, EventArgs e) {
// do whatever
refreshScript.Visible = true;
}