繁体   English   中英

单击Asp.net C#中的“编辑”按钮时,“编辑项目模板”下拉字段中的值未绑定

[英]Value Not Binding In Edit Item Template Dropdown Field When Click Edit Button In Asp.net C#

我有项目模板,编辑项目模板,用于添加的页脚模板,编辑更新记录,

当我插入时没有问题,当我单击编辑按钮进行更新时,它不会在编辑项目模板字段中绑定我的项目模板值。

注意:文本框绑定没有问题,问题不在dropdownlist中。

有没有可能这样绑定

<asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="false" 
AppendDataBoundItems="true" Width="130px" CssClass="dropdown" 
DataTextField='<%# Eval("StatusName")%>'>
   <asp:ListItem Value="0" Text="--SELECT--"></asp:ListItem>
   <asp:ListItem Value="1" Text="NIL"></asp:ListItem>
   <asp:ListItem Value="2" Text="NOT YET TAKEN"></asp:ListItem>
   <asp:ListItem Value="3" Text="WORK IN PROGRESS"></asp:ListItem>
   <asp:ListItem Value="4" Text="COMPLETED"></asp:ListItem>
   <asp:ListItem Value="5" Text="UNDER TESTING"></asp:ListItem>
   <asp:ListItem Value="6" Text="NOT POSSIBLE"></asp:ListItem>
</asp:DropDownList>

像文字方块:

<asp:TextBox ID="txtStatusName" runat="server" Text='<%# Eval("StatusName")%>' 
CssClass="textbox" Width="140px"></asp:TextBox>

C#代码:

protected void gvwTask_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            gvwTask.EditIndex = e.NewEditIndex;
            Fill();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
        }
    }


protected void gvwTask_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        try
        {
            gvwTask.EditIndex = -1;
            Fill();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
        }
    }

填():

public void Fill()
    {
        try
        {
            DataSet dsTask = new DataSet("tblTask");
            dsTask = bolTask.SelectAllTask();
            if (dsTask.Tables[0].Rows.Count > 0)
            {

                gvwTask.DataSource = dsTask.Tables[0];
                gvwTask.DataBind();

            }
            else
            {
                gvwTask.DataSource = null;
                gvwTask.DataBind();
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
        }
    }

给我解决方案!

初始网格视图 在此处输入图片说明

在每个编辑命令期间,您需要将项目集合绑定到下拉菜单(似乎在Fill()方法中当前正在发生?),然后设置SelectedValue属性以在编辑过程中向用户显示所选项目。 假设下拉列表的名称是ddlWorkStatus,而不是此代码示例

protected void gvwTask_RowEditing(object sender, GridViewEditEventArgs e)
{
    try
    {
        gvwTask.EditIndex = e.NewEditIndex;
        Fill();
        var id = Convert.ToInt64(e.Keys[0]); // you need to set DataKeyNames attribute to receive identifier of selected row item in code behind. 
        var ddlWorkStatus = gvwTask.Rows[e.RowIndex].FindControl("ddlWorkStatus") as DropDownList;
        if(ddlWorkStatus != null)
        {
            //var fetchedObject = queried object based on id;
            ddlWorkStatus.SelectedValue = fetchedObject.StatusProperty;
        } 
     }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
    }
}   
 <asp:TemplateField HeaderText="Status Name">
  <ItemTemplate>
      <asp:Label ID="lblStatusName" runat="server" Text='<%# Eval("StatusName")%>' CssClass="label"
                                                        Width="130px"></asp:Label>
 </ItemTemplate>
<EditItemTemplate>                                                       
     <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("StatusID")%>' Visible = "false"></asp:Label>
     <asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="false" AppendDataBoundItems="true" Width="130px" CssClass="dropdown">
         <asp:ListItem Value="0" Text="--SELECT--"></asp:ListItem>
         <asp:ListItem Value="1" Text="NIL"></asp:ListItem>
         <asp:ListItem Value="2" Text="NOT YET TAKEN"></asp:ListItem>
         <asp:ListItem Value="3" Text="WORK IN PROGRESS"></asp:ListItem>
         <asp:ListItem Value="4" Text="COMPLETED"></asp:ListItem>
         <asp:ListItem Value="5" Text="UNDER TESTING"></asp:ListItem>
         <asp:ListItem Value="6" Text="NOT POSSIBLE"></asp:ListItem>
         <asp:ListItem Value="7" Text="HOLD"></asp:ListItem>
     </asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>                                      
     <asp:DropDownList ID="ddlStatus" runat="server" AutoPostBack="false" AppendDataBoundItems="true" Width="130px" CssClass="dropdown">
         <asp:ListItem Value="0" Text="--SELECT--"></asp:ListItem>
         <asp:ListItem Value="1" Text="NIL"></asp:ListItem>
         <asp:ListItem Value="2" Text="NOT YET TAKEN"></asp:ListItem>
         <asp:ListItem Value="3" Text="WORK IN PROGRESS"></asp:ListItem>
         <asp:ListItem Value="4" Text="COMPLETED"></asp:ListItem>
         <asp:ListItem Value="5" Text="UNDER TESTING"></asp:ListItem>
         <asp:ListItem Value="6" Text="NOT POSSIBLE"></asp:ListItem>
         <asp:ListItem Value="7" Text="HOLD"></asp:ListItem>
     </asp:DropDownList>
 </FooterTemplate>
  </asp:TemplateField>

C#代码:

protected void gvwTask_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {    
           if (e.Row.RowType == DataControlRowType.DataRow && gvwTask.EditIndex == e.Row.RowIndex)
            {
                DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus");
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
                string t = lblStatus.Text;
                ddlStatus.SelectedValue = t;
            }
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
        }
    }

暂无
暂无

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

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