Home > Back-end >  How can i get the textbox value in selected row in gridview
How can i get the textbox value in selected row in gridview

Time:03-17

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" 
                 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"   ></asp:TextBox>
                    </ItemTemplate>

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

                 <asp:TemplateField HeaderText="Birim">
                    <ItemTemplate>
                    <asp:DropDownList ID="birimlist"  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"  
                          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.

CodePudding user response:

Give this a go:

if (e.CommandName == "Open")
{
    GridViewRow selectedRow = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer);
    string name = ((TextBox)selectedRow.FindControl("txttartim")).Text;

    DropDownList bir = (DropDownList)selectedRow.FindControl("birimlist");
    string birim = bir.SelectedItem.Value;
}

CodePudding user response:

Ok, I often just drop in a plane jane asp.net button, and I don't bother with using the GV built in commands (such as command).

However, you have a command, so you can trigger the "row" command as you have.

And the "row command" fires before the selected index fires, and in fact fires before the selected index triggers.

I note that you passing the "ID". It not really necessary, since a GV has what is called DataKeys feature. That feature lets you get the row (database) PK id, but REALLY nice is you don't have to expose, show, or even use a hidden field, or anything else to get that data key. (this is great for several reasons - one being that you don't have to hide or shove away, or even use the command argument to get that database PK row. The other HUGE issue is then your database pk values are NEVER exposed client side - so nice for security).

Ok, So, make sure your row event is being triggered, and you should be able to use this, first the markup for the Linkbutton:

        <asp:TemplateField  HeaderText="View" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>  
            <asp:LinkButton ID="cmdView" runat="server" Text="View" CommandName="Open" css />
            </ItemTemplate>
        </asp:TemplateField>

Note how I do NOT worry or pass the "ID". As noted, datakeys takes care of this, and in the GV def, we have this:

 <asp:GridView ID="GVHotels" runat="server" 
    AutoGenerateColumns="false" DataKeyNames="ID"
 bla bla bla

So, now our code is this:

  protected void GVHotels_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        LinkButton lBtn = e.CommandSource as LinkButton;
        GridViewRow gRow = lBtn.NamingContainer as GridViewRow;

        int PKID = (int)GVHotels.DataKeys[gRow.RowIndex]["ID"];

        Debug.Print("Row index click = "   gRow.RowIndex);
        Debug.Print("Database PK id = "   PKID);

        // get combo box for this row
        DropDownList cbo = gRow.FindControl("cboRating") as DropDownList;
        Debug.Print("Combo box value = "   cbo.SelectedItem.Value);
        Debug.Print("Combo box Text  = "   cbo.SelectedItem.Text);

Output:

Row index click = 7
Database PK id = 68
Combo box value = 2
Combo box Text  = Good

So, use datakeys - you have have to put in extra attributes here and there to get the PK row value.

In fact, really, using row command is often MORE of a pain, and you can DUMP the command name to trigger that button click. And REALLY nice then is you get a seperte nice commadn for each button (or link button) in the GV. So, I in fact suggest you use this:

      <asp:TemplateField  HeaderText="View" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>  
            <asp:LinkButton ID="cmdView" runat="server" Text="View"
                OnClick="cmdView_Click1" css />
            </ItemTemplate>
        </asp:TemplateField>

So, all we did was add a plane jane good old regular click event. It will not fire row command, and in fact will not trigger "selected index changed" for the GV, but in most cases I don't need them. So, now, if you 2 or 5 buttons, you just think of them as plane jane regular buttons. (just type in onclick in teh markup, and choose "create new event" that intel-sense pops up). So, now our code for the button is seperate, and not part of the row command. Our code thus becomes this:

    protected void cmdView_Click1(object sender, EventArgs e)
    {
        LinkButton lBtn = sender as LinkButton;
        GridViewRow gRow = lBtn.NamingContainer as GridViewRow;

        int PKID = (int)GVHotels.DataKeys[gRow.RowIndex]["ID"];

        Debug.Print("Row index click = "   gRow.RowIndex);
        Debug.Print("Database PK id = "   PKID);

        // get combo box for this row
        DropDownList cbo = gRow.FindControl("cboRating") as DropDownList;
        Debug.Print("Combo box value = "   cbo.SelectedItem.Value);
        Debug.Print("Combo box Text  = "   cbo.SelectedItem.Text);

    }

So, I tend to not bother with row command - better to just have that plane jane click event. Note the use of naming container - but the rest of the code is the same. I would ONLY use "commandName" to trigger the row event, since THEN the gv index changed event fires - and that useful for highlighting the whole row - but if you don't care, then don't even bother with CommandName - just use regular click events, and as noted, this is even better, since then each button gets it 100% own nice little code stub like any other button click tends to have.

So I grabbed a dropdownlist but your code could be say this:

TextBox tBox = gRow.FindControl("txttartim") as TextBox;
debug.Print (tBox.Text);
  • Related