简体   繁体   English

如何获取 gridview 中选定行中的文本框值

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

I am working on a project in c# asp.net.我正在 c# asp.net 做一个项目。

I created a gridview and connected it to a database.我创建了一个 gridview 并将其连接到数据库。 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.字符串名称和 birim 是 null。

Give this a go:给这个 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;
}

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).好吧,我经常只是掉在飞机上 jane asp.net 按钮,我不会费心使用 GV 内置命令(例如 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".我注意到您传递了“ID”。 It not really necessary, since a GV has what is called DataKeys feature.这并不是真正必要的,因为 GV 具有所谓的 DataKeys 功能。 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.该功能允许您获取行(数据库)PK id,但真正好的是您不必公开、显示甚至使用隐藏字段或其他任何东西来获取该数据密钥。 (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). (这有几个原因 - 一个是你不必隐藏或推开,甚至使用命令参数来获取数据库 PK 行。另一个巨大的问题是你的数据库 pk 值永远不会暴露在客户端- 非常适合安全)。

Ok, So, make sure your row event is being triggered, and you should be able to use this, first the markup for the Linkbutton:好的,所以,确保你的行事件被触发,你应该能够使用它,首先是 Linkbutton 的标记:

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

Note how I do NOT worry or pass the "ID".请注意我如何不担心或通过“ID”。 As noted, datakeys takes care of this, and in the GV def, we have this:如前所述,datakeys 会处理这个问题,在 GV def 中,我们有这个:

 <asp:GridView ID="GVHotels" runat="server" class="table"
    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: Output:

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

So, use datakeys - you thus DO NOT have have to put in extra attributes here and there and everywhere to get the PK row value.因此,使用数据键 - 您因此不必到处添加额外的属性来获取 PK 行值。

In fact, really, using row command is often MORE of a pain, and you can DUMP the command name to trigger that button click.事实上,真的,使用行命令往往更痛苦,你可以 DUMP 命令名称来触发按钮点击。 And REALLY nice then is you get a separate nice command "code stub" for each button (or link button) in the GV.真的很不错,那么您会为 GV 中的每个按钮(或链接按钮)获得一个单独的好命令“代码存根”。 (no need to try and shove all that code into the row command, and then a bunch of if/then or "case" statements to run code for a given command. (无需尝试将所有代码都推入 row 命令,然后是一堆 if/then 或“case”语句来运行给定命令的代码。

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" cssclass="btn btn-info" />
            </ItemTemplate>
        </asp:TemplateField>

So, all we did was add a plane jane good old regular click event.所以,我们所做的就是添加一个 plane jane good old 常规点击事件。 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.它不会触发行命令,实际上不会触发 GV 的“选定索引已更改”,但在大多数情况下我不需要它们。 So, now, if you 2 or 5 buttons, you just think of them as plane jane regular buttons.所以,现在,如果你有 2 个或 5 个按钮,你只需将它们视为普通的平面按钮。 (just type in onclick in teh markup, and choose "create new event" that intel-sense pops up). (只需在标记中输入 onclick,然后选择智能感知弹出的“创建新事件”)。 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.请注意命名容器的使用 - 但代码的 rest 是相同的。 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.我只会使用“commandName”来触发行事件,因为然后 gv 索引更改了事件触发 - 这对于突出显示整行很有用 - 但如果你不在乎,那么甚至不必理会 CommandName - 只需使用常规点击事件,如前所述,这甚至更好,因为每个按钮都会像任何其他按钮点击一样拥有 100% 自己的漂亮小代码存根。

So I grabbed a dropdownlist but your code could be say this:所以我抓住了一个下拉列表,但你的代码可以这样说:

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

I solve the problem.我解决了这个问题。

When i write the code in the Page_load section, it works.当我在 Page_load 部分编写代码时,它起作用了。 It is not null anymore.它不再是 null。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法从gridview的页脚行中获取文本框的值 - I can't get the value of textbox from the footer row of gridview 我该如何从gridview的行中的文本框中更改值? - How can i change value from textbox in row in gridview? 如何在ASP.NET C#WebForm中的文本框和下拉列表的GridView页脚行中获取值 - How Can I get the value in the gridview footer row of textbox and dropdownlist in asp.net c# webform 如何在gridview中获取行的值并在文本框中显示? - how to get value of row in gridview and display in textbox? 如何获取在DataGrid或GridView中选择的行? - How can i get the row selected in the DataGrid or GridView? 如何从GridView中获取选定的行值 - How to get selected row value from gridview 当我单击gridview中的文本框时,如何引用选择的行? - How to refer to which row was selected when i click textbox in gridview? 如何使用MVC(aspx).net在文本框中获取gridview的选定值 - how to get selected value of gridview in textbox using MVC(aspx) .net 如何将数据集或数据表行设置为从gridview文本框条目获取的值? - how can I set a dataset or datatable row to a value taken from a gridview textbox entry? 我想获取特定列的gridview的选定行的值,并将该值用于跨页发布-如何实现呢? - I want to get value of selected row of gridview for specific column and use that value for crosspage posting - how to implement this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM