简体   繁体   English

下拉列表和选定值

[英]Drop Down List and Selected Value

I'm having a very hard time figuring out what I'm doing wrong here. 我很难搞清楚我在这里做错了什么。 In my Edit Item Template I have the following code for my drop down list: 在我的编辑项模板中,我的下拉列表中包含以下代码:

<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") %>' />

Here is my aspx.cs code: 这是我的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();

}

It seems something is either missing from my .cs code or I have the values of 1 and 0 bound incorrectly in the html code. 似乎我的.cs代码中缺少某些内容,或者我在html代码中错误地绑定了1和0的值。 This exact same code works for the Insert Item Template, but the Update (or Edit Item Template) is not working correctly. 这个完全相同的代码适用于插入项模板,但更新(或编辑项模板)无法正常工作。

When I try to edit an item in my table I get an error stating the input string is in an incorrect format. 当我尝试编辑表中的项目时,我收到一条错误,指出输入字符串的格式不正确。 I know it's trying to bind the Text of "Yes" or "No" but I need to ind to the Values of either "0" or "1". 我知道它试图绑定“是”或“否”的文本,但我需要指向“0”或“1”的值。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

I think your syntax is wrong for HiddenField value. 我认为你的语法对于HiddenField值是错误的。 Instead of this 而不是这个

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

It should be 它应该是

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

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

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