bat*_*adi 1 .net asp.net postback linkbutton pageload
如何查看页面中LinkButton单击的Page_Load内容.这是为了避免调用服务,以便它只执行其事件中存在的内容.
应该在你的运行中唯一应该是Page_Load你想要在每个请求中发生的代码,或者只有你想要在回发检查中包含的代码.例如:
protected void Page_Load(object sender, EventArgs e)
{
// Put the all code you need to run with EVERY request here
// Then add a post back check for any code you want to ONLY run on the very
// first request to the page.
if (!IsPostBack)
{
// Code you only want to run the first time. Usually setup code to initalize
// DropDownLists, Grids or pre populate a form, etc...
}
}
Run Code Online (Sandbox Code Playgroud)
您的LinkButton代码应该是单击处理程序中的所有代码:
protected void yourLinkButton_Click(object sender, EventArgs e)
{
// code you want to execute when your button is clicked. This will run AFTER
// the Page_Load has finished.
}
Run Code Online (Sandbox Code Playgroud)
现在,如果你LinkButton是在使用GridView,Repeater或某些类型的控制,需要结合来填补它,那么你可能会需要实现一个RowCommand事件处理程序,以找出哪些LinkButton被按下.您还可以将数据绑定到它的CommandArgument属性,以将一些唯一的行特定数据传递给偶数处理程序.
如果你有一堆LinkButtons都使用完全相同的处理程序,最糟糕的情况是强制转换sender然后比较ID值.
protected void yourLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
if (btn.ID.Equals("Some control name you want to compare"))
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
如果我对你的问题不以为然,只需发表评论,我会尝试将其整理出来.
编辑:根据你的评论听起来你必须知道Button它是Page_Load由于一些其他约束.好吧,没有干净的方式,Page_Load但它可以做到.您需要检查Request.Form密钥并检查特定的按钮名称(只有Button被点击的按钮才能包含在按键中).例如:
if (Request.Form.AllKeys.Contains("yourButton1"))
{
// then do something based on yourButton1
}
else if (Request.Form.AllKeys.Contains("yourButton2"))
{
// then do something based on yourButton2
}
// etc...
Run Code Online (Sandbox Code Playgroud)
我不认为有任何其他干净的方式绕过它.如果框架包含导致其中一个sender属性中的回发的控件,那将是很好的.
另一个编辑:这完全是我的想法.以上编辑是你需要做的,Button因为它没有填充__EVENTTARGET.由于您使用的是以后LinkButton可以使用以下内容来获取导致回发的控件:
string controlNameThatCausedPostBack = Request.Form["__EVENTTARGET"];
Run Code Online (Sandbox Code Playgroud)
我用a测试了LinkButton它,它确实按预期工作.
希望最终解决你的问题:)
| 归档时间: |
|
| 查看次数: |
7994 次 |
| 最近记录: |