[SOLVED] How can i get the textbox value in selected row in gridview

Issue

I am working on a project in c# asp.net.

I created a gridview and connected it to a database. My code is like that;

<asp:GridView ID="GridView1" runat="server" 
                class="table table-bordered table table-hover " AutoGenerateColumns="false"  HeaderStyle-Height="40px" OnRowCommand="GridView1_RowCommand1"  >

            <Columns>
                 
                 <asp:TemplateField HeaderText="Numune Parçası" >
                 <ItemTemplate>
                 <asp:Literal ID="Literal22" runat="server" Text='<%# Eval("Açıklama") %>'></asp:Literal> 
                    </ItemTemplate>
                   </asp:TemplateField>

                  <asp:TemplateField HeaderText="Analiz">
                    <ItemTemplate>
                        <asp:Literal ID="Literal112" runat="server" Text='<%# Eval("Analiz") %>'></asp:Literal> 
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Tartım"  ItemStyle-Width="10%">
                    <ItemTemplate>
                        <asp:TextBox id="txttartim" runat="server" class="form-control"  ></asp:TextBox>
                    </ItemTemplate>

                <ItemStyle Width="10%"></ItemStyle>
                </asp:TemplateField>

                 <asp:TemplateField HeaderText="Birim">
                    <ItemTemplate>
                    <asp:DropDownList ID="birimlist" class="form-control" runat="server" >
                    <asp:ListItem>g</asp:ListItem>
                    <asp:ListItem>mg</asp:ListItem>
                    <asp:ListItem>ml</asp:ListItem>
                    <asp:ListItem>cm2</asp:ListItem> 
                    </asp:DropDownList>
                    </ItemTemplate>               
                </asp:TemplateField>

                 <asp:TemplateField HeaderText="Kaydet">
                       <ItemTemplate>        

                    <asp:LinkButton ID="Btn_Indir" runat="server"  
                         class="btn btn-primary btn-sm" Text="Kaydet" CommandName="Open" CommandArgument='<%# Eval("ID") %>' />                                                                                               
                       </ItemTemplate>
                       </asp:TemplateField>



            </Columns>
                <HeaderStyle BackColor="#D14124" CssClass="gridheader" ForeColor="White" HorizontalAlign="Left" />
        </asp:GridView>

And the code behind is like that

    protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Open")
        {

            int RowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            string name = ((TextBox)GridView1.Rows[RowIndex].FindControl("txttartim")).Text;

            DropDownList bir = (DropDownList)GridView1.Rows[RowIndex].FindControl("birimlist");
            string birim = bir.SelectedItem.Value;
        
        }

    }

But my problem is that i can not get the value of textbox and dropdownlist in which selected row.

The strings name and birim is null.

Solution

I solve the problem.

When i write the code in the Page_load section, it works! It is not null anymore.

if (!Page.IsPostBack)
   {
      load();
      ..
   }

Answered By – Oğuzhan Eker

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *