如何验证在 Page_Load 上的 asp.net 中单击了哪个 LinkBut​​ton

发布于 2024-09-15 20:03:45 字数 101 浏览 6 评论 0原文

如何检查在页面的 Page_Load 中单击了哪个 LinkBut​​ton。 这是为了避免调用服务,以便它只执行其事件中存在的内容。

How do I check which LinkButton is clicked in the Page_Load of the page.
This is to avoid calls to service so that it only executes what is present in its event.

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

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

发布评论

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

评论(6

梦萦几度 2024-09-22 20:03:45

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...
    }
}

您的 LinkBut​​ton 代码应该完全独立于点击处理程序中:

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.
}

现在,如果您的 LinkBut​​tonGridView 中使用,Repeater 或某种需要绑定来填充它的控件,那么您可能需要实现一个 RowCommand 事件处理程序来确定哪个 LinkBut​​ton 是按下。您还可以将数据绑定到其 CommandArgument 属性,以将一些唯一的行特定数据传递给偶数处理程序。

如果您有一堆 LinkBut​​ton 都使用完全相同的处理程序,最坏的情况是强制转换 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
    }
}

如果我对你的问题偏离了主题,请发表评论,我会尽力解决。

编辑:根据您的评论,由于其他一些限制,您似乎必须知道它在 Page_Load 中的哪个 Button 。好吧,在 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...

我认为没有任何其他干净的方法可以解决它。如果框架在 sender 属性之一中包含导致回发的控件,那就太好了。

另一个编辑:这完全让我忘记了。您需要对 Button 执行上述编辑,因为它不会填充 __EVENTTARGET。由于您使用的是 LinkBut​​ton,您可以使用以下命令来获取导致回发的控件:

string controlNameThatCausedPostBack = Request.Form["__EVENTTARGET"];

我已经使用 LinkBut​​ton 对其进行了测试,它确实按预期工作。

希望最终能解决您的问题:)

The only thing that should be running in your Page_Load is the code you want to happen always with everyt request, OR have the code that you only want to run once wrapped in a post back check. For example:

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...
    }
}

Your LinkButton code should be all on it's own in a click handler:

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.
}

Now if your LinkButton is used in a GridView, Repeater or some type of control that requires binding to fill it in, then you will probably need to implement a RowCommand event handler to figure out which LinkButton is pressed. You can also bind data to it's CommandArgument property to pass some unique row specific data to the even handler.

If you have a bunch of LinkButtons all using the exact same handler, the worst case scenario is to cast the sender and then compare ID values.

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
    }
}

If I am way off the mark on your question, just leave a comment and I will try and sort it out.

EDIT: Based on your comment it sounds like you have to know which Button it is in the Page_Load due to some other constraints. Well there is no clean way of doing it in the Page_Load but it can be done. You will need to check the Request.Form keys and check for a specific button name (only the Button that was clicked should be included in the keys). For example:

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...

I don't think the is any other clean way to get around it. It would have been nice if the framework included the control that caused the postback in one of the sender properties.

Another Edit: This totally slipped my mind. The above edit it was you need to do for a Button since it does not populate __EVENTTARGET. Since you are using a LinkButton you can use the following to get what control caused the post back:

string controlNameThatCausedPostBack = Request.Form["__EVENTTARGET"];

I have tested this with a LinkButton and it does work as expected.

Hope that finally solves your problem :)

剧终人散尽 2024-09-22 20:03:45

我曾经不得不这样做,因为在事件处理程序中为时已晚。您可以使用 javascript 编写被单击到隐藏字段的按钮的 ID。您可以在 Page_Load 中检索它。如果我没记错的话,另一种方法是查看 _EVENTTARGET 隐藏字段,已经有一段时间了。

I used to have to do this, because in event handler it was too late. You could use javascript to write an Id of the button that was clicked into hidden field. Than you can retrieve it in the Page_Load. Other way would be to look into _EVENTTARGET hidden field if I remember correctly, it's been a while.

话少情深 2024-09-22 20:03:45
protected void Page_Load(object sender, EventArgs e)
{
  if (IsPostBack && Request.Form["__EVENTTARGET"] == LinkButton1.UniqueID) 
  {
    // LinkButton1 was clicked
  }
}
protected void Page_Load(object sender, EventArgs e)
{
  if (IsPostBack && Request.Form["__EVENTTARGET"] == LinkButton1.UniqueID) 
  {
    // LinkButton1 was clicked
  }
}
一紙繁鸢 2024-09-22 20:03:45

您的意思是您只是试图在页面上的其他回发事件被触发时阻止执行 Page_Load 代码吗?如果是这种情况,请将其包含在:(

if (!IsPostBack)

恕我直言,这是 WebForms 事件模型所需的一点破解,但事实就是如此。)

或者我误解了您的问题?

至于标识特定的 LinkBut​​ton,可以使用 CommandName 参数:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandname.aspx

Do you mean that you're just trying to keep the Page_Load code from being executed when other post-back events on the page are fired? If that's the case, wrap it in:

if (!IsPostBack)

(IMHO this is a bit of a hack necessitated by the WebForms event model, but it is what it is.)

Or am I misunderstanding your question?

As for identifying a specific LinkButton, you can use the CommandName parameter:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandname.aspx

吻风 2024-09-22 20:03:45

您还可以声明 LinkBut​​ton(或 Control,或其他)类型的变量,并让按钮在单击事件中使用对自身的引用来更新该变量,然后检查 Load 时的值。

You could also declare a variable of type LinkButton (or Control, or whatever) and have the button(s) update that with a reference to themselves in the click event, then check the value at Load.

小ぇ时光︴ 2024-09-22 20:03:45

单独的处理程序是此处的方法,但您可以为每个按钮提供唯一的 ItemCommand 属性,并根据需要过滤这些属性。

Separate handlers are the way to go here, but you could give each button a unique ItemCommand property, filter on those as necessary.

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