datalist自动增长列

本文介绍了一个使用ASP.NET进行数据绑定的例子,演示了如何创建一个数据表,并将其与UI元素如TextBox、DropDownList等进行绑定,同时展示了如何通过按钮事件来添加、删除数据表中的记录。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected System.Data.DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            BinHead();
        }
    }

    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        return default(string);
    }

    protected void OnSelect(object sender, EventArgs e)
    {
        TextBox1.Text = ((LinkButton)sender).Text;
    }

    protected void SdlItemsOnSelect(object sender, EventArgs e)
    {
    //    for (int i = 0; i < dt.Rows.Count - 1;i++ )
    //    {
    //        TextBox _TextBox1 = (TextBox)(SdlItems.Items[i].FindControl("TextBox1"));
    //        _TextBox1.Text = ((LinkButton)sender).Text; 
    //    }
        
    }

    protected void btnAdditem_Click(object sender, EventArgs e)
    {
        
        if (Session["DT"] != null)
        {
            dt = (DataTable)(Session["DT"]);
            DataRow dr = dt.NewRow();
           // dr["SN"] = dt.Rows.Count + 1;
            dr["SName"] = "";
            dr["SDescription"] = "";
            dr["SPrice"] = "";
            dr["SQuantity"] = "";
            dr["SSum"] = "";
            dt.Rows.Add(dr);
            dt.AcceptChanges();
       
        }

        //保存数据
        if (dt.Rows.Count > 1)
        {
            for (int i = 0; i < dt.Rows.Count - 1; i++)
            {
                TextBox tbSName = (TextBox)(SdlItems.Items[i].FindControl("tbSName"));
                TextBox tbSDescription = (TextBox)(SdlItems.Items[i].FindControl("tbSDescription"));
                TextBox tbSPrice = (TextBox)SdlItems.Items[i].FindControl("tbSPrice");
                DropDownList ddlDropDownList2 = (DropDownList)(SdlItems.Items[i].FindControl("DropDownList2"));
                TextBox tbSSum = (TextBox)(SdlItems.Items[i].FindControl("tbSSum"));

                string _tbSName = tbSName.Text;
                string _tbSDescription = tbSDescription.Text;
                string _tbSPrice = tbSPrice.Text;
                string _tbSQuantity = ddlDropDownList2.SelectedItem.Text;
                string _tbSSum = tbSSum.Text;

                dt.Rows[i]["SName"] = _tbSName;
                dt.Rows[i]["SDescription"] = _tbSDescription;
                dt.Rows[i]["SPrice"] = _tbSPrice;
                dt.Rows[i]["SQuantity"] = _tbSQuantity;
                dt.Rows[i]["SSum"] = _tbSSum;
                dt.Rows[i].AcceptChanges();
            }

           
        }
        Session["DT"] = dt;
        SdlItems.DataSource = dt;
        SdlItems.DataBind();
        for (int j = 0; j < dt.Rows.Count - 1; j++)
        {
            DropDownList ddlDropDownList2 = (DropDownList)(SdlItems.Items[j].FindControl("DropDownList2"));
            ddlDropDownList2.SelectedIndex = -1;
            ddlDropDownList2.Items.FindByText(dt.Rows[j]["SQuantity"].ToString()).Selected = true;
            TextBox tbSDescription = (TextBox)(SdlItems.Items[j].FindControl("tbSDescription"));
            
        }
       
    }
    private void BinHead()
    {
        dt.Columns.Add("SN");
        dt.Columns.Add("SName");
        dt.Columns.Add("SDescription");

        dt.Columns.Add("SPrice");
        dt.Columns.Add("SQuantity");
        dt.Columns.Add("SSum");
        dt.AcceptChanges();
        Session["DT"] = dt;
        SdlItems.DataSource = dt;
        SdlItems.DataBind();
    }

    protected void SdlItems_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        try
        {
            //SetXmlItemsInfo();
            DataTable dsTemp = Session["DT"] as DataTable;

            dsTemp.Rows.Remove(dsTemp.Rows[e.Item.ItemIndex]);

            Session["DT"] = dsTemp;
            SdlItems.DataSource = dsTemp;
            SdlItems.DataBind();
        }
        catch (System.Exception ex)
        {
            string error = ex.Message;
            Response.Write("<script>window.location.href=window.location.href</script>");
        }
    }
}

***************************HTML***************************

  <asp:datalist id="SdlItems" runat="server" Width="100%" CellSpacing="0" CellPadding="0" cssClass="tbl_border"

RepeatLayout="Flow" RepeatDirection="Horizontal" OnDeleteCommand="SdlItems_DeleteCommand" >

<HeaderTemplate>

<table class="tbl_border" cellpadding="0" cellspacing="0">

<tr class="tblTH">

<td>S/N</td>

<td>Item Name</td>

<td>Description</td>

<td>Unit Price</td>

<td>Quantity</td>

<td>Estimated Total</td>

<td>Del</td>

</tr>

</HeaderTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

<ItemTemplate>

<tr>

<td>

<%# Container.ItemIndex+1%>

</td>

<td>

<asp:TextBox  ID="tbSName" runat="server" Width="159px" BorderStyle="Groove" Text='<%# DataBinder.Eval(Container.DataItem, "SName")%>'  onmouseout="HideRemark()">

</asp:TextBox></td>

<td>

<asp:TextBox id="tbSDescription" runat="server" Width="290" BorderStyle="Groove" Text='<%# DataBinder.Eval(Container.DataItem, "SDescription")%>'>

</asp:TextBox></td>

<td>

<asp:TextBox id="tbSPrice" runat="server" CssClass="f_t_right" BorderStyle="Groove" Text='<%# DataBinder.Eval(Container.DataItem, "SPrice")%>' width="70px">

</asp:TextBox></td>

<td>                

                <asp:DropDownList ID="DropDownList2" runat="server">

            <asp:ListItem>1</asp:ListItem>

            <asp:ListItem>2</asp:ListItem>

            <asp:ListItem>3</asp:ListItem>

        </asp:DropDownList>

             </td>

      

<td>

<asp:TextBox id="tbSSum" runat="server" CssClass="tb_transparent" BorderStyle="None" EnableViewState="False" ReadOnly="True" Text='<%# DataBinder.Eval(Container.DataItem,"SSum")%>' width="80px">

</asp:TextBox></td>

<td>

<asp:ImageButton id="Imagebutton4" runat="server" BorderStyle="None" ImageUrl="~/del.gif"

CommandName="Delete" AlternateText="Delete"></asp:ImageButton></td>

</tr>

</ItemTemplate>

</asp:datalist>

 

转载于:https://www.cnblogs.com/AllenYang/archive/2008/11/26/1341678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值