zoukankan      html  css  js  c++  java
  • Edit(Update,Cancel) ,Delete in DataList webcontrol

    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();
        }


  • 相关阅读:
    郎咸平 马行空 - 郎咸平说:萧条下的希望(2014年10月25日)
    豆豆 - 遥远的救世主(2014年10月18日)
    高铭 - 天才在左 疯子在右(2014年10月3日)
    张庭斌 艾经纬 - 中国富人为何变穷:金融危机攻略(2014年5月20日)
    王晋康 - 终极爆炸 ▪ 王晋康科幻小说精选集3(2014年4月24日)
    刘慈欣 - 乡村教师 ▪ 刘慈欣科幻自选集(2014年4月2日)
    王晋康 - 替天行道 ▪ 王晋康科幻小说精选集2(2014年3月27日)
    易之 - 我是个算命先生(2014年3月23日)
    宋鸿兵 - 货币战争5(2014年3月20日)
    【英】阿瑟 ▪ 克拉克 - 神的九十九亿个名字(2014年3月16日)
  • 原文地址:https://www.cnblogs.com/Winston/p/1027739.html
Copyright © 2011-2022 走看看