简体   繁体   English

C#(对象发送者,GridViewCommandEventArgs e)如何为第二部分做些过分的准备?

[英]C# (object sender, GridViewCommandEventArgs e) How can I overgive something for the 2nd part?

I got the following Code: 我得到以下代码:

protected void Name_RowCommand(object sender, GridViewCommandEventArgs e)

{
    if (e.CommandName == "TextINeed")
    {
        //Some Code
    }

    if (e.CommandName == "SomethingElse")
    {
       //Some Code
    }
}

protected void btn_someButton(object sender, EventArgs e)
{
    Name_RowCommand(sender, "TextINeed");
}

The Problem i have is with the Name_RowCommand(sender, "TextINeed"); Name_RowCommand(sender, "TextINeed");的问题是Name_RowCommand(sender, "TextINeed"); I know that (sender, should be right, but how do i make the 2nd part right? I tried a few things but nothing worked so far. 我知道(sender,应该是正确的,但是我如何使第二部分正确?我尝试了一些操作,但到目前为止没有任何效果。

I'm happy about any advice! 我很高兴收到任何建议!

I assume this is ASP.NET due to the GridViewCommandEventArgs . 由于GridViewCommandEventArgs我认为这是ASP.NET。 You can't pass arguments to events directly. 您不能将参数直接传递给事件。 Events are triggered. 事件被触发。 In this case you have to register this event handler, for example on aspx: 在这种情况下,您必须注册此事件处理程序,例如在aspx上:

<asp:gridview id="GridView1" 
  onrowcommand="Name_RowCommand"
  runat="server">

  <columns>
    <asp:buttonfield buttontype="Link" 
      commandname="TextINeed" 
      text="SomeText"/>
   .....
  </columns>

</asp:gridview>

If the user now clicks on the link-button the CommandName will be TextINeed . 如果用户现在单击链接按钮,则CommandName将为TextINeed

If you want to call the code inside of an event handler from another one you should create a method that you can call from both event handlers. 如果要从另一个事件处理程序中调用代码,则应创建一个可以从两个事件处理程序中调用的方法。

private void MyFirstMethod()
{
    //Some Code
}

protected void Name_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "TextINeed")
    {
        MyFirstMethod();
    }
}

protected void btn_someButton(object sender, EventArgs e)
{
    MyFirstMethod();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM