C# 如何从gridview检索文本框文本

C# 如何从gridview检索文本框文本,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个包含文本框和下拉列表的gridview。在gvScheduleBattingScore_RowDataBound事件中,我绑定下拉列表没有任何问题。按钮控件位于gridview的外部。实际上,我想将所有文本框值和下拉选择的值提交到按钮ClickEvent上的数据库,但我不知道哪里出了问题。 问题是文本框不包含任何文本,我得到了异常 输入字符串的格式不正确 请帮帮我 <asp:GridView ID="gvScheduleBattingScore" runat="server" Al

我有一个包含文本框和下拉列表的gridview。在gvScheduleBattingScore_RowDataBound事件中,我绑定下拉列表没有任何问题。按钮控件位于gridview的外部。实际上,我想将所有文本框值和下拉选择的值提交到按钮ClickEvent上的数据库,但我不知道哪里出了问题。 问题是文本框不包含任何文本,我得到了异常

输入字符串的格式不正确

请帮帮我

<asp:GridView ID="gvScheduleBattingScore" runat="server" AllowSorting="false" AutoGenerateColumns="False" AllowPaging="false"
                 GridLines="None" CellPadding="1" CssClass="GridViewStyle" ShowFooter="false" width="100%"
                 OnRowDataBound="gvScheduleBattingScore_RowDataBound">                
                    <Columns>
                        <asp:BoundField DataField="P_PlayerId" HeaderText="Player Id" HeaderStyle-Wrap="true" HeaderStyle-Width="5%" Visible="false"/>
                        <asp:BoundField DataField="PlayerName" HeaderText="Player Name" HeaderStyle-Wrap="true" HeaderStyle-Width="30%"/>
                        <asp:TemplateField HeaderText="Playing Order" HeaderStyle-Wrap="true" HeaderStyle-Width="5%">
                            <ItemTemplate>
                                <asp:TextBox ID="txtPlayingOrder" runat="server" CssClass="TinyTexBox"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Status">
                            <ItemTemplate>
                                <asp:TextBox ID="txtStatus" runat="server"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Bold By">
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlBoldBy" runat="server">                       </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                   </Columns>
                </asp:GridView>
</br>
<asp:Button ID="ButtonAdd" runat="server" Text="Add" CssClass="SmallButton" 
                                 ValidationGroup="Add" onclick="ButtonAdd_Click"/>

在将文本框值转换为int之前添加空检查

if (!string.IsNullOrEmpty(textPlayerId.Text))
    playerId = Convert.ToInt32(textPlayerId);
还要对代码进行这些更改,以便在正确的单元格中找到textbox控件

TextBox textPlyerOrder = (TextBox)GVRow.Cells[2].FindControl("txtPlayingOrder"); 
TextBox textBatsmanStatus = (TextBox)GVRow.Cells[3].FindControl("txtStatus");

根据与@bhoopendra.sahoo的聊天讨论,我们得出结论,这是一个具有约束力的问题

触发Button Click事件时,
GridView
再次绑定导致问题


解决方法是只绑定一次
GridView
,并在其他事件期间限制其绑定。

您能告诉我您从哪里得到确切的错误吗..当我将文本分配给局部变量时,单击按钮添加事件。playerId=转换为32(textPlayerId);plyerOrder=Convert.ToInt16(textPlyerOrder.Text);BatsmanStatus=textsbatsmanstatus.Text;playerId=Convert.ToInt32(textPlayerId)中缺少(.Text);这是一个字符串(textPlayerId)。主要问题是所有文本框都是空的。textPlayerId是一个字符串。因此,不要应用。文本用于此。字符串textPlayerId=GVRow.Cells[0].Text;我的问题是我得到了textPlyerOrder.Text和textBatsmanStatus.Text都是空的。仍然有相同的问题。两个文本框都是空的。
TextBox textPlyerOrder = (TextBox)GVRow.Cells[2].FindControl("txtPlayingOrder"); 
TextBox textBatsmanStatus = (TextBox)GVRow.Cells[3].FindControl("txtStatus");