C#如何实现同一个按钮改变值

C#如何实现同一个按钮改变值
[ 标签:c#, 同一个按钮 ] ╭ァ紅太狼。 2009-05-15 16:10
要实现的是一个开关按钮 点了“开” 这个按钮就会显示“关” 反之 点了“关” 这个 按钮就会显示 “开” 怎么写啊 这个代码
前台:
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>

<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="开" CommandName="up" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "up")
{
if (((Button)sender).Text == "开")
{
((Button)sender).Text = "关";
}
else if (((Button)sender).Text == "关")
{
((Button)sender).Text = "开";
}
}
}

 

 

在Button的点击事件中添加代码
if(Button1.Text == "开")
{
Button2.Text = "关";
}
else
{
Button2.Text = "开";

}
继续追问: 不不 就一个按钮 就一个btnOpen 在里面同时要有开和关
补充回答: if(Button1.Text == "开")
{
Button1.Text = "关";
}
else
{
Button1.Text = "开";
}
打错数字了不好意思
Button1.Text=Button1.Text=="关"?"开":"关";
$乐吧$ 回答采纳率:16.0% 2009-05-15 10:52
您已经评价过!
好:0
您已经评价过!
不好:0
btnOpen.Text=="开"?btnOpen.Text="关":btnOpen.Text="开";

绝对没有问题。。。
ぺ烟抽寂寞 2009-05-15 11:37
您已经评价过!
好:0
您已经评价过!
不好:0
bool bl=false
if(bl==false)
{
Button2.Text = "关";
bl=true;
}
else
{
Button2.Text = "开";
bl=false;
}
阔┽软 2009-05-15 12:50
您已经评价过!
好:0
您已经评价过!
不好:0
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在子窗体中刷新父窗体可以使用委托和事件来实现。具体步骤如下: 1. 在父窗体中定义一个委托和事件: ```csharp public delegate void RefreshEventHandler(); public event RefreshEventHandler RefreshEvent; ``` 2. 在子窗体中通过构造函数传递父窗体对象,并在需要刷新时触发事件: ```csharp public partial class ChildForm : Form { private ParentForm parentForm; public ChildForm(ParentForm parentForm) { InitializeComponent(); this.parentForm = parentForm; } private void button1_Click(object sender, EventArgs e) { // 子窗体按钮点击时触发事件 parentForm.RefreshEvent?.Invoke(); } } ``` 3. 在父窗体中订阅事件,并在事件处理程序中执行刷新操作: ```csharp public partial class ParentForm : Form { public ParentForm() { InitializeComponent(); // 订阅子窗体事件 childForm.RefreshEvent += RefreshData; } private void RefreshData() { // 刷新操作 // ... } } ``` 如果要在子窗体中改变父窗体控件的,可以通过将父窗体对象传递给子窗体,并在子窗体中直接修改父窗体控件的。具体步骤如下: 1. 在子窗体中定义一个父窗体对象,并在构造函数中初始化: ```csharp public partial class ChildForm : Form { private ParentForm parentForm; public ChildForm(ParentForm parentForm) { InitializeComponent(); this.parentForm = parentForm; } private void button1_Click(object sender, EventArgs e) { // 修改父窗体控件的 parentForm.label1.Text = "新的文本内容"; } } ``` 2. 在父窗体中打开子窗体时将自身对象传递给子窗体: ```csharp private void button1_Click(object sender, EventArgs e) { ChildForm childForm = new ChildForm(this); childForm.Show(); } ``` 这样,在子窗体中就可以直接修改父窗体的控件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值