简体   繁体   English

C# GridView 获取文本框输入

[英]C# GridView get textbox input

I have a problem that is most likely easy but I'm missing something small.我有一个很可能很容易出现的问题,但我错过了一些小问题。

I have looked at numerous entries on Stack Overflow with the same question but I must be doing something wrong.我在 Stack Overflow 上查看了很多关于相同问题的条目,但我一定是做错了什么。

I am trying to get the value of a textbox in a GridView after typing into it.我试图在输入 GridView 后获取文本框的值。 I want to take that value and update a SQL field.我想采用该值并更新 SQL 字段。

Here is my GridView:这是我的 GridView:

<asp:GridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SALESID" HeaderStyle-HorizontalAlign="CENTER" HorizontalAlign="CENTER" CellPadding="4" ForeColor="#333333" GridLines="Both" AllowSorting="true" AllowPaging="true" PageSize="100" OnRowDataBound="ASPxGridView1_RowDataBound" OnSorting="ASPxGridView1_Sorting" OnRowUpdating="ASPxGridView1_RowUpdating">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>
                        <%-- 0  This is e.Row.Cells[0].Text --%>
                        <asp:BoundField DataField="SALESID" HeaderText="SALES ORDER" SortExpression="SALESID" HeaderStyle-CssClass="padLeft" HeaderStyle-Width="125" ItemStyle-Width="115">
                            <HeaderStyle HorizontalAlign="Center"  />
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                           
                        <%-- 1 --%>
                        <asp:BoundField DataField="ITEMID" HeaderText="PART NUMBER" HeaderStyle-HorizontalAlign="Center" SortExpression="ITEMID" ItemStyle-Width="150" >
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                           
                        <%-- 2 --%>
                        <asp:BoundField DataField="NAME" HeaderText="PART NAME"  HeaderStyle-HorizontalAlign="Center" SortExpression="NAME" ItemStyle-Width="320">
                            <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Left" />
                        </asp:BoundField>
                            
                        <%-- 3 --%>
                        <asp:BoundField DataField="CUSTACCOUNT" HeaderText="ACC" HeaderStyle-HorizontalAlign="Center" SortExpression="CUSTACCOUNT">
                        <ItemStyle HorizontalAlign="Left" />
                        </asp:BoundField>
                            
                        <%-- 4 --%>
                        <asp:BoundField DataField="SALESNAME" HeaderText="CUSTOMER"  HeaderStyle-HorizontalAlign="Center" SortExpression="SALESNAME" ItemStyle-Width="350" >
                        <ItemStyle HorizontalAlign="Left" />
                        </asp:BoundField>
                           
                        <%-- 5 --%>
                        <asp:BoundField DataField="SHIPPINGDATECONFIRMED" HeaderText="SHIPPING DATE"  HeaderStyle-HorizontalAlign="Center" SortExpression="SHIPPINGDATECONFIRMED" ItemStyle-Width="100" >
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                            
                        <%-- 6 --%>
                        <asp:BoundField DataField="QTYORDERED" HeaderText="QTY ORDERED"  HeaderStyle-HorizontalAlign="Center" SortExpression="QTYORDERED" >
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                            
                        <%-- 7 --%>
                        <asp:BoundField DataField="REMAINSALESPHYSICAL" HeaderText="QUANTITY REMAINED"  HeaderStyle-HorizontalAlign="Center" SortExpression="REMAINSALESPHYSICAL" >
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>

                        <%-- 8 --%>
                        <asp:BoundField DataField="AVAILPHYSICAL" HeaderText="AVAIL PHYSICAL"  HeaderStyle-HorizontalAlign="Center" SortExpression="AVAILPHYSICAL" ItemStyle-Width="80" >
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                            
                        <%-- 9 --%>
                        <asp:HyperLinkField DataTextField="WMSLOCATIONID" HeaderText="LOCATION"  HeaderStyle-HorizontalAlign="Center" DataNavigateURLFields="ITEMID" DataNavigateURLFormatString="~\Location.aspx?ITEMID={0}" Target="_blank" SortExpression="WMSLOCATIONID">
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:HyperLinkField>

                        <%-- 10 --%>
                        <asp:HyperLinkField DataTextField="INPROCESS" HeaderText="IN PROCESS"  HeaderStyle-HorizontalAlign="Center" DataNavigateUrlFields="ITEMID" DataNavigateUrlFormatString="~\InProcess.aspx?ITEMID={0}" Target="_blank" SortExpression="INPROCESS">
                        <ItemStyle HorizontalAlign="Left" /> 
                        </asp:HyperLinkField>

                        <%-- 11 --%>
                        <asp:BoundField DataField="PRD" HeaderText="PRD" HeaderStyle-HorizontalAlign="Center" SortExpression="PRD" ItemStyle-Width="135">
                            <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                       
                         <%-- 12 --%>
                        <asp:TemplateField HeaderText="SHIPTODAY" ItemStyle-Width="90">
                            <ItemTemplate>
                                <asp:TextBox ID="SHIPTODAY" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>

                        <%-- 13 --%>
                        <asp:TemplateField HeaderText="NOTES" >
                            <ItemTemplate>
                                <asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField> 
                        
                        <asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />
                          
                        
                    </Columns>
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="False" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                </asp:GridView>

Here is my gridview _RowUpdating method:这是我的 gridview _RowUpdating 方法:

  protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            Debug.WriteLine("row updating");
            int index = Convert.ToInt32(e.RowIndex);
            Debug.WriteLine("Index: " + index);
            GridViewRow selectedRow = ASPxGridView1.Rows[index];
            TableCell itemIDSelected = selectedRow.Cells[1];
            TableCell rowPRD = selectedRow.Cells[11];
            TableCell rowShipToday = selectedRow.Cells[12];
            TableCell rowNotes = selectedRow.Cells[13];
            string itemIdSelectedText = itemIDSelected.Text;
            string rowPRDSelected = rowPRD.Text;
            string rowShipTodaySelected = rowShipToday.Text;
            string test = ((TextBox)selectedRow.FindControl("NOTES")).Text;
            string test1 = ((TextBox)ASPxGridView1.Rows[index].FindControl("NOTES")).Text;
            TextBox test2 = ASPxGridView1.Rows[index].FindControl("NOTES") as TextBox;
            string test2text = test2.Text;
            //string rowNotesSelected = rowNotes.Text;
            //Debug.WriteLine(itemIdSelectedText);
            //Debug.WriteLine(itemIdSelectedText);
            //Debug.WriteLine(rowPRDSelected);
            //Debug.WriteLine(rowShipTodaySelected);
            Debug.WriteLine(test1);
            Debug.WriteLine(test);
            Debug.WriteLine(test2text);
            //Debug.WriteLine(selectedRow.Cells[0].Text);
            //Debug.WriteLine(selectedRow.Cells[1].Text);
            //Debug.WriteLine(selectedRow.Cells[2].Text);
            //Debug.WriteLine(selectedRow.Cells[3].Text);
            //Debug.WriteLine(selectedRow.Cells[4].Text);
            //Debug.WriteLine(selectedRow.Cells[5].Text);
            //Debug.WriteLine(selectedRow.Cells[6].Text);
            //Debug.WriteLine(selectedRow.Cells[7].Text);
            //Debug.WriteLine(selectedRow.Cells[8].Text);
            //Debug.WriteLine(selectedRow.Cells[9].Text);
            //Debug.WriteLine(selectedRow.Cells[10].Text);
            Debug.WriteLine(selectedRow.Cells[11].Text);
            //Debug.WriteLine(selectedRow.Cells[12].Text);
            //Debug.WriteLine(selectedRow.Cells[13].Text);
            Debug.WriteLine("-----------");
            //Debug.WriteLine(rowShipTodaySelected);
            //Debug.WriteLine(rowNotesSelected);
        }

When I run this and I type something in, the debug writeline's show.当我运行它并输入一些内容时,调试写入行的显示。

I know i'm on the correct line because the index is correct per the line I am clicking the Update button on.我知道我在正确的行上,因为根据我单击更新按钮的行,索引是正确的。

Sample debug output from above looks like this:上面的示例调试 output 如下所示:

row updating
Index: 5
D31948P01
PRD-00030521




PRD-00030521

I cannot seem to get the value of the NOTES TextBox field.我似乎无法获得 NOTES TextBox 字段的值。 As you can see with my testing I've tried using a FindControl and I've tried having it as a TextBox and also trying to get it directly to a string.正如您在我的测试中看到的那样,我尝试使用 FindControl 并且尝试将其作为 TextBox 并尝试将其直接获取到字符串。

I've set breakpoints and each time I do it the test variable is null .我设置了断点,每次我这样做时, test变量都是null I'm missing something and I do not know what.我错过了一些东西,我不知道是什么。

To summarize: On non-text fields I can pull the data from the field.总结一下:在非文本字段上,我可以从字段中提取数据。 On TextBox fields I cannot even using the FindControl method.在 TextBox 字段上,我什至无法使用 FindControl 方法。

What I want to do: Get the TextBox field value in the GridView so I can update the database.我想要做什么:获取 GridView 中的 TextBox 字段值,以便我可以更新数据库。

StackOverflow links I've tried before asking this question:在问这个问题之前我尝试过的 StackOverflow 链接:
c# Get value (text) of a bound textbox in a gridview c# 获取 gridview 中绑定文本框的值(文本)
How to get value of TextBox of GridView in C#? 如何在 C# 中获取 GridView 的 TextBox 的值?
Manipulate textbox on gridview C# 操作 gridview C# 上的文本框
Get TextBox value from GridView cell 从 GridView 单元格获取 TextBox 值
ASP.net C# Gridview ButtonField onclick event ASP.net C# Gridview ButtonField onclick 事件

Thanks in advance.提前致谢。 I've spent over 6 hours of attempting this without results.我已经花了 6 个多小时尝试这个,但没有结果。 I don't understand it well enough yet.我还不太明白。 If there are any tutorials or videos explaining GridView's please let me know what they are so I can understand better.如果有任何解释 GridView 的教程或视频,请告诉我它们是什么,以便我更好地理解。

The reason why RowUpdating method cannot find the text inside the textbox is most likely because of the postback. RowUpdating 方法在文本框中找不到文本的原因很可能是因为回发。 Clicking update button causes a post back to server.单击更新按钮会导致回发到服务器。 Before ASPxGridView1_RowUpdating is called, there is an a call to re-build the gridview ie.: ASPxGridView1.DataBind() .在调用 ASPxGridView1_RowUpdating 之前,有一个调用来重建 gridview 即: ASPxGridView1.DataBind() To avoid this, wrap the databind in an if (.Page.IsPostBack)为避免这种情况,请将数据绑定包装在if (.Page.IsPostBack)

Below is a basic working example下面是一个基本的工作示例

<asp:GridView ID="ASPxGridView1" 
            runat="server" 
            AutoGenerateColumns="False" 
            OnRowUpdating="ASPxGridView1_RowUpdating">
            <Columns>         
                <asp:TemplateField HeaderText="NOTES" >
                    <ItemTemplate>
                        <asp:TextBox ID="NOTES" runat="server" TextMode="MultiLine"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>                         
                <asp:ButtonField ButtonType="Button" HeaderText="" CommandName="Update" Text="Update" />                                                  
            </Columns>
        </asp:GridView>

Code behind:后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // This code will not be reached if you click update button.
        var data = new string[] { "a", "b", "c" };
        ASPxGridView1.DataSource = data;
        ASPxGridView1.DataBind();
    }
}

protected void ASPxGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
           
    GridViewRow selectedRow = ASPxGridView1.Rows[index];            
    TextBox txtNotes = (TextBox)selectedRow.FindControl("NOTES");
    Debug.WriteLine(txtNotes.Text);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM