0

我正在研究数据列表项编辑概念,我认为我已完成所有代码正确,但是当单击编辑按钮时显示空白页面,请查看此问题并发送任何错误和拯救建议

代码

    <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>
4

1 回答 1

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 会丢失其数据源并再次需要它。

于 2012-12-27T20:10:42.487 回答