[英]How to call button's click event from click of linkbutton of another page without post back in c# ASP.NET
我在masterpage
有一個LinkButton
,點擊了LinkButton,我重定向到了Page1.aspx
。 在Page1.aspx上,我有一個button1
。 點擊那個按鈕button1
,我打開新窗口,不影響Page1.aspx
數據。
但是當我點擊LinkButton
,重定向到Page1.aspx
並從后面的代碼中,單擊button1
, Page1.aspx
的數據會被更改。
如何防止這種情況。 我提供我的代碼。
主頁上的LinkButton:
<asp:LinkButton ID="lnkAppointMent" runat="server" OnClick="lnkAppointMent_Click"><span>Appointment Scheduler </span></asp:LinkButton>
單擊LinkButton的事件:
protected void lnkAppointMent_Click(object sender, EventArgs e)
{
Session["PhoneCenter"] = "Appointment";
Response.Redirect("PhoneMessage.aspx");
}
PageLoad重定向頁面(PhoneMessage.aspx):
protected void Page_Load(object sender, EventArgs e)
{
fillCustomTypeMessages();
if (!Page.IsPostBack)
{
.
.
.
else if (Session["PhoneCenter"].ToString() == "Appointment")
{
btnScheduleAppointments_Click(btnScheduleAppointments, null);
}
.
.
.
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
上的RaisPostBack方法:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
try
{
base.RaisePostBackEvent(source, eventArgument);
}
catch (Exception ex)
{
}
.
.
PS:當我點擊btnScheduleAppointment
, source
將Schedule Appointments
,如果我點擊lnkAppointMent
, source
將<span>Appointment Scheduler </span>
即使我打電話btnScheduleAppointment
上的點擊lnkAppointMent
。
點擊按鈕事件:
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);
}
}
腳本:
function openNewWin(url)
{
alert(url);
var open_link = window.open('', '_blank');
open_link.location = url;
}
需要任何澄清。 請評論。
我花了一些時間來模擬你的問題。 您是否嘗試在加載Page1.aspx后修改LinkButton href或屬性?
我假設您的Page1.aspx是您的PhoneMessage.aspx,所以當您從母版頁單擊lnkAppointMent時,它會轉到“PhoneMessage.aspx”並執行該方法,然后如果再次單擊它,它將只發回而不是它之前做了什么,基本上,你需要再次重定向到“PhoneMessage.aspx”。
那你為什么不修改這樣的部分:
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);
}
這將使鏈接再次重定向到Page1.aspx,並在沒有回發的情況下執行相同的操作。 它不是最優雅的解決方案,但它可以完成您的工作。 只要確保有Session [“PhoneCenter”] =“約會”;
所以這不是你問題的答案,但我認為是你問題的解決方案。
它對我有用,我希望它有所幫助! 讓我知道你的想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.