调用 page_load asp.net 上的button_click

发布于 2024-11-28 18:13:47 字数 1682 浏览 1 评论 0原文

我有一个搜索文本框和一个搜索按钮,单击时会显示一个包含以下名称和性别的网格。但是,我已将页面重定向到编辑时的另一个页面。现在,当我从该页面返回到包含 gridview 的页面时,我想要再次显示相同的搜索。我已成功将检索到的信息存储到会话中,但我无法调用我的 btn_click 事件@ page_Load。

这是一个片段:

编辑:我在代码中做了一些更改

protected void Page_Load(object sender, EventArgs e)
{
   if (Session["Redirected"] != null)
    {
        if (Session["FirstName"] != null)
            txtSearch.Text = Session["FirstName"].ToString();
        if (Session["Gender"] != null)
            ddlGen.SelectedValue = Session["Gender"].ToString();
        btnSearch_Click(sender, e);
    }

    if (!Page.IsPostBack)
    {
        BindGrid();
    }
}

,这是点击事件:

protected void btnSearch_Click(object sender, EventArgs e)
{
    string query = "Select EmployeeId,FirstName,Password,Address,sex,Deptno,act_book,actTV,DOJ,isActiveYN from employees where 1=1";

    if (txtSearch.Text != "")
    {
        query += " and FirstName like '%" + txtSearch.Text + "%'";
        Session["FirstName"] = txtSearch.Text;
    }
    if (ddlGen.SelectedValue != "")
    {
        query += "  and sex='" + ddlGen.SelectedValue.ToUpper() + "'";
        Session["Gender"] = ddlGen.SelectedValue;
    }


    DataSet ds = new DataSet("Employees");
    SqlConnection con = new SqlConnection("Password=admin;User ID=admin;Initial Catalog=asptest;Data Source=dbsvr");
    SqlDataAdapter da = new SqlDataAdapter(query, con);            


    da.Fill(ds);
    gvSession.DataSource = ds;
    gvSession.DataBind();


}

现在我能够保存搜索,这样问题就解决了,但是当我在更改文本后单击按钮搜索,它会将我带回旧的搜索。原因可能是因为会话未清除,但我也通过处理 textchangedselectedindexchanged事件。

I have a search textbox and a search button, when clicked displays a grid with the following names and gender.However I have redirected the page to another page on edit.Now When I comeback from that page to the page containing the gridview I want to display the same search again. I have successfully put retrieved the information but storing it into session, but I'm not able to call my btn_click event @ page_Load.

Here's a snippet:

EDIT: I have made some changes in my code

protected void Page_Load(object sender, EventArgs e)
{
   if (Session["Redirected"] != null)
    {
        if (Session["FirstName"] != null)
            txtSearch.Text = Session["FirstName"].ToString();
        if (Session["Gender"] != null)
            ddlGen.SelectedValue = Session["Gender"].ToString();
        btnSearch_Click(sender, e);
    }

    if (!Page.IsPostBack)
    {
        BindGrid();
    }
}

and here's the click event:

protected void btnSearch_Click(object sender, EventArgs e)
{
    string query = "Select EmployeeId,FirstName,Password,Address,sex,Deptno,act_book,actTV,DOJ,isActiveYN from employees where 1=1";

    if (txtSearch.Text != "")
    {
        query += " and FirstName like '%" + txtSearch.Text + "%'";
        Session["FirstName"] = txtSearch.Text;
    }
    if (ddlGen.SelectedValue != "")
    {
        query += "  and sex='" + ddlGen.SelectedValue.ToUpper() + "'";
        Session["Gender"] = ddlGen.SelectedValue;
    }


    DataSet ds = new DataSet("Employees");
    SqlConnection con = new SqlConnection("Password=admin;User ID=admin;Initial Catalog=asptest;Data Source=dbsvr");
    SqlDataAdapter da = new SqlDataAdapter(query, con);            


    da.Fill(ds);
    gvSession.DataSource = ds;
    gvSession.DataBind();


}

Now I'm able to save search, so that problem is resolved ,but another has poped up that when I click the button search after changin text it takes me back to the older search..The reason is probably because sessions are not cleared,but I did that as well by handling textchanged and selectedindexchanged eventd.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴随着你 2024-12-05 18:13:47

不要尝试从 Page_Load 调用按钮点击处理程序,而是将按钮点击处理程序更改为简单地调用另一个方法,例如:

protected void btnSearch_Click(object sender, EventArgs e)
{
     RunSearch();
}

然后将所有 btnSearch_Click() 代码移至 RunSearch()

然后在 Page_Load 中,您可以执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Gender"] != null && Session["FirstName"] != null)
    {
        txtSearch.Text = Session["FirstName"].ToString();
        ddlGen.SelectedValue = Session["Gender"].ToString();
        RunSearch();
    }

    if (!Page.IsPostBack)
    {
        BindGrid();

    }
}

顺便说一下,我建议您查看一下 SQLCommand 参数。您的代码容易受到SQL注入攻击

http://en.wikipedia.org /wiki/SQL_注入

Rather than trying to call your button click handler from the Page_Load, change your button click handler to simply call another method like:

protected void btnSearch_Click(object sender, EventArgs e)
{
     RunSearch();
}

Then move all your btnSearch_Click() code into RunSearch()

Then in your Page_Load you can do something like:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["Gender"] != null && Session["FirstName"] != null)
    {
        txtSearch.Text = Session["FirstName"].ToString();
        ddlGen.SelectedValue = Session["Gender"].ToString();
        RunSearch();
    }

    if (!Page.IsPostBack)
    {
        BindGrid();

    }
}

On a side note, I would recommend taking a look into SQLCommand Parameters. Your code is prone to SQL Injection Attacks:

http://en.wikipedia.org/wiki/SQL_injection

情仇皆在手 2024-12-05 18:13:47

您应该重置会话重定向变量,以便它不会出现相同的情况。

protected void Page_Load(object sender, EventArgs e)
{
   if (Session["Redirected"] != null)
   {
        Session["Redirected"] = null;
        ....

You should reset the session redirected variable so it doesn't fall in the same case.

protected void Page_Load(object sender, EventArgs e)
{
   if (Session["Redirected"] != null)
   {
        Session["Redirected"] = null;
        ....
坠似风落 2024-12-05 18:13:47

当页面返回主页时,您可以使用 QueryString 参数,然后在这里您可以检查 QueryString 参数是否存在。在这里您可以实现绑定网格的代码

if (Request.QueryString["Back"]!= null)
{
    // Your bind grid function
}

You can do using an QueryString paremeter when page return back to main page then here you can check QueryString paremeter is exist. here you can implement code for bind grid

if (Request.QueryString["Back"]!= null)
{
    // Your bind grid function
}
表情可笑 2024-12-05 18:13:47

您可以创建一个函数,该函数将从button_click 和page_load 调用。

You can create a function, which will called both from the button_click and page_load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文