无法在 GridView 更新时获取文本框和下拉列表的值

本文关键字:下拉列表 取文本 获取 GridView 更新 | 更新日期: 2023-09-27 17:56:14

GirdView

<Columns>
    <asp:BoundField DataField="idSupplier" HeaderText="idSupplier" SortExpression="idSupplier" Visible="False"/>
    <asp:TemplateField HeaderText="supplierName" SortExpression="supplierName">
        <ItemTemplate>
            <asp:Label ID="LabelsupplierName" runat="server" Text='<%# Bind("supplierName") %>' ></asp:Label>   
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="updatePriceTo" SortExpression="updatePriceTo">
        <EditItemTemplate>
            <asp:TextBox ID="TextBoxUpdatePriceTo" runat="server" Text='<%# Bind("updatePriceTo") %>' ></asp:TextBox>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="LabelUpdatePriceTo" runat="server" Text='<%# Bind("updatePriceTo") %>' ></asp:Label>   
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="operation" SortExpression="operation">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownListOperation" runat="server" DataTextField="operation" DataValueField="operation" 
                SelectedValue='<%# Bind("operation") %>'>
                <asp:ListItem Text="Select" Value=""></asp:ListItem>
                <asp:ListItem Text="+" Value="+"></asp:ListItem>
                <asp:ListItem Text="-" Value="-"></asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="LabelOperation" runat="server" Text='<%# Bind("operation") %>' ></asp:Label>   
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="updateType" SortExpression="updateType">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownUpdateType" runat="server" DataTextField="updateType" DataValueField="updateType" 
                SelectedValue='<%# Bind("updateType") %>'>
                <asp:ListItem Text="Select" Value=""></asp:ListItem>
                <asp:ListItem Text="$" Value="$"></asp:ListItem>
                <asp:ListItem Text="%" Value="%"></asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="LabelUpdateType" runat="server" Text='<%# Bind("updateType") %>' ></asp:Label>   
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="updateTime" SortExpression="updateTime">
        <ItemTemplate>
            <asp:Label ID="LabelUpdateTime" runat="server" Text='<%# Bind("updateTime") %>' ></asp:Label>   
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

.aspx.cs

protected void GridViewHouzzPriceUpdate_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)GridViewHouzzPriceUpdate.Rows[e.RowIndex];
    string updatePriceTo = ((TextBox)row.FindControl("TextBoxUpdatePriceTo")).Text;
    string Operation = ((DropDownList)row.FindControl("DropDownListOperation")).SelectedValue;
    string updateType = ((DropDownList)row.FindControl("DropDownUpdateType")).SelectedValue;
    //Reset the edit index.
    GridViewHouzzPriceUpdate.EditIndex = -1;
    //Bind data to the GridView control.
    bindGridViewHouzzPriceUpdate();
}

当我尝试更新行时,我无法从文本框和下拉列表中获取编辑后的值。 有人知道吗?

无法在 GridView 更新时获取文本框和下拉列表的值

绑定前的简单更新数据源。我添加了更新数据源自定义方法。您必须为该方法编写适当的代码。根据您的标准更新记录,您会没事的。

protected void GridViewHouzzPriceUpdate_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   GridViewRow row = (GridViewRow)GridViewHouzzPriceUpdate.Rows[e.RowIndex];
   string updatePriceTo = string.Empty;
   string Operation = string.Empty;
   string updateType = string.Empty;
   TextBox txtUpdatePriceTo = row.FindControl("TextBoxUpdatePriceTo") as TextBox;
   if(txtUpdatePriceTo != null)
     updatePriceTo = txtUpdatePriceTo.Text;
   DropDownList ddlOperation = row.FindControl("DropDownListOperation") as DropDownList;
   if(ddlOperation != null)           
    Operation = ddlOperation.SelectedValue;

   DropDownList ddlDropDownUpdateType = row.FindControl("DropDownUpdateType") as DropDownList;
   if(ddlDropDownUpdateType != null)           
    updateType = ddlDropDownUpdateType .SelectedValue;
   //Reset the edit index.
   GridViewHouzzPriceUpdate.EditIndex = -1;
  if(updatePriceTo != string.Empty && Operation != string.Empty && updateType != string.Empty)
   //update data source here 
   UpdateDataSource(updatePriceTo, Operation, updateType); 
  //Bind data to the GridView control.
  bindGridViewHouzzPriceUpdate();
}