update panel in master page and asyncpostbacktrigger in content page - asp.net

I have a cart in master page that this cart is inside update panel.
In my content page I have a listview that in this listview exist linkbutton for add product to my cart.
I want to add product to cart using these linkbuttons without refresh, and cart is updated.
I have this code in my content page:
protected void Page_Init(object sender, EventArgs e)
{
UpdatePanel up = (this.Master.Master.FindControl("UpCart")) as UpdatePanel;
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = lvNewProducts.UniqueID;
trigger.EventName = "ItemCommand";
up.Triggers.Add(trigger);
}
But my page is refreshed when I click in link button.
Please help me!

Do you mean you want a partial post back and you are getting a full post back?
I think you can't add AsyncPostbackTriggers to an update panel from a different naming container. You did try with lvNewProducts.UniqueID to set ControlID, which is normally set with the ID. So that's a hint that this might be the case.
What you can do is calling RegisterAsyncPostBackControl on the ScriptManager.
Assuming your ScriptManager is in your master page and it's ID is myScriptManager you have todo the following:
protected void Page_Init(object sender, EventArgs e)
{
ScriptManager myScriptManager = (this.Master.Master.FindControl("myScriptManager")) as ScriptManager;
if (scriptManager != null) {
scriptManager.RegisterAsyncPostBackControl(lvNewProducts);
}
}
That replaced your code and should result in a partial post back. The link to your update panel to get updated is still missing, so you should do the following to get it updated in the ItemCommand event handler (e.g. if you set OnItemCommand="ListViewClicked":
protected void ListViewClicked(object sender, EventArgs args) {
//Do whatever necessary
//Now update your update panel
UpdatePanel up = (this.Master.Master.FindControl("UpCart")) as UpdatePanel;
if (up != null) {
up.Update();
}
}

Related

Load a dynamic custom user control added during page execution (button click event)

I'm trying to add a custom control to a panel (containing a "form" to input data) during a button click event and I want to access it's methods like .Validate() after this data has been inputted. But when I try to do so the ctrl comes with null value.
Here's part of my code :
protected void btnNext2_Click(object sender, EventArgs e)
{
...
ctrlCompliance = (Compliance)LoadControl("../../ascx/SRM/Compliance.ascx");
ctrlCompliance.ReadOnly = false;
pnlCompliance.Controls.Add(ctrlCompliance);
...
}
protected void btnNext3_Click(object sender, EventArgs e)
{
...
ctrlCompliance = pnlCompliance.Controls[0] as Compliance;
ctrlCompliance.Validate() <- this is allways null
...
}
I cannot use Page_Init as most of the solutions propose and really need to load it only during that button click. Did someone had the same problem as me?
You should load it anyway using Page_Init, but set its visible property to False. Then using javascript or code-behind you can change its visible property to True when you need to show the print button.
So move:
ctrlCompliance = (Compliance)LoadControl("../../ascx/SRM/Compliance.ascx");
ctrlCompliance.ReadOnly = false;
pnlCompliance.Controls.Add(ctrlCompliance);
to Page_Init

Posted data in gridview where I was added control dynamically

I added controls (i.e. template column) dynamically in grid view and click on button (post back) then grid view populated existing data (i.e.posted data) twice separated by
protected void Page_Init(object sender, EventArgs e)
{
//on init recreated controls
// if (IsPostBack)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
}
Sounds like you might be using Auto-generated fields. In design view, click the smart tag on the gridview and click "edit columns." Then uncheck the checkbox that says "Auto-generate fields." I think this should fix your problem if I am understanding you correctly.

clear textbox after postback

I have a textbox and a button on the ascx page.
After entering text i click on the button where it will post to the database and clear the textbox.
After positing, if i refresh the web page it is going to the button click method and posting the old text to the database. how can i clear the textbox value from the view state.
i have set the enableviewstate of the textbox to false. But still it is not working. Please let me know. Thanks
protected void Button_Click(object sender, EventArgs e)
{
var txt=textBox1.Text;
textBox1.Text="";//Set it to empty
// do other stuff
............
............
}
protected void btnClear_Click(object sender, EventArgs e)
{
ClearControls();
}
private void ClearControls()
{
foreach (Control c in Page.Controls)
{
foreach (Control ctrl in c.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Text = string.Empty;
}
}
}
}
After you save your data with the click button you can set the textbox.text = ""
EDIT: After your comment that textbox.text = "" is not working....
When you hit the refresh it sounds like it is resubmitting your work again. Try just reloading the page by browsing to your page again.
Also be sure to check that you aren't saving your data on every postback but just on the button click event.
Do you have any code in your page load event?
Long shot here, but this worked in my case:
TextBox.Attributes.Remove("value");
This was on a textbox with the text mode sent to 'Password'.
just add this at the end of the event after page i going o be refresh
Response.Redirect("Registration.aspx");
here my WebForm name is "Registration.aspx" instead of this put your WebForm name.

Link Button on ASP.NET user control not firing

I have a user control, which is added to another user control. The nested user control is built up of a GridView, an image button and a link button. The nested user control is added to the outer control as a collection object based upon the results bound to the GridView.
The problem that I have is that my link button doesn't work. I click on it and the event doesn't fire. Even adding a break point was not reached. As the nested user control is added a number of times, I have set image button to have unique ids and also the link button. Whilst image button works correctly with its JavaScript. The link button needs to fire an event in the code behind, but despite all my efforts, I can't make it work. I am adding the link button to the control dynamically. Below is the relevant code that I am using:
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
public event EventHandler ViewAllClicked;
protected override void CreateChildControls( )
{
base.CreateChildControls( );
string strUniqueID = lnkShowAllCust.UniqueID;
strUniqueID = strUniqueID.Replace('$','_');
this.lnkShowAllCust.ID = strUniqueID;
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
}
I have been stuggling with this problem for the last 3 days and have had no success with it, and I really do need some help.
Can anyone please help me?
My LinkButton wasn't firing it's Click event, and the reason was I had its CausesValidation property set to True. If you don't want the link to validate the form, be sure to set this to False.
Try adding your click event to the linkbutton tag:
<asp:LinkButton runat="server" OnClick="linkShowAllCust_Click" />
Or adding it to your Page_Load:
Page_Load(object sender, EventArgs e)
{
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
Is the usercontrol within the gridview? If so register the event handler on the gridview's onrowcreated event.
It appears that you have a viewstate issue. Because the control isn't there when the viewstate is loaded the application doesn't know how to hook up the event to be fired. Here is how to work around this.
You can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I had the same issue. I had viewstate="false" on the page I was adding the control to. (on the aspx page)

Refresh problem when treeview node changed

I have a Treeview in a masterpage and a products page in child page.
When i click treeview node i want to bind data to a gridview on the product page.
protected void trvCategoryTab_SelectedNodeChanged(object sender, EventArgs e)
{
if (trvCategoryTab.SelectedNode.Value != string.Empty)
{
Response.Redirect("~/Customer/Products.aspx?Search=" + trvCategoryTab.SelectedNode.Value);
}
}
It's working fine, but the problem is that the page reloads every time. I want to prevent this.
Is there any other method to redirect to the child page?
It sounds like you want to use AJAX.

Resources