I want to get a row’s column value (and make query operations) from onclick event of row’s button. How can ı do this ? When btnAktar is pressed I want to take Meslek column’s value in that row. Thank you. my gridview is as follows:
<iskurControls:IskurGridView runat="server"
ID="ctlGridSgkTecrube"
AutoGenerateColumns="False"
EmptyDataText="Tecrübe Bilginiz Bulunmamaktadır."
EnableViewState="true"
CssClass="table table-bordered table-condensed text-small">
<Columns>
<asp:TemplateField HeaderText="Seç">
<ItemTemplate>
<asp:Button ID="btnAktar" runat="server" Text="Aktar" CommandName="AktarRow" class = "inp" OnClientClick="return callme(this)" OnClick="btnIstecrubesi_Click" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ISYERISICILNO" HeaderText="İşyeri Sicil No" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ISYERIADI" HeaderText="İşyeri Adı" >
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="MESLEKKODU" HeaderText="Meslek Kodu">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="MESLEK" HeaderText="Meslek">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="SURE" HeaderText="Süre">
<HeaderStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="BASLANGICTARIHI" HeaderText="Baş Tar">
<HeaderStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="BITISTARIHI" HeaderText="Bit Tar" DataFormatString="{0:d}" >
<HeaderStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="SEKTOR" HeaderText="Sektör" ItemStyle-CssClass="Hide" HeaderStyle-CssClass="Hide" />
</Columns>
<PagerStyle CssClass="grid-PagerStyle"></PagerStyle>
<SelectedRowStyle CssClass="grid-SelectedRowStyle"></SelectedRowStyle>
<HeaderStyle CssClass="grid-HeaderStyle"></HeaderStyle>
<EditRowStyle CssClass="grid-EditRowStyle"></EditRowStyle>
<AlternatingRowStyle CssClass="grid-AlternatingRowStyle"></AlternatingRowStyle>
<FooterStyle CssClass="grid-FooterStyle"></FooterStyle>
<RowStyle CssClass="grid-RowStyle"></RowStyle>
<EmptyDataRowStyle CssClass="grid-EmptyRow" />
</iskurControls:IskurGridView>
When btnAktar is pressed I want to take Meslek column’s value in that row. I need to do this in backEnd.
2
Answers
Using commandArgument of <%# ((GridViewRow) Container).RowIndex %> solved.
Ok, so you have a standard asp.net button like this:
Note how I have removed the CommandName (you don’t need it).
It’s also not clear why you have a different name for the OnClick event?
The default should be as per above.
So, now the code behind stub:
Since your controls in the GridView are databound columns, then you use the cells() collection to fetch out a given value.
If your column was a templated field, then you use gRow.FindControl("control name") to fetch out the control in that given row.
Note that NamingContainer used in above also works for all data bound controls from GridView, ListView, Repeater and more.
So, like any button, just wire up a simple click event. In such examples, there is no real need nor case to use GridView events and features such as CommandName.
So above shows the row index value, the hidden database PK value (this requires you having set datakeys in the GridView definiation. Say like this:
So, note the use of DataKeyNames. This allows you to use the database PK value, but you don’t have to display nor include the database PK value in the columns.