Edit(Update,Cancel) ,Delete in DataList webcontrol

來源:互聯網
上載者:User
1
   we  must set different template of the datalist object  as diffirent appearance, such as itemplate,editplate,headertemplate,footertemplate. in edit template, Update function should have the Update command name as 'Update',and the same to Delete command. At the same time , we 'd better set the datakeyfield of the datalist so as to pick one item's key value.
2
   for different command,writing concerned codes.such as datalist1_editcomamnd,datalist1_updatecommand,datalist1_deletecommand

3 examples:
///this is a datalist object html code for its appearance
  <asp:DataList ID="DataList1"  DataKeyField="UserName" runat="server" Height="129px" Width="775px" OnItemCreated="DataList1_ItemCreated" GridLines="Both" OnDeleteCommand="DataList1_DeleteCommand" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand"   OnItemCreated="DataList1_ItemCreated">
       <ItemTemplate>
       <asp:Table  BorderWidth=0px BorderColor=Brown   CellPadding=0 CellSpacing=0 runat=server Height=20 ID="tablefuck">
       <asp:TableRow Width=800px Height=100%>
       <asp:TableCell Width=200px HorizontalAlign=center><%#DataBinder.Eval(Container.DataItem,"UserName")%></asp:TableCell>
        <asp:TableCell Width=300px  HorizontalAlign=center><%#DataBinder.Eval(Container.DataItem,"Site")%></asp:TableCell>
        <asp:TableCell Width=300px  HorizontalAlign=center><%#DataBinder.Eval(Container.DataItem,"Planner")%></asp:TableCell>
       <asp:TableCell Width=300px  HorizontalAlign=center>
       <asp:LinkButton Text="Edit" runat=server CommandName="Edit" 
>
   <asp:LinkButton Text="Update" runat=server CommandName="Update" 
>
</asp:LinkButton></asp:TableCell>
       </asp:TableRow>
       </asp:Table>
   
       </ItemTemplate>
            <EditItemTemplate>
            <asp:Table runat=server  ID="table1" Height="36px">
           
            <asp:TableRow Height=100% runat=server>
             <asp:TableCell runat=server Width=200px HorizontalAlign=Center><%# DataBinder.Eval(Container.DataItem,"UserName") %></asp:TableCell>
              <asp:TableCell runat=server Width=300px HorizontalAlign=Center>
             <asp:TextBox runat=server  ID="tb_Site"  Text='<%# DataBinder.Eval(Container.DataItem,"Site") %>'></asp:TextBox>
              </asp:TableCell>
             <asp:TableCell runat=server Width=300px HorizontalAlign=Center>
            <asp:TextBox runat=server ID="tb_Planner"  Text='<%# DataBinder.Eval(Container.DataItem,"Planner") %>' />
             </asp:TableCell>
            <asp:TableCell runat=server Width=300px HorizontalAlign=Center>
             <asp:LinkButton Text="Update  " CommandName="Update" runat=server  ID="lb_Update" />
            <asp:LinkButton Text="  Cancel" CommandName=" Cancel" runat=server /> 
            </asp:TableCell>
            </asp:TableRow>
            </asp:Table>
              
            </EditItemTemplate>
            <HeaderTemplate>
           
              <asp:Table runat=server ID=fuck CellPadding="0" CellSpacing="0" Height="22px" BackColor=black ForeColor=white>
             <asp:TableRow Width=800px BorderWidth=0px runat="server" >
            <asp:TableCell  Width=200px  runat=server  HorizontalAlign=Center >UserName</asp:TableCell>
            <asp:TableCell  Width=300px  runat=server  HorizontalAlign=Center>Site</asp:TableCell>
            <asp:TableCell  Width=300px runat=server  HorizontalAlign=Center>Planner</asp:TableCell>
             <asp:TableCell ID="TableCell1"  Width=300px runat=server  HorizontalAlign=Center>Operation</asp:TableCell>
             </asp:TableRow>
            
              </asp:Table>
            </HeaderTemplate>
          
        </asp:DataList></div>

///corresponding codes for edit(update,cancel),delete

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

private void RePaintAllControl(int itemIndex)
    {
#if HRG_VERSION
//給contronl增加屬性對話方塊
        ImageButton lb;
        lb = (ImageButton)DataList1.Items[itemIndex].FindControl("lb_Update");
        System.Diagnostics.Debug.Assert(lb != null);
        lb.Attributes.Add("onclick", "return confirm('Are you sure to update?')");
        TextBox ddl;

        ddl = (TextBox)DataList1.Items[itemIndex].FindControl("tb_Planner");
        ddl = (DropDownList)DataList1.Items[itemIndex].FindControl("ddl_category");
            ........
            .......
        #endif
    }

protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    {
       // Response.Write(e.Item.ItemIndex);
        ImageButton ib;
        ib = (ImageButton)e.Item.FindControl("ib_Delete");
        if (ib != null)
        {
            ib.Attributes.Add("onclick", "return confirm('Are you sure to delete the bug which Id is "+DataList1.DataKeys[e.Item.ItemIndex]+"')");
        }
    }

   protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        Response.Write("<script>alert(\"Delete comamd\")</script>");
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        connOpen();
        SqlCommand cmd = new SqlCommand("Pr_UpdateUser", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        Response.Write(((TextBox)e.Item.FindControl("tb_Site")).Text.Trim());

        SqlParameter username = new SqlParameter("@UserName", SqlDbType.VarChar, 20);
        username.Value = DataList1.DataKeys[e.Item.ItemIndex].ToString().Trim();
        cmd.Parameters.Add(username);

        SqlParameter site = new SqlParameter("@Site", SqlDbType.VarChar, 10);
        site.Value = ((TextBox)e.Item.FindControl("tb_Site")).Text.Trim();
        cmd.Parameters.Add(site);

        SqlParameter planner = new SqlParameter("@Planner", SqlDbType.VarChar, 10);
        planner.Value = ((TextBox)e.Item.FindControl("tb_Planner")).Text.Trim();
        cmd.Parameters.Add(planner);

        cmd.ExecuteNonQuery();
        DataList1.EditItemIndex = -1;
        DataBind();
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.