gridview에서 클릭으로 이미지에서 행 색인 가져 오기
-
19-08-2019 - |
문제
템플릿 필드를 통해 열에 imageButtons가 추가 된 그리드 뷰가 있습니다. "Onclick"이벤트에 함수를 첨부했습니다.
이 함수에서 일단 클릭 한 버튼이있는 행의 색인을 어떻게 얻을 수 있습니까? 내가 가진 것은 페이지의 마우스 좌표 만있는 것 같습니다.
해결책
행을 통과하는 대신에 사용할 수 있습니다.
<asp:ImageButton runat="server" id="ibtn1" ... RowIndex='<%# Container.DisplayIndex %>'
OnClick="button_click"/>
...
protected void button_click(object sender, EventArgs e){
ImageButton ibtn1 = sender as ImageButton;
int rowIndex = Convert.ToInt32(ibtn1.Attributes["RowIndex"]);
//Use this rowIndex in your code
}
다른 팁
발신자를 ImageButton에 캐스트 한 다음 이미지 버튼의 namingcontainer를 행으로 캐스팅합니다.
VB :
Dim btn as ImageButton = CType(sender, ImageButton)
Dim row as GridViewRow = CType(btn.NamingContainer, GridViewRow)
씨#:
ImageButton btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
가장 간단한 옵션은 Commandargument를 사용하는 것이 가장 간단한 옵션에 동의합니다. 데이터의 고유 ID 번호를 해당 속성에 바인딩 한 다음 _RowCommand 이벤트를 처리하십시오.
예를 들어:
<asp:TemplateField >
<HeaderStyle Width="20" />
<ItemTemplate>
<asp:ImageButton ImageUrl="images/icons/iCal.png" CommandArgument='<%# Eval("Event_ID") %>' ToolTip="iCal" runat="server" Height="18" Width="18" />
</ItemTemplate>
</asp:TemplateField>
Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
e.CommandArgument 'use this value in whatever way you like
End Sub
이것은 아주 좋은 속임수입니다. 또 다른 트릭도 있습니다. 당신은 그것을 시도 할 수 있습니다 ...
protected void userGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int rowindex = rowSelect.RowIndex;
}
}
또한 좋은 방법입니다.
제휴하지 않습니다 StackOverflow