怎么通过button 来更改Lable的值
- C# code
<asp:GridView ID="gvSelect" runat="server" AutoGenerateColumns="False" DataKeyNames="Pro_Id" onrowdeleting="gvSelect_RowDeleting" onrowdatabound="gvSelect_RowDataBound" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" Width="1000px" AllowPaging="True" AllowSorting="True" ClientIDMode="Static" PageIndex="0" PageSize="5" onpageindexchanging="gvSelect_PageIndexChanging" onrowediting="gvSelect_RowEditing" onrowcreated="gvSelect_RowCreated" onrowcommand="gvSelect_RowCommand" > <AlternatingRowStyle BackColor="PaleGoldenrod" /> <Columns> <asp:TemplateField HeaderText="图片"> <ItemTemplate> <img src='img/<%# Eval("Pro_Image") %>' width="50" height="50" alt="" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Pro_Name" HeaderText="名称" SortExpression="Pro_Name" /> <asp:BoundField DataField="Pro_Price" HeaderText="价格" SortExpression="Pro_Price" DataFormatString="{0:f2}"/> <asp:TemplateField HeaderText="数量"> <ItemTemplate> <asp:Button ID="jian" CommandArgument='<%# Eval("Pro_Id") %>' CommandName="jian" runat="server" Text="-" /> <asp:Label ID="Num" runat="server" Text="1"></asp:Label> <asp:Button ID="jia" CommandArgument='<%# Eval("Pro_Id") %>' CommandName="jia" runat="server" Text="+" /> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" DeleteText="取消" HeaderText="取消" /> </Columns> <FooterStyle BackColor="Tan" /> <HeaderStyle BackColor="Tan" Font-Bold="True" /> <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> <SortedAscendingCellStyle BackColor="#FAFAE7" /> <SortedAscendingHeaderStyle BackColor="#DAC09E" /> <SortedDescendingCellStyle BackColor="#EDA612" /> <SortedDescendingHeaderStyle BackColor="#EDA612" /> </asp:GridView>
[解决办法]
我不知道你是怎么测试的,如果你没有看懂方法,你可以直接把下面的内容全部拷贝粘贴到
x.aspx里面,然后直接浏览 x.aspx自己不要做任何改动
- HTML code
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { System.Data.DataSet ds = new System.Data.DataSet(); System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser"); System.Data.DataRow dr; dataTable1.Columns.Add(new System.Data.DataColumn("Pro_Id", typeof(System.Int32))); dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String))); dr = dataTable1.Rows.Add(new Object[] { 1, "【孟子E章】" + 1.ToString() }); dr = dataTable1.Rows.Add(new Object[] { 2, "【孟子E章】" + 2.ToString() }); dr = dataTable1.Rows.Add(new Object[] { 3, "【孟子E章】" + 3.ToString() }); dr = dataTable1.Rows.Add(new Object[] { 4, "【孟子E章】" + 4.ToString() }); dr = dataTable1.Rows.Add(new Object[] { 5, "【孟子E章】" + 5.ToString() }); GridView1.DataSource = dataTable1; GridView1.DataBind(); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "jian") { Button x = e.CommandSource as Button; Label sm = x.NamingContainer.FindControl("Num") as Label; int old = 0; int.TryParse(sm.Text, out old); sm.Text = (old - 1).ToString(); } if (e.CommandName == "jia") { Button x = e.CommandSource as Button; Label sm = x.NamingContainer.FindControl("Num") as Label; int old = 0; int.TryParse(sm.Text, out old); sm.Text = (old + 1).ToString(); } }</script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>dddd</title></head><body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Button ID="jian" CommandArgument='<%# Eval("Pro_Id") %>' CommandName="jian" runat="server" Text="-" /> <asp:Label ID="Num" runat="server" Text='1'></asp:Label> <asp:Button ID="jia" CommandArgument='<%# Eval("Pro_Id") %>' CommandName="jia" runat="server" Text="+" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </form></body></html>