【问题标题】:I cannot access the data entered into an edit textbox in a databound gridview in asp.net C#我无法访问在 asp.net C# 的数据绑定网格视图中输入到编辑文本框中的数据
【发布时间】:2022-10-20 01:11:37
【问题描述】:

我创建了一个gridview,它现在完全按照我需要的方式运行,但是我需要对gridview 进行编辑。这有点奇怪,因为我必须创建一个 xml 字符串来输入 api,而不是直接更新表。我的每一行都有一个编辑按钮。单击编辑按钮将单元格转换为填充有预期初始值的 texbox,并将编辑按钮转换为“更新”和“取消”按钮。我已经实现了一个在单击“更新”按钮时成功触发的过程。问题是我似乎无法获得输入到文本框中的值。

我试过检查文本框并使用那里的信息来调用 findControl 函数,我也尝试过 Request.Form 函数,但是这些都不起作用,它们返回了一个空值。我得到的最好结果是通过硬编码单元格和控件数组地址,如“((TextBox)(row.Cells [5].Controls [0])).Text”。当我使用这种方法时,我得到了正确的单元格,在这种情况下是状态,但它会提取原始值而不是用户输入的值。例如,在这种情况下,原始值为“Active”,我在文本框中键入“Canceled”并点击更新,“Active”的值将在我的语句中返回。

如果需要,我很乐意提供更多信息或说明。提前感谢您提供的任何帮助。

网格视图定义:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server" >
   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" HeaderStyle-VerticalAlign="Bottom"
       RowStyle-Wrap="true" HeaderStyle-Wrap="true" OnDataBound ="OnDataBound" AllowSorting="True" HeaderStyle-Height="50px"
       OnSorting="GridView1_SelectedIndexChanged" CssClass="reportData" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
       CellPadding="4" ForeColor="Black" GridLines="Vertical" AutoGenerateEditButton="true" OnRowEditing ="GridView_RowEditing" 
       OnRowCancelingEdit="GridView_RowCancellingEdit" OnRowUpdating="GridView_RowUpdating" DataKeyNames ="PropID,Project Type" >
       
       
       <AlternatingRowStyle BackColor="White" />
       <FooterStyle BackColor="#CCCC99" />
<HeaderStyle Wrap="True" Height="50px" BackColor="#6B696B" Font-Bold="True" ForeColor="White"></HeaderStyle>

       <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />

<RowStyle Wrap="True" BackColor="#F7F7DE"></RowStyle>
       <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
       <SortedAscendingCellStyle BackColor="#FBFBF2" />
       <SortedAscendingHeaderStyle BackColor="#848384" />
       <SortedDescendingCellStyle BackColor="#EAEAD3" />
       <SortedDescendingHeaderStyle BackColor="#575357" />
   </asp:GridView>

后面代码中Gridview的构建:

            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand com = new SqlCommand(sqlString, con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);

            BoundField bf = new BoundField
            {
                HeaderText = "State",
                DataField = "State"
            };
            GridView1.Columns.Add(bf);

            bf = new BoundField
            {
                HeaderText = "Status",
                DataField = "Status"
            };

            bf.ItemStyle.CssClass = "statusField";
            bf.HeaderStyle.CssClass = "statusHead";


            GridView1.Columns.Add(bf);

还有其他领域,但你明白了

            GridView1.AutoGenerateColumns = false;
            GridView1.DataBind();
            con.Close();

在gridview rowUpdating:

GridViewRow row = GridView1.Rows[e.RowIndex]

            for (int i = 0; i< GridView1.Rows.Count; i++)
            {
                string text = ((TextBox)(row.Cells[5].Controls[0])).Text;
            }

【问题讨论】:

  • 我已经玩了更长的时间并且基本上清理了我的代码,但是在我的调查中我发现这个问题只发生在我网格的第一行。如果我编辑不同的行,用户输入的值将通过文本框返回。我确实在页面加载中有数据绑定,它在我的“IsPostBack”检查之外,但是如果我将该数据绑定包装在检查中,当我单击编辑按钮时,我的整个网格都会消失。

标签: c# asp.net gridview


【解决方案1】:

看起来这是由于我的 gridhelper 组导致行号关闭。

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多