【问题标题】:Drop Down List and Selected Value下拉列表和选定值
【发布时间】:2013-04-24 03:13:55
【问题描述】:

我很难弄清楚我在这里做错了什么。在我的编辑项目模板中,我的下拉列表有以下代码:

<asp:DropDownList ID="dd_is_active" runat="server" AppendDataBoundItems="true"
 DataValueField="Enabled">
 <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
 <asp:ListItem Text="No" Value="0"></asp:ListItem>
 </asp:DropDownList>
 <asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") %>' />

这是我的 aspx.cs 代码:

 protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
    e.Values["SUB_last_modified_date"] = DateTime.Now.ToString();
    e.Values["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
    e.Values["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();
    e.Values["Enabled"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_is_active")).SelectedValue;
    e.Values["Category_ID"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_category")).SelectedValue;
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    e.NewValues["SUB_last_modified_date"] = DateTime.Now.ToString();
    e.NewValues["SUB_last_modified_by_user_id"] = HttpContext.Current.User.Identity.Name;
    e.NewValues["SUB_last_modified_by_user_name"] = Session["UserName"].ToString();

}

似乎我的 .cs 代码中缺少某些内容,或者我在 html 代码中错误地绑定了 1 和 0 的值。此完全相同的代码适用于插入项目模板,但更新(或编辑项目模板)无法正常工作。

当我尝试编辑表格中的项目时,我收到一条错误消息,指出输入字符串的格式不正确。我知道它正在尝试绑定“是”或“否”的文本,但我需要找到“0”或“1”的值。非常感谢任何帮助!

【问题讨论】:

  • 是的。这是我与该字段相关的 C# 代码:protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e) { e.Values["Enabled"] = ((DropDownList)(sender as ListView).InsertItem.FindControl("dd_is_active"))。选定值;
  • 我对编码很陌生,所以请原谅我可能排除的任何信息。我的插入项目模板中有相同的代码,它工作得很好。
  • 能分享一下DropDownList中使用的SqlDataSource的SelectCommand的代码吗?
  • 您好。现在选项只有 "Yes" = 1 和 "No" = 0,它们设置为 asp:Listitem。更新表的 SQL 代码是“Update [myTable] Set [Enabled] = @Enabled Where [Sub_ID] = @Sub_Id”。该列是允许空值的 int 类型。我尝试将其更改为 tinyint not null 类型,但 Insert 选项不起作用。我应该使用 SQL 代码适当地提取 1 和 0 的值吗?感谢您的帮助!

标签: c# html


【解决方案1】:

我认为您的 HiddenField 值的语法是错误的。 而不是这个

<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled") '>' />

应该是

<asp:HiddenField ID="is_activeTextBox" runat="server" Value='<%# Bind("Enabled")%>' />

【讨论】:

  • 我尝试替换您的代码,但仍然收到“错误:输入字符串格式不正确”。
  • 我见过有人使用看起来更像这样的代码: 但是我的智能感知甚至没有提供“SelectedValue”的选项,所以当我这样运行时,我收到一条错误消息,指出 SelectedValue 无效 bc 它不存在于项目列表中
  • 你在 Listview 中使用这个 DropDownList 吗??
  • 是的,我将它标记为 ListView1
  • 你能详细解释一下你的问题到底是什么吗?你有什么要求?
猜你喜欢
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多