检查从不同的页面调用sql脚本

本文关键字:调用 sql 脚本 检查 | 更新日期: 2023-09-27 17:49:20

我有两个页面-第一页有两个按钮,点击它们中的任何一个将运行不同的SQL查询并将它们转移到GridView中的第二页。在页面加载时,我有两个不同的IF语句:第一个语句将运行第一个查询,第二个语句将运行第二个查询。我的问题是——我如何检查这个按钮是否被点击了?

下面是我的代码示例:

public partial class Page2: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (page1 button1 is clicked) //this is what I need help with
        {
            //run sql...
        }
        else if (page1 button2 is clicked) //this will be similar to the first
        {
            //run sql...
        }
    }
}

检查从不同的页面调用sql脚本

您可以使用会话状态来存储被单击的按钮。在button事件中,输入

protected void button1_OnClikc(object sender, EventArgs e)
{
    session["WhichButtonWasClicked"] = "Button1";
}

在下一页的页面加载中,您可以读取该会话值。

If (Session["WhichButtonWasClicked"] == "Button1")
{
 // Button 1 is clicked
}
else
{
//Button 2
}

另一个选项是将按钮单击信息作为URL参数传递。您还可以将信息存储在cookie中,然后将其存储在DB中,稍后再检索它。有很多选择!

你可以使用SESSIONS,当你的按钮被按下时,你应该分配一个值给Session变量,例如:

protected void button1_Click(object sender, EventArgs e){
    Session["buttonPressed"] = "button1";
    // your query and transfer code here
}
protected void button2_Click(object sender, EventArgs e){
    Session["buttonPressed"] = "button2";
    // your query and transfer code here
}

会话变量将在整个页面中保持该值,因此如果您想要

,则使用开关。
protected void Page_Load(object sender, EventArgs e)
{
    switch (Session["buttonPressed"].ToString())
    {
        case "button1":
        //Your code if the button1 was pressed
        break;
        case "button2":
        //Your code if the button2 was pressed    
        break;
        default: 
        //Your code if you have a default action/code
        break;
    }
}