How to prevent the page load twice when dynamically adding the button? - asp.net

I have dynamically created button in c#, button is bind to page on page load event of page
Now, when I press the button from them then page loading twice, it is like page refreshing
How can I stop that re-binding of control when I click button?
my code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {}
else
{
fillquotes();
}
}
fillquotes()
{
//dynamicaaly create the button
msg.OnClientClick="lnkmsg(" + i + ")";
}
btnMsg_Click(object sender, EventArgs e)
{
------
}
client side:
function lnkmsg(id)
{
__doPostBack('<%= btnMsg.UniqueID %>', "");
}
when msg button clicks
pageload
msg_click()
pageload
msg_click()

You can prevent it by setting condition for checking Request.Form["__EventTarget"]. Check the spelling and syntax for it. May this solve your problem.
Enjoy Coding!!!

Related

Browser back button lost session on asp.net

I'm clicking browser back button and then I try to go to any aspx page, I'm losting session on my Asp.net web project.
How can I solve this?
My master page load code like that;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["admin_id"] != null)
{
AdminName.Text = Session["admin_name"].ToString();
mybadge.Text = Session["my_badge"].ToString();
}
else
{
Response.Redirect("admin.aspx");
}
}
}
I think you are not touching/saving any data before navigating to another page. So that's why session gets blank when you press on back button.

Web Forms Response.RedirectToRoute and Ajax UpdatePanel?

I added a button in datalist in updatepanel (Web Forms/AJAX). Can we use Response.RedirectToRoute() in codebehind? I would like to send cat_id with it ofcourse if it's possible.
I can use Response.Redirect in button click :
protected void goto_Click(object sender, EventArgs e)
{
Response.Redirect("Page.aspx?cat_id=3", true); // working
}
I can't use Response.RedirectToRoute in button click:
protected void goto_Click(object sender, EventArgs e)
{
Response.RedirectToRoute("go", new { cat_id= 3 }); //not working
}
I was going to be crazy.. i saw it's visual studio's problem. Response.RedirectToRoute(); is working in UpdatePanel. However, i coudn't understand why it didn't work (because it's same code) for a length of time.
Cheers

ASP.NET Button on Placeholder and Placeholder in an User Control (ascx) file Button event not fired

I am dynamically creating link buttons on user control using a place holder and the eventhandler attached to the link button click+=new Event(Button_Click) is not firing
Thanks in Advance
Code Snippet of
protected override void OnInit(EventArgs e)
MenuListPlaceHolder.Controls.Add(new LiteralControl("<li>"));
ctrl.ID = this.UniqueID + (nCounter++).ToString();
ctrl.Text = cardType.Name;
ctrl.Click += new EventHandler(this.CardName_Click);
MenuListPlaceHolder.Controls.Add(ctrl);
MenuListPlaceHolder.Controls.Add(new LiteralControl("</li>"));
When clicked post back event is fired but not executing CardName_Click
You have to attach it on Page_Load event
like so:
protected void Page_Load(object sender, EventArgs e) {
this.Btn.OnClick+=new Event(Button_Click)
}
you should check it: here to learn about page cycle

Handle Button Click Event from User Control loaded dynamically

I have a blank user control (child) and during Page_Load i create some text boxes and a button. I also add an event to the button.
I then load that user control dynamically from a placeholder in another usercontrol (parent).
Both controls are rendered correctly but when i click on the button, the event isnt fired.
Any ideas on how to handle the event?
Child code:
protected void Page_Load(object sender, EventArgs e)
{
/*... other user controls ..*/
UpdateButton = new LinkButton();
UpdateButton.ID = "buttonid";
UpdateButton.Text = "text";
UpdateButton.Click += new EventHandler(UpdateButton_Click);
this.Controls.Add(UpdateButton);
}
protected override void Render(HtmlTextWriter writer)
{
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].RenderControl(writer);
}
}
public void UpdateButton_Click(object sender, EventArgs e)
{
//Do stuff
}
Parent code:
protected void Page_Load(object sender, EventArgs e)
{
placeholder.Controls.Clear();
ChildControl uc = (ChildControl)LoadControl("~/UserControls/ChildControl.ascx");
placeholder.Controls.Add(uc);
}
If i use the child control directy (ie without the parent the control) the click event is raised and handled nicely. But when i use the parent control, the debugger wont even hit a breakpoint inside UpdateButton_Click().
Thanks in advance.
I think I know what might be happening. In your parent Page_Load you are calling placeholder.Controls.Clear(); This does what you would imagine and clears the control, including any events that have occurred. What happens when remove this line? Do you get an additional one created on each postback?

Asp.net page - user control communication

I have a page Product.aspx,there I have a user control ProductDisplay.ascx which has been created by drag and drop.
Now when a button is clicked in ProductDisplay.ascx,I want a logging function to be called which is in Product.aspx.
To achieve this I have used delegates
on ProductDisplay.ascx
public delegate void LogUserActivity(ProductService objService);
public event LogUserActivity ActivityLog;
on Product.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ProductDisp.ActivityLog += new User_UserControl_ProductDisplayBox.LogUserActivity(LogProduct);
}
}
Now button click event of ProductDisplay.ascx
protected void imgBtnBuyNow_Click(object sender, ImageClickEventArgs e)
{
if (ActivityLog != null)
{
ActivityLog(Product);
}
Response.Redirect("~/User/ShoppingCart.aspx");
}
My problem is that whenever i click this button ActivityLog is null.Why is it null?
My idea is that once i click this button,page posts back and its previous state is lost.
Please help me out with a reason and solution.
Secondly,I want to do away with null checking
**if (ActivityLog != null)**
{
ActivityLog(Product);
}
I saw some code which instantiates a delegate with a default value the moment it is declared,but i was not able to find it.Please help.
I have found solution to first problem
if (!IsPostBack)
{
ProductDisp.ActivityLog += new User_UserControl_ProductDisplayBox.LogUserActivity(LogProduct);
}
This was causing the issue.Move this line
ProductDisp.ActivityLog += new User_UserControl_ProductDisplayBox.LogUserActivity(LogProduct);
out of if (!IsPostBack)

Resources