我有项目模板,编辑项目模板,添加页脚模板,编辑更新记录,
当我插入时没有问题, 当我点击编辑按钮进行更新时,它不会在编辑项目模板字段中绑定我的项目模板值。
注意:文本框绑定没有问题,问题在下拉列表中没有绑定。
是否有可能像这样绑定
<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);
}
}
给我解决方案!..
答案 0 :(得分:0)
在每个编辑命令中,您需要将项目集合绑定到下拉列表(似乎当前正在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);
}
}
答案 1 :(得分:0)
<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);
}
}