Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call button's click event from click of linkbutton of another page without post back in c# ASP.NET

When I click on that right top green button, page data will stay as it is.And new window will open for another functionality

After clicking on linkbutton,that is left side.`AppointMentScheduler', my data will flush out

I have a LinkButton in masterpage and on click of LinkButton , I am redirecting to, say, Page1.aspx . On Page1.aspx , I have a button1. On click of that button1, I am opening new window, not affecting data of the Page1.aspx.

But when I click on LinkButton of masterpage, redirecting to Page1.aspx and from code behind,clicking button1 , Page1.aspx 's data gets changed.

How to prevent this. I am providing my code.

LinkButton on Masterpage :

<asp:LinkButton ID="lnkAppointMent" runat="server" OnClick="lnkAppointMent_Click"><span>Appointment Scheduler </span></asp:LinkButton>

click Event of LinkButton :

protected void lnkAppointMent_Click(object sender, EventArgs e)
        {
            Session["PhoneCenter"] = "Appointment";
            Response.Redirect("PhoneMessage.aspx");
        }

PageLoad of redirecting page(PhoneMessage.aspx) :

    protected void Page_Load(object sender, EventArgs e)
                {
                    fillCustomTypeMessages();            
                    if (!Page.IsPostBack)
                    {
                        .

    .

    .
                        else if (Session["PhoneCenter"].ToString() == "Appointment")
                        {
                            btnScheduleAppointments_Click(btnScheduleAppointments, null);
                        }

.

.

.

Button on PhoneMessage.aspx :

<div style="float: right; padding-right: 120px">
                                <asp:Button ID="btnScheduleAppointments" runat="server" OnClick="btnScheduleAppointments_Click"
                                    CssClass="button" Text="Schedule Appointments" ToolTip="Open appointment scheduler" />
                            </div>

RaisPostBack method on PhoneMessage.aspx :

protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
        {
            try
            {
                base.RaisePostBackEvent(source, eventArgument);
            }
            catch (Exception ex)
            {


            }

.

.

p.s : when I click on btnScheduleAppointment , source will be Schedule Appointments and if I click on lnkAppointMent , source will be <span>Appointment Scheduler </span> even if I am calling btnScheduleAppointment on click of lnkAppointMent.

Click event of button :

protected void btnScheduleAppointments_Click(object sender, EventArgs e)
        {
            if (!Permissions.checkPermissions(Session["employeeloggedin"].ToString(), "PHMSGVMD"))
            {
                ScriptManager.RegisterStartupScript(this, Page.GetType(), "OnLoad", "alert('You must have the Phone Messages: View and Modify permission to schedule appointments!')", true);
            }
            else
            {
                string script = String.Format("openNewWin('" + "phonescheduler.aspx" + "')");
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "openNewWin", script, true);
            }

        }

Script :

function openNewWin(url)
        {
            alert(url);
            var open_link = window.open('', '_blank');
            open_link.location = url;
        }

Any clarification needed. Please comment.

like image 373
Dinav Ahire Avatar asked Oct 31 '22 22:10

Dinav Ahire


1 Answers

It took me some time to simulate your problem. Have you tried to modify the LinkButton href or atributes after the Page1.aspx has been loaded?

I am assuming your Page1.aspx is your PhoneMessage.aspx, so when you click lnkAppointMent from the master page, it goes to "PhoneMessage.aspx" and execute the method, then if you click it again it will only post back instead of do what it did before, basically, you you need to redirect to "PhoneMessage.aspx" again.

So why dont you modify the part like this:

                    else if (Session["PhoneCenter"].ToString() == "Appointment")
                    {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "NoExec", String.Format("document.getElementById('lnkAppointMent').href=window.location.href;"), true);
                btnScheduleAppointments_Click(btnScheduleAppointments, null);
                    }

That would make the link redirect to the Page1.aspx again, and do the same without postback. It is not the most elegant solution, but it does the job in your case. Just make sure to have Session["PhoneCenter"] = "Appointment";

So it is not the answer to your question but I think is a solution to your problem.

It worked for me, I hope It helps! Let me know what you think.

enter image description here

like image 51
Rolando Retana Avatar answered Nov 14 '22 07:11

Rolando Retana