简体   繁体   中英

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:

 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. 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". Any help is greatly appreciated!

I think your syntax is wrong for HiddenField value. 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")%>' />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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