怎么通过button 来更改Lable的值

hxlisgod 2012-09-24 02:41:57

<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>
...全文
212 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
孟子E章 2012-09-24
  • 打赏
  • 举报
回复
我不知道你是怎么测试的,如果你没有看懂方法,你可以直接把下面的内容全部拷贝粘贴到
x.aspx里面,然后直接浏览 x.aspx自己不要做任何改动

<%@ 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>
hxlisgod 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

得到的方法

if (e.CommandName == "jia")
{
Button x = e.CommandSource as Button;
Label sm = x.NamingContainer.FindControl("Num") as Label;
Session["Cart"]=dt[/Quote]
还是第一行在加...
SeanDing1989 2012-09-24
  • 打赏
  • 举报
回复
string lable = ((Label)gvSelect.Rows[gvSelect.EditIndex].FindControl("你Lable的Id")).Text;
孟子E章 2012-09-24
  • 打赏
  • 举报
回复
得到的方法

if (e.CommandName == "jia")
{
Button x = e.CommandSource as Button;
Label sm = x.NamingContainer.FindControl("Num") as Label;
Response.Write("你点击行的Label值" + sm.Text);
}
孟子E章 2012-09-24
  • 打赏
  • 举报
回复
给你一个完整的例子
拷贝粘贴看效果
<%@ Page Language="C#" EnableViewState="true" AutoEventWireup="true" %>

<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public String id = "";
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;
Response.Write("你点击行的Label值" + sm.Text);
}

if (e.CommandName == "jia")
{
Button x = e.CommandSource as Button;
Label sm = x.NamingContainer.FindControl("Num") as Label;
Response.Write("你点击行的Label值" + sm.Text);
}
}
</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='<%# Eval("UserName")%>'></asp:Label>
<asp:Button ID="jia" CommandArgument='<%# Eval("Pro_Id") %>' CommandName="jia" runat="server"
Text="+" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>

hxlisgod 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

rt.Items.Count是1

那你为什么要使用rt.Items.Count
gvSelect.Rows[rt.Items.Count]
[/Quote]
因为我想获得 这一行Lable里的值- -
aluogang 2012-09-24
  • 打赏
  • 举报
回复
哦,应该是 string num = lbl.Text - 1.
aluogang 2012-09-24
  • 打赏
  • 举报
回复
<asp:Button ID="jian" CommandArgument='<%# Container.DataItemIndex %>' CommandName="jian" runat="server" Text="-" />
protected void gvSelect_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "jian")
{
Label lbl= (Label)gvSelect.Rows[Convert.ToInt32(e.CommandArgument.ToString())].FindControl("Num"));
lbl.Enabled = true;
string num = lbl.Text + 1;
}
}

孟子E章 2012-09-24
  • 打赏
  • 举报
回复
rt.Items.Count是1

那你为什么要使用rt.Items.Count
gvSelect.Rows[rt.Items.Count]
hxlisgod 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

引用 3 楼 的回复:

引用 2 楼 的回复:

我永远是第一行的lable在加1
rt.Items.Count你这个就是一个定值,不是当前行的值。当然有问题
[/Quote]
那我应该??
hxlisgod 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

rt.Items.Count是多少?
rt又是什么对象?
[/Quote]
Repeater rt = new Repeater();
rt.Items.Count是1
我就是大神 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:

我永远是第一行的lable在加1
[/Quote]rt.Items.Count你这个就是一个定值,不是当前行的值。当然有问题
孟子E章 2012-09-24
  • 打赏
  • 举报
回复
rt.Items.Count是多少?
rt又是什么对象?
hxlisgod 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

在onrowcommand里面,得到操作行的Index,然后
Label Num = gvSelect.Items[Index].FindControl("Num") as Label ;
[/Quote]
这样
sa = Convert.ToInt32((this.gvSelect.Rows[rt.Items.Count].FindControl("Num") as Label).Text);
Label lab = gvSelect.Rows[rt.Items.Count].FindControl("Num") as Label;
lab.Enabled = true;
num = sa + 1;

我永远是第一行的lable在加1
孟子E章 2012-09-24
  • 打赏
  • 举报
回复
在onrowcommand里面,得到操作行的Index,然后
Label Num = gvSelect.Items[Index].FindControl("Num") as Label ;
yanglei6979431 2012-09-24
  • 打赏
  • 举报
回复
你也不把你的问题介绍一下,谁知道你到底是要干嘛?

61,655

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧