【问题标题】:How to show original DropDownList selected items in GridView RowUpdating event如何在 GridView RowUpdating 事件中显示原始 DropDownList 选定项目
【发布时间】:2018-11-05 10:40:10
【问题描述】:

我有想要使用 RowUpdating 事件更新的 GridView 列。为此,我在 GridView EditItemTemplate 中使用了一个下拉列表,它运行良好。

但是当我进入 GridView 编辑模式时出现问题;下拉列表中的选定项目显示其默认值,而不是存储在数据库中的先前选定项目。因此,如果用户正在编辑不同的列并忽略 dropdownlist ,则 GridView 列(带有 EditItemTemplate 中的下拉列表)将错误地更新为下拉列表默认值。

换句话说,当我进入 GridView 编辑模式时,我希望下拉列表显示(或选择)绑定到我的 SQL 数据库的值,而不显示其默认值或第一个值。我尝试使用适用于文本框但不适用于下拉列表的 '。有什么想法吗?

这是我的 GridView 代码:

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
    DataKeyNames="ID" AllowSorting="False"  
    onrowediting="GridView1_RowEditing"
    onrowcancelingedit="GridView1_RowCancelingEdit"
    onrowdatabound="GridView1_RowDataBound"
    onrowupdating="GridView1_RowUpdating"
    EnableViewState="False">
    <Columns>
        <asp:TemplateField HeaderText="Zone" SortExpression="Zone">
            <EditItemTemplate>         
                <asp:DropDownList ID="ddlZone" runat="server" Width="80px">
                    <asp:ListItem Value="zone1">Zone 1</asp:ListItem>
                    <asp:ListItem Value="zone2">Zone 2</asp:ListItem>
                    <asp:ListItem Value="zone3">Zone 3</asp:ListItem>
                </asp:DropDownList>                        
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblZone" runat="server" Text='<%# Bind("zone") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Subzone" SortExpression="Subzone">
            <EditItemTemplate>
                <asp:TextBox ID="txtSubzone" runat="server" Text='<%# Bind("subzone") %>' Width="100px"></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblSubzone" runat="server" Text='<%# Bind("subzone") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

后面的代码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    string textZone = (GridView1.Rows[e.RowIndex].FindControl("ddlZone") as DropDownList).SelectedItem.Value;
    string textSubzone = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSubzone")).Text;



    string query = "UPDATE tblRegion SET zone = '" + textZone
        + "', subzone ='" + Subzone"
        + "' WHERE (ID ='" + entryID + "')";

    SqlCommand sqlCmd = new SqlCommand(query, conn);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

    conn.Open();
    DataTable dt = new DataTable();
    sqlDa.Fill(dt);
    conn.Close();
}

提前致谢:-)

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    如果您的列表始终是静态的,这应该可以。

    &lt;asp:DropDownList ID="ddlZone" runat="server" Width="80px" SelectedValue='&lt;%# Bind("zone") %&gt;'&gt;

    如果由于某种原因没有,那么您可以尝试从 OnDataRowBound 事件后面的代码中设置所选值。

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState == DataControlRowState.Edit)
            {
                var ddl = (DropDownList) e.Row.FindControl("ddlZone");
                ddl.SelectedValue = "zone1"; //Your logic to bind the requiered index or value
            }
        }
    

    【讨论】:

    • 我使用了第一个建议,因为我的列表是静态的并且它有效!不能做本该很容易的事情,我觉得很傻。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多