是否可以在不使用对象数据源的情况下强制网格视图编辑模式,我设置了EditIndex,但在我将绑定更改为ObjectDataSource之前,从不显示编辑模板
谢谢
答案 0 :(得分:0)
您的意思是设置Edit-Mode as default吗?因为您必须对gridview进行数据绑定,否则没有要显示的项目。
答案 1 :(得分:0)
我不知道你是说这样的例子,但我希望它会有所帮助:
<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
onrowcancelingedit="GridView1_RowCancelingEdit">
<Columns>
<asp:CommandField EditText="Edit" UpdateText="Update" ShowEditButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = ItemRepo.GetItemList();
GridView1.DataBind();
}
}
class Item
{
public string Name { get; set; }
}
class ItemRepo
{
public static List<Item> GetItemList()
{
List<Item> list = new List<Item>();
list.Add(new Item() { Name = "Item1" });
list.Add(new Item() { Name = "Item2" });
list.Add(new Item() { Name = "Item3" });
list.Add(new Item() { Name = "Item4" });
list.Add(new Item() { Name = "Item5" });
return list;
}
}
我认为在设置editindex之后你的问题可能不是数据绑定gridview。