母版页链接按钮不能正常工作

母版页链接按钮不能正常工作

本文介绍了母版页链接按钮不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的问题是,我在母版页上有一个链接按钮(lnk_logout)。在内容页面中,我在gridview模板字段中有另一个链接按钮(LinkBut​​ton1)。现在我想在linkbutton1上打开一个新窗口。我正在使用Javascript,它的工作正常。但之后,当我点击lnk_logout(母版页链接按钮)时,它也会打开新窗口。下面是我的来源和cs代码。



提前致谢。



我有什么尝试过:



大家好,
我的问题是,我在母版页上有一个链接按钮(lnk_logout) 。在内容页面中,我在gridview模板字段中有另一个链接按钮(LinkBut​​ton1)。现在我想在linkbutton1上打开一个新窗口。我正在使用Javascript,它的工作正常。但之后,当我点击lnk_logout(母版页链接按钮)时,它也会打开新窗口。下面是我的源代码和cs代码。

母版页的源代码:
<%@ Master语言=C#AutoEventWireup =trueCodeBehind =Site.master.csInherits =openNewWindow.Site% >

<!DOCTYPE html>
< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title>< / title>
< asp:ContentPlaceHolder ID =headrunat =server>
< / asp:ContentPlaceHolder>
< / head>
< body>
< form id =form1runat =server>
< div>
< div style =background-color:red; width:100%; height:100px;>
< h2>测试< / h2>
< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverOnClick =LinkBut​​ton1_Click> Logout< / asp:LinkBut​​ton> &NBSP;
< / div>
< div>
< / div>
< div>
< asp:ContentPlaceHolder ID =ContentPlaceHolder1runat =server>
< / asp:ContentPlaceHolder>
< / div>
< / div>
< / form>
< / body>

cs主页代码

protected void LinkBut​​ton1_Click(object sender,EventArgs e)
{
Session.Abandon();
Session.RemoveAll();
Response.Redirect(logout.aspx);
}

源代码内容页面

<%@ Page Title =Language =C#MasterPageFile =〜/ Site.MasterAutoEventWireup =trueCodeBehind =index.aspx.csInherits =openNewWindow.index%>
< asp:Content ID =Content1ContentPlaceHolderID =headrunat =server>
< script type =text / javascript>
函数SetTarget(){
document.forms [0] .target =_ blank;
}
< / script>
< / asp:Content>
< asp:Content ID =Content2ContentPlaceHolderID =ContentPlaceHolder1runat =server>
< asp:ScriptManager ID =ScriptManager1runat =server>< / asp:ScriptManager>
< asp:UpdatePanel ID =UpdatePanel1runat =server>
< ContentTemplate>
< asp:按钮ID =btn_fillgridrunat =serverText =填充网格OnClick =btn_fillgrid_Click/>
< br />
< br />
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseOnRowDataBound =GridView1_RowDataBound>
< Columns>
< asp:TemplateField HeaderText =Title>
< ItemTemplate>
< asp:LinkBut​​ton ID =LinkBut​​ton1runat =serverText ='<%#Eval(title)%>'OnClientClick =SetTarget();的OnClick = LinkBut​​ton1_Click >< / ASP:LinkBut​​ton的>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =Image>
< ItemTemplate>
< asp:Label ID =Label1runat =serverText ='<%#Eval(image)%>'>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>
< / ContentTemplate>
<触发器>
< asp:PostBackTrigger ControlID =btn_fillgrid/>
< /触发器>
< / asp:UpdatePanel>
< br />
< / asp:Content>

CS内容代码页$

protected void LinkBut​​ton1_Click(object sender,EventArgs e)
{
try
{
LinkBut​​ton btnSelect =(LinkBut​​ton)sender;
GridViewRow Grow =(GridViewRow)btnSelect.NamingContainer;
Label lblDocumentName =(Label)Grow.FindControl(Label1);

Response.Redirect(home.aspx?image =+ lblDocumentName.Text);
}
catch(Exception ex)
{Response.Write(ex.Message.ToString()); }
}


private void FillGrid()
{
try
{
DataRow dr;
DataTable dt = new DataTable();
dt.Columns.Add(title);
dt.Columns.Add(image);

dr = dt.NewRow();
dr [title] =Page1;
dr [image] =Page1;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr [title] =Page2;
dr [image] =Page2;
dt.Rows.Add(dr);

GridView1.DataSource = dt;
GridView1.DataBind();

GridView2.DataSource = dt;
GridView2.DataBind();
}
catch(exception ex)
{
Response.Write(ex.Message.ToString());
}
}

protected void btn_fillgrid_Click(object sender,EventArgs e)
{
FillGrid();
}

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
try
{
if(e.Row.RowType = = DataControlRowType.DataRow)
{
控制lnkBut​​ton = e.Row.FindControl(LinkBut​​ton1);
ScriptManager mgr = ScriptManager.GetCurrent(this.Page);
mgr.RegisterPostBackControl(lnkBut​​ton);
}
}
catch(Exception ex)
{Response.Write(ex.Message.ToString()); }
}
解决方案

Hi,
My problem is, I have a link button(lnk_logout) on master page.In content page i have another link button (LinkButton1) in gridview template field. Now I want to open a new window on linkbutton1 click. I am using Javascript and its working fine. But after that when i click on lnk_logout(master page link button) it also open new window. below is my source and cs code.

Thanks in advance.

What I have tried:

Hi All,
 My problem is, I have a link button(lnk_logout) on master page.In content page i have another link button (LinkButton1) in gridview template field. Now I want to open a new window on linkbutton1 click. I am using Javascript and its working fine. But after that when i click on lnk_logout(master page link button) it also open new window. below is my source and cs code.

Source code of master page:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="openNewWindow.Site" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div style="background-color: red; width: 100%; height: 100px;">
                <h2>Testing</h2>
                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Logout</asp:LinkButton>  
            </div>
            <div>
            </div>
            <div>
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
            </div>
        </div>
    </form>
</body>

cs code of master page

protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.RemoveAll();
            Response.Redirect("logout.aspx");
        }

Source Code of Content Page

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="openNewWindow.index" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
        function SetTarget() {
            document.forms[0].target = "_blank";
        }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Button ID="btn_fillgrid" runat="server" Text="Fill Grid" OnClick="btn_fillgrid_Click" />
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="Title">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("title") %>' OnClientClick="SetTarget();" OnClick="LinkButton1_Click"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("image") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="btn_fillgrid" />
        </Triggers>
    </asp:UpdatePanel>
    <br />
    </asp:Content>

CS Code of Content Page

protected void LinkButton1_Click(object sender, EventArgs e)
        {
            try
            {
                LinkButton btnSelect = (LinkButton)sender;
                GridViewRow Grow = (GridViewRow)btnSelect.NamingContainer;
                Label lblDocumentName = (Label)Grow.FindControl("Label1");

                Response.Redirect("home.aspx?image=" + lblDocumentName.Text);
            }
            catch(Exception ex)
            { Response.Write(ex.Message.ToString()); }
        }


        private void FillGrid()
        {
            try
            {
                DataRow dr;
                DataTable dt = new DataTable();
                dt.Columns.Add("title");
                dt.Columns.Add("image");

                dr = dt.NewRow();
                dr["title"] = "Page1";
                dr["image"] = "Page1";
                dt.Rows.Add(dr);

                dr = dt.NewRow();
                dr["title"] = "Page2";
                dr["image"] = "Page2";
                dt.Rows.Add(dr);

                GridView1.DataSource = dt;
                GridView1.DataBind();

                GridView2.DataSource = dt;
                GridView2.DataBind();
            }
            catch(Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

        protected void btn_fillgrid_Click(object sender, EventArgs e)
        {
            FillGrid();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if(e.Row.RowType == DataControlRowType.DataRow )
                {
                    Control lnkButton = e.Row.FindControl("LinkButton1");
                    ScriptManager mgr = ScriptManager.GetCurrent(this.Page);
                    mgr.RegisterPostBackControl(lnkButton);
                }
            }
            catch(Exception ex)
            { Response.Write(ex.Message.ToString()); }
        }
解决方案


这篇关于母版页链接按钮不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:58