How to trigger a button click event with a click on a link on another page without sending a message in C # ASP.NET

When I click on this top green button, the page data remains as it is. And a new window will open for another function

After clicking on the link, this is the left side.'AppointMentScheduler ', my data will be hidden

I have a LinkButton in the masterpage and when I click LinkButton I redirect, say, Page1.aspx . On page1.aspx, I have button1 . When I click this button1 I open a new window without affecting the Page1.aspx data.

But when I click on the LinkButton main page, redirecting to Page1.aspx and from the code behind, the data of button1 , Page1.aspx .

How to prevent this. I provide my code.

LinkButton on the main page:

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

Click LinkButton Event:

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

PageLoad redirect 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 on PhoneMessage.aspx:

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

ps: when I click btnScheduleAppointment , the source will be Schedule Appointments , and if I lnkAppointMent , the source will be <span>Appointment Scheduler </span> , even if I call btnScheduleAppointment when I click lnkAppointMent .

Click the button event:

 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.

+5
source share
1 answer

It took me a while to model your problem. Have you tried changing the LinkButton href attribute or attributes after loading Page1.aspx ?

I assume that your Page1.aspx is your PhoneMessage.aspx, so when you click lnkAppointMent from the main page, it goes to "PhoneMessage.aspx" and executes this method, and then if you click it again, it will only send back instead of doing what used to be, basically, you need to redirect to "PhoneMessage.aspx" again.

So why don't you change this part as follows:

  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); } 

This will cause the link to redirect to page 1.aspx again and do the same without postback. This is not the most elegant solution, but it does the job in your case. Just make sure you have Session ["PhoneCenter"] = "Destination";

So this is not the answer to your question, but I think this is the solution to your problem.

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

enter image description here

+2
source

All Articles