skip to Main Content

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


  1. Chosen as BEST ANSWER

    Using commandArgument of <%# ((GridViewRow) Container).RowIndex %> solved.

              <iskurControls:IskurGridView runat="server"
                    ID="ctlGridSgkTecrube"
                    AutoGenerateColumns="False"
                    EmptyDataText="Tecrübe Bilginiz Bulunmamaktadır."
                    EnableViewState="true"
                    CssClass="table table-bordered table-condensed text-small" OnRowCommand="ctlGridSgkTecrube_RowCommand">
                    <Columns>
                        <asp:TemplateField HeaderText="Seç">
                            <ItemTemplate>
                                <asp:Button CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ID="btnAktar" runat="server" Text="Aktar" CommandName="AktarRow"  class = "inp" OnClientClick="return callme(this)"  />
                            </ItemTemplate>
                        </asp:TemplateField>
    
    
        protected void ctlGridSgkTecrube_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int row = Convert.ToInt32(e.CommandArgument.ToString());
            String v = ctlGridSgkTecrube.Rows[row].Cells[4].Text;
    
    
            
        }
    

  2. Ok, so you have a standard asp.net button like this:

    <asp:TemplateField HeaderText="Seç">
        <ItemTemplate>
            <asp:Button ID="btnAktar" runat="server" Text="Aktar" class="inp"
                OnClientClick="return callme(this)"
                OnClick="btnAktar_Click" />
        </ItemTemplate>
    </asp:TemplateField>
    

    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:

    Protected Sub btnAktar_Click(sender As Object, e As EventArgs)
    
        Dim btn As Button = sender
        Dim gRow As GridViewRow = btn.NamingContainer
        Dim pk As Integer = GVHotels.DataKeys(gRow.RowIndex)("ID")
    
        Debug.Print($"Row index = {gRow.RowIndex}")
        Debug.Print($"Database PK = {pk}")
    
        Debug.Print($"MESLEK = {gRow.Cells(4).Text}")
    
    
    End Sub
    

    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:

     <asp:GridView ID="GVHotels" runat="server"
        AutoGenerateColumns="False"
        DataKeyNames="ID" 
        CssClass="table table-hover table-striped" 
        Width="45%"
      >
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search