ASP.NET中DataList数据绑定汇总

落爺英雄遲暮 提交于 2020-04-05 19:48:42

1、
a、前台aspx文件代码:
<asp:DataList ID="dataListDocument" runat="server" Width="400px">
                                <ItemTemplate>
                                    <table class="style1">
                                        <tr>
                                            <td class="style3" rowspan="2"><img src ="Images/Btn/icon_arrow_5.gif"/>
                                            </td>
                                            <td class="style7"><asp:Label ID="lblCommentMatter" runat="server" Text ='<%# GetShot(Eval("Title"))%>' ToolTip='<%# Eval("Title")%>'></asp:Label>
                                                </td>
                                            <td class="style5">
                                                评论:<asp:Label ID="lblCommentTime" runat="server" Text='<%#Get(Eval("CommentOnMatter"))  %>' ToolTip='<%# Eval("CommentOnMatter")%>'></asp:Label>
                                            </td>
                                            <td class="style6">
                                                <a href='..//?&amp;DocumentId=<%# DataBinder.Eval(Container.DataItem,"DocumentId")%>&Classify=<%# DataBinder.Eval(Container.DataItem,"Classify")%>&SmallClass=<%# DataBinder.Eval(Container.DataItem,"SmallClass")%>&UploadMemId=<%# DataBinder.Eval(Container.DataItem,"UploadMemId")%>'>
                                                查看</a></td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:DataList>
CSS格式约束
<style type="text/css">
        .styles1
        {
            width: 100%;
        }
        .style1
        {
            width: 100%;
            height: 22px;
            height:24px!important;
            font-size:12px; color: #0000FF;
            padding:2px 0 7px 0;
            padding:0px!important;
            padding-top:0px;
           
        }
        .style3
        {
            width: 11px;width: 12px!important;
        }
        .style5
        {width: 280px!important;}
        .style6
        {width: 25px!important;}
        .style7
        {
            width: 75px;
        }
        </style>

b、后台cs文件代码:
Document document = new Document();
                dataListDocument .DataSource = document.NewDocument(0);
                dataListDocument.DataBind();
//控制前台字符长度
 public string Get(object obj)
        {
            string str = obj.ToString();
            if (str.Length > 16)
            {
                return str = str.Substring(0, 16).ToString() + "....";
            }
            return str;
        }
        public string GetShot(object obj)
        {
            string str = obj.ToString();
            if (str.Length > 4)
            {
                return str = str.Substring(0, 4).ToString() + "....";
            }
            return str;
        }

3、图像路径读取
A.<asp:DataList ID="dataListNewCritique" runat="server" Width="560px">
                                <ItemTemplate>
                                            <td class="style3" rowspan="2"><asp:Image ID="imgPhoto" ImageUrl='<%# ImageS(DataBinder.Eval(Container.DataItem,"PhotoPath"))%>' runat="server" Height="17px" Width="15px" />
                                </ItemTemplate>
                            </asp:DataList>


B.<img src="<%#"../Images/MemPhotos/"+DataBinder.Eval(Container.DataItem, "PhotoPath") %>" width="50px" height="70px" />


//读取页面头像绑定
        protected string ImageS(object PBface)
        {
            if (PBface == null)
            {
                return "~/Images/MemPhotos/photo.gif";
            }
            else
            {
                try
                {
                    if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Images\\MemPhotos\\" + PBface))
                    {
                        return "~/Images/MemPhotos/" + PBface;
                    }
                    else
                    {
                        return "~/Images/MemPhotos/photo.gif";
                    }
                }
                catch
                {
                    return "~/Images/MemPhotos/photo.gif";
                }
            }
        }

<asp:DataList ID="dataListItemPross" runat="server">
                <ItemTemplate>
                    <div id="xianshi">
                        <div id="xianshiL">
                            <ul style="width: 270px; height: 44px; overflow: inherit;">
                                <li>步骤:<asp:Label ID="Label2" runat="server" Text='<%# Eval("ProcessId")%>'></asp:Label>
                                    &nbsp;事务名称:<asp:TextBox ID="TextBoxs" runat="server" ReadOnly="True" CssClass="tl" Text='<%# Eval("ItemName")%>' ></asp:TextBox>
                                </li>
                                <li>内容显示</li>
                            </ul>
                            <asp:TextBox ID="TextBoxp" runat="server" CssClass="xianshiL" TextMode="MultiLine" ReadOnly="True" Text='<%# Eval("ProcessMatter")%>' ></asp:TextBox>
                        </div>
                        <div id="xianshiR">
                            <dl>
                                &nbsp 参与者</dl>
                            <asp:TextBox ID="txtList" runat="server" Text='<%# ListShow(Eval("Participants"))%>' CssClass="Indexview"  ReadOnly="True"></asp:TextBox>
                        </div>
                    </div>
                    <div id="Indextextx">
                        <li>
                            <dl id="biaozhu">
                                负责人:</dl>
                            <asp:TextBox ID="txtprincipalIdx" runat="server" Text='<%# Eval("MemName")%>' CssClass="txtAll" ReadOnly="True"></asp:TextBox></li>
                        <li>
                            <dl id="biaozhu">
                                步骤编号:</dl>
                            <asp:TextBox ID="txtProcessIdx" runat="server" Text='<%# Eval("ProcessId")%>' CssClass="txtAll" ReadOnly="True"></asp:TextBox></li>
                        <li>
                            <dl id="biaozhu">
                                金 费:</dl>
                            <asp:TextBox ID="txtexpensesx" runat="server" Text='<%# Eval("Expenses")%>' CssClass="txtAll" ReadOnly="True"></asp:TextBox></li>
                        <li>
                            <dl id="biaozhu">
                                设备/场地:</dl>
                            <asp:TextBox ID="txtequipmentsx" runat="server" Text='<%# Eval("Equipments")%>' CssClass="txtAll" ReadOnly="True"></asp:TextBox></li>
                        <li>
                            <dl id="biaozhu">
                                审核信息:</dl>
                            <asp:TextBox ID="txtauditingInfox" runat="server" Text='<%# Eval("AuditingInfo")%>' CssClass="txtAll" TextMode="MultiLine" ReadOnly="True"></asp:TextBox></li>
                        <li>
                            <dl id="biaozhu">
                                处理期限:</dl>
                            <asp:TextBox ID="txtdisposeTimex" runat="server" Text='<%# Eval("DisposeTime")%>' CssClass="txtAll" ReadOnly="True"></asp:TextBox></li>
                    </div>
                    <div style="width: 400px; height: 60px; margin-left: 300px; _margin-left: 150px;
                        +margin-left: 150px; margin-top: 5px;">
                        <a href='AmendItemProcessWF1.aspx?ItemId=<%#ItemId() %>&ItemProcessId=<%#Eval("ItemProcessId")%>'><asp:Label ID="lblDel" runat="server" Text='<%# Amend(Eval("AuditingInfo"),Eval("PrincipalId"))%>'></asp:Label></a><%--修改--%>
                        <a href='IndexOnePlantItemProcessWF.aspx?ItemId="+<%#ItemId() %>+"&ItemProcessId="+<%#Eval("ItemProcessId")%>'><asp:Label ID="lblExam" runat="server" Text='<%# Exam()%>'></asp:Label></a><%--评论--%>
                        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                        <asp:Button ID="Button1" runat="server" Text="删除" CssClass="Indexbtqudian" />
                        <%--<asp:Button ID="Button2" runat="server" Text="评审" CssClass="Indexbtqudian" />
                        <asp:Button ID="Button3" runat="server" Text="修改" CssClass="Indexbtqudian" />--%>
                    </div>
                </ItemTemplate>
            </asp:DataList>

//asp:DataList 内部控件运用:
 protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=SIMS;Data Source=.
");
        string aa = "";
        foreach (DataListItem item in DataList1.Items)
        {
            Label id = (Label)item.FindControl("lbid");
            Label name = (Label)item.FindControl("lbname");
            TextBox cheng = (TextBox)item.FindControl("txtCheng");
            aa = "insert into xscj values(" + id.Text + ",'" + name.Text + "'," + cheng.Text + ")";
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(aa, conn);
                cmd.ExecuteNonQuery();
            }
            catch { Response.Write("<script>alert('添加失败 ')</script>"); }
            finally { conn.Close(); }
        }
        Response.Write("<script>alert('" + aa + "')</script>");
    }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!