GridView control with two asp: Command columns using the same command

I have an ASP.NET control GridViewwith two columns asp:CommandFieldthat use the Select command to perform various tasks. How to distinguish which column was selected in the event OnRowCommand, when both return "Select", when I check the property of the CommandNameobject GridViewCommandEventArgs?

Here is my source code:

ASPX page:

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false" OnRowCommand="MyGridView_OnRowCommand">
    <Columns>
        <asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />
        <asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="No Click Me!" />
    </Columns>
</asp:GridView>

Code behind:

protected void MyGridView_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    string x = e.CommandName //returns "Select" for both asp:CommandField columns
}   
+3
source share
5 answers

Use the button column so that you can specify specific command names and work with them there

<asp:ButtonField ButtonType="Link" Text="Click Me" CommandName="MyCommand1" />
<asp:ButtonField ButtonType="Link" Text="No Click Me" CommandName="MyCommand2" />

Then you can take an action based on e.CommandName

+4

GridViewCommandEventArgs. CommandArgument!

0

, SELECt ? - , .

-, CommandArgument , , .

0

.

e.commandargument

_ .

gridview_Load

for (int i = 0; i <= GridView1.Rows.Count - 1; i++) {

    linkbutton btnedit = new linkbutton();


    GridView1.Rows(i).Cells(3).Controls.Add(btnedit);
   //the above is where you want the button to be on the grid
    btndel.CommandName = "Select2";
    btndel.CommandArgument = "whatever you want";
}

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "Select1" Then 

       //do stuff
End Sub
0

, , . -, , .

, , , , , RowCommand event GridView CommandName property GridViewCommandEventArgs. GridView "Command" "Button".

string/text CommandName, RowCommand , . Button1 Button2, , , , CommandName , SELECT selecT ..

select CommandName , . , GridView, CommandName , SELECT, , - RowCommand,

if (e.CommandName == "select")
  { 
   Label1.Text = "You clicked first button";
  }
  else 
  {
   Label1.Text = "You clicked second button";
  }

SelectedDataKey GridView . , , CommandName, select_one, select_two , select_one, , SelectedDataKey GridView null DataKey , .

, - , . , !

0

Source: https://habr.com/ru/post/1709695/


All Articles