datalist编辑不起作用?

时间:2012-07-20 11:01:12

标签: asp.net datalist

我正在研究数据列表项目编辑概念,我想我完成了所有代码正确但是当点击编辑按钮空白页面显示时请看到这个问题并发送任何错误和救赎建议请

HTML代码          

    <asp:DataList ID="DataList1" runat="server" DataKeyField="outlet_id" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" RepeatColumns="2"> 
        <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%#Eval("outlet_id") %>' ></asp:Label>
            <asp:Label ID="lbl" runat="server" Text='<%#Eval("company_name") %>' ></asp:Label>
            <asp:Button ID="edit" runat="server" Text="Edit" CommandName="edit"  />
            <asp:Button ID="delete" runat="server" Text="Del" CommandName="del" />
        </ItemTemplate>
        <EditItemTemplate>
        id<asp:TextBox ID="id" runat="server" Text='<%#Eval("outlet_id") %>'></asp:TextBox> 
        name<asp:TextBox ID="name" runat="server" Text='<%#Eval("company_name") %>'  ></asp:TextBox>
            <asp:Button ID="Button2" runat="server" CommandName="update" Text="update" />
            <asp:Button ID="Button3" runat="server" CommandName="cancel" Text="Cancel" />
        </EditItemTemplate>
    </asp:DataList>


    </form>
</body>

和c#代码在这里吼叫

页面加载

  protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bind();
    }

}

编辑命令代码

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataBind();

    }

取消命令代码

protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
    DataList1.EditItemIndex = -1;
    DataList1.DataBind();
}

这是数据主义绑定

protected void bind()
{
    SqlConnection con = new SqlConnection(strconnection);
    con.Open();
    string qry = "select top 10 outlet_id,company_name from outlet_table ";
    SqlDataAdapter Da = new SqlDataAdapter(qry, con);
    DataSet ds = new DataSet();
    Da.Fill(ds, "outlet");
    DataList1.DataSource = ds;
    DataList1.DataBind();
}

这是单击编辑按钮后显示的客户端html数据

<body>
<form name="form1" method="post" action="Demo.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="zVlNF4jkNpf9ZmIBtYaHSdSPa9l8zKVA4H0JXxnhqVytzfrbVWUuOuY8004hXYRY8KuBKqPKgV3kS4RQBegrJQ5LErL3VdV9TeluNoxAZLtDu0/62xR8Ij/jD638pPKb">
</div>

<div>

    <input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="">
</div>





    </form>


</body>

1 个答案:

答案 0 :(得分:1)

您需要将数据重新绑定到Datalist。

此:

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataBind();

    }

应该是这样的:

protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        bind();

    }

在回发后,Datalist丢失了数据源并再次需要它。