How to get the index of the gridview with click of asp button
-
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
-
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
You have to use
e.RowIndex
, where e isEventArgs
. -
laziale wrote:
not GridViewCommandEventArgs, since with GridViewCommandEventArgs nothing is happening
Can you post your code?
Cheers!! Brij
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
-
You have to use
e.RowIndex
, where e isEventArgs
. -
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
-
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
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.