Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 具有多个链接选项的链接按钮_Asp.net - Fatal编程技术网

Asp.net 具有多个链接选项的链接按钮

Asp.net 具有多个链接选项的链接按钮,asp.net,Asp.net,我有今年和明年的两个文件,链接按钮必须根据年份指向不同的文件。但问题是我们想在新窗口中打开文件。所以我决定从服务器端调用javascript asp:linkbutton的代码 <asp:LinkButton ID="guide" runat="server" Text="XXX" OnClick="guide_click"> 代码隐藏 protected void guide_click(object sender, EventArgs e) {

我有今年和明年的两个文件,链接按钮必须根据年份指向不同的文件。但问题是我们想在新窗口中打开文件。所以我决定从服务器端调用javascript

asp:linkbutton的代码

<asp:LinkButton ID="guide" runat="server" Text="XXX" OnClick="guide_click">
代码隐藏

    protected void guide_click(object sender, EventArgs e)
    {
        if (Session["YearLastLicence"] != null)
        {
            if (int.Parse(Session["YearLastLicence"].ToString()) < DateTime.Now.Year)
            {
                ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:Guidelink2012();", true);
            }
            else ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:Guidelink2013();", true);

        }
    }
protectedvoid-guide\u单击(对象发送者,事件参数e)
{
如果(会话[“YearLastLicense”]!=null)
{
if(int.Parse(会话[“YearLastLicense”].ToString())

它在第一次打开页面时工作正常,但是如果我刷新页面,弹出窗口也会自动打开。有什么想法吗?谢谢

您应该在页面加载中编写代码,而不是在链接按钮的单击事件中编写代码

所以在页面加载时

 if (Session["YearLastLicence"] != null)
        {
            if (int.Parse(Session["YearLastLicence"].ToString()) < DateTime.Now.Year)
            {
               guide.Attrbuites.Add("onclick","javascript:Guidelink2012();return false;");
            }
            else {
guide.Attrbuites.Add("onclick" "javascript:Guidelink2013();return false;");

        }
}
if(会话[“YearLastLicense”]!=null)
{
if(int.Parse(会话[“YearLastLicense”].ToString())

您还可以从Linkbutton中删除click事件。

为什么不将pdf名称作为参数发送到javascript函数,而不是编写两个除了pdf名称几乎相同的函数?这将为您节省4或5行代码。应为“属性”。它可以工作,您能再解释一下为什么它在onclick事件中不能正常工作吗?在onclick事件中,您正在执行ClientScript.RegisterStartupScript,它注册了一个事件,以便在客户端页面加载期间运行。因此,只要单击链接按钮,它就会回发并运行脚本,但稍后回发将不会有此信息,也不会打开popoup。希望这是有意义的。也一定要把回答作为答案,这样其他人会发现它很有用!:)
 if (Session["YearLastLicence"] != null)
        {
            if (int.Parse(Session["YearLastLicence"].ToString()) < DateTime.Now.Year)
            {
               guide.Attrbuites.Add("onclick","javascript:Guidelink2012();return false;");
            }
            else {
guide.Attrbuites.Add("onclick" "javascript:Guidelink2013();return false;");

        }
}