2011-01-09 138 views
0

我不是從編輯項目模板給我的代碼如下asp.net DataList控件更新

代碼隱藏能夠將數據綁定到項模板在DataList控件上更新command.I的點擊。

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標記。

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

我無法獲得患者對項目模板的價值。上面的代碼不工作。請幫助我。我可以這樣做嗎?

回答

0

單擊更新按鈕時ItemTemplate不存在。它會在下一次回發中出現。

您應該使用Update事件來更新數據庫。在下一個回傳中,將數據庫中的數據綁定到ItemList控件。