2011-02-14 42 views
1

时我如何获得一个DropDownList的“@”的价值我有一个标准的DataGrid,看起来像这样:更新一个DataGrid

<asp:GridView id="MyGridView" 
    DataSourceID="MyDataSource1" 
    AutoGenerateColumns="false" 
    AutoGenerateEditButton="true" 
    DataKeyNames="Id" 

    Runat="Server"> 

    <Columns> 

    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 


    <asp:TemplateField HeaderText="State"> 
    <ItemTemplate> 
     <asp:Label ID="Label1" runat="server" Text='<%# Bind("State") %>'></asp:Label> 
    </ItemTemplate> 

    <EditItemTemplate> 
     <asp:DropDownList ID="IdState1" DataSource='<%# GetCategoryNames() %>' DataTextField="State" DataValueField="State" runat="server"></asp:DropDownList> 
    </EditItemTemplate> 

    </asp:TemplateField> 

在更新网格我有一个看起来像这样的更新命令:

UpdateCommand= "UPDATE [MauriceBlackburnOffices] SET [Name] = @Name, [Address1] = @Address1, [TheStates] = @State WHERE [Id] = @Id" 

但@State字段无法识别。

Must declare the scalar variable "@State". 

@值应该是什么?

如何将DropDownList值更新到更新语句中?

回答

3

我会猜测你正在使用SQLDataSource对象来填充GridView。如果是这样,你可能没有设置UpdateParameters(MSDN Link)。

更新您的SqlDataSource如下(你需要稍微修改为您的代码):

<asp:SqlDataSource 
      id="MyDataSource1" 
      runat="server" 
      ConnectionString="MyDataConnectionString" 
      SelectCommand="SELECT * FROM Table" 
      UpdateCommand="UPDATE [MauriceBlackburnOffices] SET [Name] = @Name, [Address1] = @Address1, [TheStates] = @State WHERE [Id] = @Id"> 
      <UpdateParameters> 
       <asp:ControlParameter Name="Name" ControlId="NameControl" PropertyName="Text"/> 
       <asp:ControlParameter Name="AddressID" ControlId="AddressControl" PropertyName="SelectedValue"/> 
<asp:ControlParameter Name="Name" ControlId="StateControl" PropertyName="SelectedValue"/> 
       <asp:ControlParameter Name="ID" ControlId="IDControl" PropertyName="SelectedValue"/> 
      </UpdateParameters> 
     </asp:SqlDataSource> 
1

实际上,你可以做到这一点,而不UpdateParameters

通过在完整的源仔细观察这里http://msdn.microsoft.com/en-us/library/ms972948.aspx我发现了这个问题。

其真实的奇怪。

在哪里我说:

[TheStates] = @State 

“国家”必须是EditItemTemplate中的价值的SelectedValue,并必须由“绑定”的方式进行连接。

绑定(“状态”)创建@State。

0

试试这个

<asp:DropDownList ID="IdState1" runat="server" DataTextField="State" DataValueField="State" SelectedValue='<%# Bind("State") %>'></asp:DropDownList>