简体   繁体   English

如何在GridView RowUpdating事件中显示原始DropDownList所选项目

[英]How to show original DropDownList selected items in GridView RowUpdating event

I have GridView columns which I'd like to update using RowUpdating event. 我有GridView列,我想使用RowUpdating事件进行更新。 To do that, I use a dropdownlist in the GridView EditItemTemplate and it works perfectly. 为此,我在GridView EditItemTemplate中使用了一个下拉列表,它可以完美运行。

However there's a problem when I enter GridView edit mode; 但是,当我进入GridView编辑模式时会出现问题。 the selected item in dropdownlist is showing its default value, not the previously selected items which is stored in the database. 下拉列表中的选定项目将显示其默认值,而不是存储在数据库中的先前选定项目。 So if a user is editing a different column & overlook the dropdownlist , the GridView column (with the dropdownlist in EditItemTemplate) will be wrongly updated to the dropdownlist default value. 因此,如果用户正在编辑其他列并忽略下拉列表,则GridView列(在EditItemTemplate中带有下拉列表)将错误地更新为下拉列表的默认值。

In other words, as I enter GridView edit mode I want the dropdownlist to show (or select) a value binded to my SQL database and not showing its default or first value. 换句话说,当我进入GridView编辑模式时,我希望下拉列表显示(或选择)绑定到我的SQL数据库的值,而不显示其默认值或第一个值。 I tried using <%# Bind("zone") %>' which works for a textbox but it won't work for dropdownlist. 我尝试使用<%#Bind(“ zone”)%>'对文本框有效,但对dropdownlist无效。 Any idea? 任何想法?

Here's my GridView code: 这是我的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>

Code behind: 后面的代码:

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();
}

Thanks in advance :-) 提前致谢 :-)

If your list is always static this should work. 如果您的列表始终是静态的,则应该可以使用。

<asp:DropDownList ID="ddlZone" runat="server" Width="80px" SelectedValue='<%# Bind("zone") %>'>

If for some reason it doesn't then you can try setting the selected value from code behind OnDataRowBound event. 如果由于某种原因而不是,那么您可以尝试从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
        }
    }

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

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