Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to get the index of the gridview with click of asp button

How to get the index of the gridview with click of asp button

Scheduled Pinned Locked Moved ASP.NET
databasetutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laziale
    wrote on last edited by
    #1

    Hello - I want to get the index of the row where the button is clicked. Please note that I am using EventArgs, not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening, once the button is clicked. Thx, Laziale

    B A J 3 Replies Last reply
    0
    • L laziale

      Hello - I want to get the index of the row where the button is clicked. Please note that I am using EventArgs, not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening, once the button is clicked. Thx, Laziale

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      laziale wrote:

      not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening

      Can you post your code?

      Cheers!! Brij

      L 1 Reply Last reply
      0
      • L laziale

        Hello - I want to get the index of the row where the button is clicked. Please note that I am using EventArgs, not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening, once the button is clicked. Thx, Laziale

        A Offline
        A Offline
        Abhijit Jana
        wrote on last edited by
        #3

        You have to use e.RowIndex, where e is EventArgs.

        L 1 Reply Last reply
        0
        • B Brij

          laziale wrote:

          not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening

          Can you post your code?

          Cheers!! Brij

          L Offline
          L Offline
          laziale
          wrote on last edited by
          #4

          hello thx for your reply and here is my code: backend:

          protected void updateNote(object sender, GridViewCommandEventArgs e)
          {
          string selectedIndex = "";
          int index = 0;
          index = Convert.ToInt32(e.CommandArgument);
          // index = 1;
          // Button bttn = (Button)sender;

               GridViewRow row = gvOrderNotes.Rows\[index\];
              
                   ListItem item = new ListItem();
                  item.Text = Server.HtmlDecode(row.Cells\[3\].Text);
                  selectedIndex = item.Text;
              }
          

          html:

          <ItemTemplate>
          <asp:Button runat="server" ID="ChkUpdate" onclick="updateNote" AutoPostBack="true" Width="160%" Text="Update Order"/>
          <ajaxToolkit:ConfirmButtonExtender ID="chkUpdateConfirmBtnExtender" runat="server" TargetControlID="chkUpdate" DisplayModalPopupID="modalPopupExtenderChkUpdate"></ajaxToolkit:ConfirmButtonExtender>
          <ajaxToolkit:ModalPopupExtender ID="modalPopupExtenderChkUpdate" runat="server" TargetControlID="chkUpdate" PopupControlID="pnlChkUpdate"
          OKControlID="chkUpdateOK" CancelControlID="chkUpdateCancel" BackgroundCssClass="modalBackground" />
          </ItemTemplate>

          Thx for your help one more time. Laziale

          B 1 Reply Last reply
          0
          • A Abhijit Jana

            You have to use e.RowIndex, where e is EventArgs.

            L Offline
            L Offline
            laziale
            wrote on last edited by
            #5

            there is no rowIndex under EventArgs e. I have just Equals, GetHashCode, GetType and ToString. Thx for your help though. Laziale

            1 Reply Last reply
            0
            • L laziale

              hello thx for your reply and here is my code: backend:

              protected void updateNote(object sender, GridViewCommandEventArgs e)
              {
              string selectedIndex = "";
              int index = 0;
              index = Convert.ToInt32(e.CommandArgument);
              // index = 1;
              // Button bttn = (Button)sender;

                   GridViewRow row = gvOrderNotes.Rows\[index\];
                  
                       ListItem item = new ListItem();
                      item.Text = Server.HtmlDecode(row.Cells\[3\].Text);
                      selectedIndex = item.Text;
                  }
              

              html:

              <ItemTemplate>
              <asp:Button runat="server" ID="ChkUpdate" onclick="updateNote" AutoPostBack="true" Width="160%" Text="Update Order"/>
              <ajaxToolkit:ConfirmButtonExtender ID="chkUpdateConfirmBtnExtender" runat="server" TargetControlID="chkUpdate" DisplayModalPopupID="modalPopupExtenderChkUpdate"></ajaxToolkit:ConfirmButtonExtender>
              <ajaxToolkit:ModalPopupExtender ID="modalPopupExtenderChkUpdate" runat="server" TargetControlID="chkUpdate" PopupControlID="pnlChkUpdate"
              OKControlID="chkUpdateOK" CancelControlID="chkUpdateCancel" BackgroundCssClass="modalBackground" />
              </ItemTemplate>

              Thx for your help one more time. Laziale

              B Offline
              B Offline
              Brij
              wrote on last edited by
              #6

              Why dont you use rowcommand event of gridview.. use your button like in the link

              Cheers!! Brij

              1 Reply Last reply
              0
              • L laziale

                Hello - I want to get the index of the row where the button is clicked. Please note that I am using EventArgs, not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening, once the button is clicked. Thx, Laziale

                J Offline
                J Offline
                jayantbramhankar
                wrote on last edited by
                #7

                try this, Add commandArgument in tag

                <asp:Button ID="Button1" runat="server" Text='<%#Eval("YourDataFiled")%>'

                              CommandArgument = "Button1"  OnClick = "Button1\_Click" /> 
                

                c# code

                protected void Button1_Click(object sender, EventArgs e)
                {
                Button button = (Button)sender;

                    string CommandName = button.CommandName;
                
                    string CommandArgument = button.CommandArgument;
                    int index = Int32.parse(button.CommandArgument);
                
                
                } 
                

                Hope this helps.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups