2012-11-27 149 views
-1

当我选择从下拉列表的值,我喜欢这样的价值在于需要对asp.net下拉列表中选择价值

 InsertCommand="INSERT INTO [Companies] ([TimeZone]) VALUES (@TimeZone)" 

的@TimeZone值保存我不知道如何做到这一点。

这里是我的代码:

在我的页面加载,我有以下的代码分配值下拉:

ddlTimeZone.DataSource = from p in TimeZoneInfo.GetZones() 
          select new { p.Id }; 
    ddlTimeZone.DataTextField = "Id"; 
    ddlTimeZone.DataValueField = "Id";    
    ddlTimeZone.DataBind(); 

接下来,我的.aspx文件中,我有以下:

<EditItemTemplate> 
     <asp:DropDownList ID="ddlTimeZone" runat="server"> 
     </asp:DropDownList> 
    </EditItemTemplate> 

..........

InsertCommand="INSERT INTO [Companies] ([TimeZone]) VALUES (@TimeZone)" 

    <InsertParameters>     
     <asp:Parameter Name="TimeZone" Type="String" />   
    </InsertParameters> 

同样,我需要知道的是如何我选择的值绑定从下拉列表(ddlTimeZone)到@TimeZone

我想:

 <asp:DropDownList ID="ddlTimeZone" SelectedValue='<%# Bind("TimeZone") %>' runat="server"> 
     </asp:DropDownList> 

但没有奏效。请注意,我在GRIDVIEW中使用DropDownList。

回答

0

尝试这种方式

var command = new SqlCommand("INSERT INTO [Companies] ([TimeZone]) VALUES (@TimeZone)", connection); 
command.Parameters.Add(new SqlParameter("@TimeZone", valuetopass)); 
+0

我需要在aspx文件内做到这一点,因为我有其他20多个栏目,我也绑定。如何保存在aspx文件中而不是后面的代码 –

+0

如果您需要在'.aspx'页面中编写代码,请查看关于[在ASP.NET中使用内联代码](http:// www。 808.dk/?code-aspnet-inline) –