asp.net datalist更新

时间:2011-01-09 06:08:09

标签: asp.net

点击更新命令,我无法将数据从编辑项目模板绑定到数据列表控件中的项目模板。我给出了下面的代码

代码隐藏。

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{

    DataTable dt = (DataTable)Session["time"];

    string patient = ((TextBox)e.Item.FindControl("TbxPatient")).Text;

    ((Label)e.Item.FindControl("LblPatient")).Text = patient;

    DataList1.EditItemIndex = -1;

    DataList1.DataSource = dt;

    DataList1.DataBind();
}

ASPX Markup。

<asp:DataList ID="DataList1" runat="server" Caption="Doctor1" OnEditCommand="DataList1_EditCommand"
        BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
        CellPadding="3" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
        Font-Underline="False" ForeColor="Purple" GridLines="Vertical" OnCancelCommand="DataList1_CancelCommand"
        OnItemDataBound="DataList1_ItemDataBound" OnSelectedIndexChanged="DataList1_SelectedIndexChanged"
        OnUpdateCommand="DataList1_UpdateCommand">
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <AlternatingItemStyle BackColor="#DCDCDC" />
        <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
        <EditItemTemplate>
            <table style="width: 100%;">
                <tr>
                    <td>
                        Reg No
                    </td>
                    <td>
                        <asp:TextBox ID="TbxRegNo" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td>
                        Patient
                    </td>
                    <td>
                        <asp:TextBox ID="TbxPatient" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td>
                        Compliats
                    </td>
                    <td>
                        <asp:TextBox ID="TbxCompliant" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td>
                        Mob.No
                    </td>
                    <td>
                        <asp:TextBox ID="TbxNo" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:LinkButton ID="button2" runat="server" CommandName="update" OnClick="button2_Click"
                            Text="Update" />
                        <asp:LinkButton ID="button3" runat="server" CommandName="Cancel" Text="Cancel" />
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
            </table>
        </EditItemTemplate>
        <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <ItemTemplate>
            <br />
            <table style="width: 100%;">
                <tr>
                    <td colspan="3">
                        <asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"time") %>'></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:Label ID="LblPatient" runat="server"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        &nbsp;
                    </td>
                    <td>
                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="select">Select</asp:LinkButton>
                        <asp:LinkButton ID="button3" runat="server" CommandName="edit" Text="Edit" />
                    </td>
                    <td>
                        &nbsp;
                    </td>
                </tr>
            </table>
        </ItemTemplate>
</asp:DataList>

我无法获得患者对项目模板的价值。上面的代码无效。请帮助我。我可以这样做吗?

1 个答案:

答案 0 :(得分:0)

单击“更新”按钮时,ItemTemplate不存在。它将在下一个PostBack上出现。

您应该使用Update事件来更新数据库。在下一个回发中,将数据从数据库数据绑定到ItemList控件。