ASP.NET GridView RowIndex As CommandArgument
buttonfield 열 단추의 명령 인수로 gridview 항목의 행 인덱스에 액세스하고 표시하는 방법은 무엇입니까?
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button"
CommandName="Edit" Text="Edit" Visible="True"
CommandArgument=" ? ? ? " />
.....
다음은 매우 간단한 방법입니다.
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True"
CommandArgument='<%# Container.DataItemIndex %>' />
MSDN 은 다음과 같이 말합니다.
ButtonField 클래스는 CommandArgument 속성을 적절한 인덱스 값으로 자동으로 채 웁니다. 다른 명령 단추의 경우 명령 단추의 CommandArgument 속성을 수동으로 설정해야합니다. 예를 들어 GridView 컨트롤에 페이징이 활성화되지 않은 경우 CommandArgument를 <% # Container.DataItemIndex %>로 설정할 수 있습니다.
따라서 수동으로 설정할 필요가 없습니다. GridViewCommandEventArgs를 사용하는 행 명령은 액세스 할 수 있도록합니다. 예 :
protected void Whatever_RowCommand( object sender, GridViewCommandEventArgs e )
{
int rowIndex = Convert.ToInt32( e.CommandArgument );
...
}
다음은 이에 대한 Microsoft 제안입니다. http://msdn.microsoft.com/en-us/library/bb907626.aspx#Y800
gridview에서 명령 단추를 추가하고 템플릿으로 변환 한 다음이 경우에는 " AddToCart " 명령 이름을 지정 하고 CommandArgument "<% # ((GridViewRow) Container) .RowIndex %>"도 추가합니다.
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
그런 다음 gridview의 RowCommand 이벤트에서 "AddToCart"명령이 트리거되는시기를 식별하고 거기에서 원하는대로 수행합니다.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCart")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
** 내가 저지른 한 가지 실수는 RowCommand 이벤트에서 직접 수행하는 대신 템플릿 단추에 작업을 추가하고 싶었다는 것입니다.
나는 이것이 작동 할 것이라고 생각한다.
<gridview>
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Edit" Text="Edit" Visible="True" CommandArgument="<%# Container.DataItemIndex %>" />
</Columns>
</gridview>
void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button b = (Button)e.CommandSource;
b.CommandArgument = ((GridViewRow)sender).RowIndex.ToString();
}
일반적으로 GridView와 함께 RowDatabound 이벤트를 사용하여이 데이터를 바인딩합니다.
protected void FormatGridView(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((Button)e.Row.Cells(0).FindControl("btnSpecial")).CommandArgument = e.Row.RowIndex.ToString();
}
}
<asp:TemplateField HeaderText="" ItemStyle-Width="20%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkAdd" Text="Add" CommandName="Add" CommandArgument='<%# Eval("EmpID"))%>' />
</ItemTemplate>
</asp:TemplateField>
이것은 강력한 형식의 데이터를 가진 asp.net 프레임 워크의 전통적인 방법이자 최신 버전이며 "EMPID"와 같은 문자열로 사용할 필요가 없습니다.
<asp:LinkButton ID="LnkBtn" runat="server" Text="Text" RowIndex='<%# Container.DisplayIndex %>' CommandArgument='<%# Eval("??") %>' OnClick="LnkBtn_Click" />
이벤트 핸들러 내부 :
Protected Sub LnkBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
dim rowIndex as integer = sender.Attributes("RowIndex")
'Here you can use also the command argument for any other value.
End Sub
ReferenceURL : https://stackoverflow.com/questions/389403/asp-net-gridview-rowindex-as-commandargument
'your programing' 카테고리의 다른 글
flutter의 wrap_content 및 match_parent에 해당합니까? (0) | 2021.01.05 |
---|---|
Python의 시퀀스에서 항목을 제거하는 우아한 방법? (0) | 2021.01.05 |
Windows에서 pkg 구성을 설치하는 방법은 무엇입니까? (0) | 2021.01.05 |
낮은 권한으로 PL-SQL을 사용하여 Oracle에서 열 데이터 유형을 얻으려면 어떻게해야합니까? (0) | 2021.01.05 |
android sdk main.out.xml 파싱 오류? (0) | 2021.01.05 |