最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Get rowindex of gridview on click of a button outside gridview - Stack Overflow

programmeradmin1浏览0评论

In my asp application (v4.0), I have a grid view. I use a list object to bind data to the grid view.

In the grid view there is a cancel button. On click of the cancel button the application should pop a message to the user asking for confirmation to proceed with cancel. ie. are you sure you want to cancel the record yes/no. When the user selects yes then the particular record should be cancelled.

Now the issue is, when the user selects yes then i need to the get the index of the row for which the cancel button is clicked and i need to remove it from the list object that is used to bind the grid and rebind the gridview .

please let me know how to achieve this.

Thanks for all the reply.. im using custom pop up instead of built in 'confirm' method. The custom pop up will have 'OK' and 'Cancel' button controls. Only on click of 'OK' button i need to get the selected record index. Built in confirm method mentioned in some replies will not suit my scenario. please let me know how to achieve this.

In my asp application (v4.0), I have a grid view. I use a list object to bind data to the grid view.

In the grid view there is a cancel button. On click of the cancel button the application should pop a message to the user asking for confirmation to proceed with cancel. ie. are you sure you want to cancel the record yes/no. When the user selects yes then the particular record should be cancelled.

Now the issue is, when the user selects yes then i need to the get the index of the row for which the cancel button is clicked and i need to remove it from the list object that is used to bind the grid and rebind the gridview .

please let me know how to achieve this.

Thanks for all the reply.. im using custom pop up instead of built in 'confirm' method. The custom pop up will have 'OK' and 'Cancel' button controls. Only on click of 'OK' button i need to get the selected record index. Built in confirm method mentioned in some replies will not suit my scenario. please let me know how to achieve this.

Share Improve this question edited Apr 10, 2014 at 2:46 user1531912 asked Mar 19, 2014 at 11:37 user1531912user1531912 3721 gold badge5 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Add a hiddenfield into your page

<asp:HiddenField ID="HiddenField1" runat="server" />

Use the Id(use corresponding column name instead of Id) of the records as the CommandArgument of Cancel button

<asp:Button ID="btncancel" runat="server" CommandArgument='<%#Eval("Id") %>'  Text="Cancel" />

Then on clicking the cancel button it calls the gridviews rowmand function. In that function keep the CommandArgument value in the hidden field as follows

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

    HiddenField1.Value = e.CommandArgument.ToString();

}

Then on clicking the OK button, it calls the click event. In that function remove the Item from the List and then bind the list again to the gridview

protected void btnOK_Click(object sender, ButtonClickEventArgs e)
{
   string id = HiddenField1.Value;
   //use this id to remove the data from the List
   // bind the gridview
}

Try this..

You can use javascript function..

<asp:Button ID="Button1" runat="server"  onclientclick="Validate(this) />

Declare an HTML hidden field in your page...

<input id="Hidden1" type="hidden" runat="server" clientidmode="static"/>

function Validate(obj) {
    var r = confirm("are you sure you want to cancel ?");
    if (r == true) {
        var id = obj.id.toString();

        var index = id.split("_");

        var RowNumber = index[2].toString();
        document.getElementById('Hidden1').value=RowNumber ;
    }
    else {
        return;
    }
}

Here we get id of the button somewhat as ContentPlacedholde_Button1_0..Then we split it to get the index..

Use mand name and mand argument on cancel button like this:

 <asp:Button ID="btncancel" runat="server" CommandArgument='<%#Eval("Id") %>' CommandName="Cancel" Text="Cancel" />

And then on gridviewRowmand use this:

 if (e.CommandName == "Cancel")
        {
              int count = GridViewName.Rows.Count;
                for (int i = 0; i < count; i++)
                {                      
                        int id = Convert.ToInt32(e.CommandArgument);
                }
        }

Have you tried adding a CommandArgument value to the cancel button that represents the item, for example an ID? Then onclick, display a popup, if the user selects yes then use the ID to remove the item from your collection, then simply rebind the grid. The item will then be gone.

发布评论

评论列表(0)

  1. 暂无评论