简体   繁体   English

GridView行更新和DropList

[英]GridView row update and DropList

I have a gridview with column that have droplist when enter edit mode: 当进入编辑模式时,我的gridview的列具有下拉列表:

<asp:TemplateField HeaderText="genre" SortExpression="genre">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList2" runat="server" 
                            DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("genre") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

Now i want to take it to UpdateParameters of SqlDataSource : 现在我想把它带到SqlDataSource UpdateParameters

    <UpdateParameters>
            <asp:ControlParameter ControlID="DropDownList2" Type="string" PropertyName="SelectedValue" Name="genre" />
    </UpdateParameters>

But when i pressed he give me Error msg 但是当我按下他给我错误消息

Could not find control 'DropDownList2' in ControlParameter 'genre'. 

Any idea why? 知道为什么吗?

DropDownList2 is a nested control located under the Grid; DropDownList2是位于Grid下面的嵌套控件; therefore, your SqlDataSource control does not have visibility at all of the DropDownList2. 因此,您的SqlDataSource控件在所有DropDownList2上都不可见。

You could try assigning the value on the code behind by using the Updating event: 您可以尝试使用Updating事件在后面的代码上分配值:

protected void SqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
  e.Command.Parameters["@genre"].Value = GetDropDownListValue();
}

Note: You will need to use FindControl("DropDownList2") in GetDropDownListValue() 注意:您将需要在GetDropDownListValue()中使用FindControl(“ DropDownList2”)

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

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