调用 page_load asp.net 上的button_click
我有一个搜索文本框和一个搜索按钮,单击时会显示一个包含以下名称和性别的网格。但是,我已将页面重定向到编辑时的另一个页面。现在,当我从该页面返回到包含 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();
}
现在我能够保存搜索,这样问题就解决了,但是当我在更改文本后单击按钮搜索,它会将我带回旧的搜索。原因可能是因为会话未清除,但我也通过处理 textchanged 和 selectedindexchanged事件。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要尝试从
Page_Load
调用按钮点击处理程序,而是将按钮点击处理程序更改为简单地调用另一个方法,例如:然后将所有
btnSearch_Click()
代码移至RunSearch()
然后在
Page_Load
中,您可以执行以下操作:顺便说一下,我建议您查看一下
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:Then move all your
btnSearch_Click()
code intoRunSearch()
Then in your
Page_Load
you can do something like:On a side note, I would recommend taking a look into
SQLCommand Parameters
. Your code is prone toSQL Injection Attacks
:http://en.wikipedia.org/wiki/SQL_injection
您应该重置会话重定向变量,以便它不会出现相同的情况。
You should reset the session redirected variable so it doesn't fall in the same case.
当页面返回主页时,您可以使用 QueryString 参数,然后在这里您可以检查 QueryString 参数是否存在。在这里您可以实现绑定网格的代码
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
您可以创建一个函数,该函数将从button_click 和page_load 调用。
You can create a function, which will called both from the button_click and page_load.