翻出以前的asp.net学习笔记,发现很多有关Gridview中的数据绑定使用,发上来大家看看,新手参考(控件党神器,嘿嘿。。。),高手莫笑!
1、Gridview设置时间格式
- <asp:BoundField DataField="Adddate" DataFormatString="{0:d}" HeaderText="时间"/>
2、绑定后台函数
- <asp:TemplateField HeaderText="最后回复">
- <ItemTemplate>
- <%# Convert.ToInt32(Eval("LastReplyId")) == 0 ? "暂无回复" : getUsername(Eval("LastReplyId").ToString())%>
- </ItemTemplate>
- <HeaderStyle Height="20px" Width="20%"/>
- <ItemStyle Height="30px" HorizontalAlign="Center"/>
- </asp:TemplateField>
- public string getUsername(string ReplyId)
- {
- return dt.Rows[0][1].ToString() +" by:" + dt.Rows[0][0].ToString();
- }
3、页面数据绑定
- <asp:LinkButtonID="lbModify" runat="server" PostBackUrl='<%# "~/modifyGoods.aspx?id="+Eval("id") %>'>修改</asp:LinkButton>
- <asp:LabelID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("istop"))==0?"是":"否" %>'></asp:Label>
- <asp:LabelID="Label2" runat="server" Text='<%# Convert.ToDateTime(Eval("creatime")).ToString("yyyy-MM-dd") %>'></asp:Label>
- <a href="notice.aspx?id=<%# Eval("id") %>"><%# Eval("title").ToString().Length>9?Eval("title").ToString().Substring(0,8)+"..":Eval("title").ToString() %></a>
4、gridview中linkbutton绑定CommandArgument
- <asp:TemplateField HeaderText="操作">
- <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"/>
- <HeaderStyle Font-Bold="False" Width="90px"/>
- <ItemTemplate>
- <asp:LinkButtonID="lbModify" runat="server" CommandArgument='<%# Eval("id") %>'
- oncommand="lbModify_Command">取消授权</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- string id = e.CommandArgument.ToString();
5、绑定嵌套三目运算符
- <asp:TemplateField HeaderText="购买方式">
- <HeaderStyle Font-Bold="False" Width="50px"/>
- <ItemStyle HorizontalAlign="Center"/>
- <ItemTemplate>
- <asp:LabelID="Label1" runat="server" Text='<%# Convert.ToInt32(Eval("buyType"))==0?"按天购买":Convert.ToInt32(Eval("buyType"))==1?"按次购买":"整部购买" %>'></asp:Label>
- </ItemTemplate>
- <FooterStyle BorderColor="#D6EFF8" BorderStyle="Solid"/>
- </asp:TemplateField>
6、Gridview设置货币格式
- <asp:BoundField DataField="price" DataFormatString="{0:C}" HeaderText="金额">
- <HeaderStyle Font-Bold="False" Width="45px"/>
- <ItemStyle HorizontalAlign="Center"/>
- <FooterStyle BorderColor="#D6EFF8" BorderStyle="Solid"/>
- </asp:BoundField>
7、绑定javascript函数与参数
- <asp:TemplateField HeaderText="查看">
- <ItemTemplate>
- <ahref="javascript:void(0)" onclick='<%# "openWindow(\""+ Eval("empId").ToString() +"\")" %>' id="btn-check">
- 查看</a>
- </ItemTemplate>
- <ItemStyle Width="40px"/>
- </asp:TemplateField>
先这么多,有点乱,凑合看吧!