选择/由最后一次选择的首先取消在asp.net中取消按钮 [英] select/deselect button in asp.net by last selected first deselect

查看:123
本文介绍了选择/由最后一次选择的首先取消在asp.net中取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经动态创建按钮,如在代码中所有的所有按钮,从10:00到12:00与间隔15分钟,就像如果我选择了10取消LIFO顺序:0和10:15那我就不能选择10:45 ..和II已选择10:0,10:15,10:30,10:45,然后我不得不取消10:15那么首先我们要取消10:30和10:45 ... 。请帮我

 私人无效的GetControls()
{
计数++;
的for(int i = 10; I< 12;我++)
{
为(INT J = 0; J< 60; J + = 15)
{
键BTN =新按钮();
btn.Text = I + - + J;
btn.ID = I + - + J;
btn.Command + =新CommandEventHandler(this.btn_Click);
// btn.Click + = btn_Click;
标志= TRUE;
btn.CommandName = I + - + J;
如果(计数== 1)
{
PlaceHolder1.Controls.Add(BTN);
}
}
}

}

私人无效btn_Click(对象发件人,CommandEventArgs E)
{
算++;

字符串ID =(发件人为按钮).ID;
Label1.Text =!Congrates您的会议时间已经sheduled高达+ ID;
Label1.Visible = FALSE;
键BTN =发件人的按钮;


如果(btn.BackColor == Color.Green)
{
btn.BackColor = System.Drawing.Color.Yellow;
的getStatus(寄件人);

}

,否则
{
btn.BackColor = System.Drawing.Color.Green;

}


解决方案

纠正我,如果我错了!你想每次只选择一个按钮,如果单击一个按钮,剩下的应该重置?如果是这样的话,这里有一个代码,这样做的:








添加一个Session记住动态按钮标识和使用,而不是()( - )的按钮IDS

 私人无效的GetControls()
{
计数++;
的for(int i = 10; I< 12;我++)
{
为(INT J = 0; J< 60; J + = 15)
{
键BTN =新按钮();
btn.Text = I + - + J;
btn.ID = I +。 + j的;
btn.Command + =新CommandEventHandler(this.btn_Click);
// btn.Click + = btn_Click;
标志= TRUE;
btn.CommandName = I + - + J;
如果(计数== 1)
{
PlaceHolder1.Controls.Add(BTN);
名单,LT;字符串> createdControls =会话[控制]!= NULL?会话[控件]作为名单<串GT;新名单<串GT;();
如果createdControls.Add(btn.ID)(createdControls.Contains(btn.ID)!);
会话[控件] = createdControls;
}
}
}

}

替换为以下的BTN-Click事件。

 私人无效btn_Click(对象发件人,CommandEventArgs E) 
{
计数++;
字符串ID =(发件人为按钮).ID;
ResetButton(Convert.ToDouble(ID));
Label1.Text =!Congrates您的会议时间已经sheduled高达+ ID;
键BTN =发件人的按钮;

如果(btn.BackColor == Color.Green)
{
btn.BackColor = System.Drawing.Color.Yellow;


}

,否则
{
btn.BackColor = System.Drawing.Color.Green;

}
}

这是方法实施

 私人无效ResetButton(双selectedButtonID)
{
名单<串> createdControls =会话[控制]!= NULL?会话[控件]作为名单<串GT;新名单<串GT;();
时间跨度时间跨度= TimeSpan.FromHours(selectedButtonID);
串currentSelectedTime = timespan.ToString(h\\:毫米);
的foreach(字符串buttonID在createdControls)
{
如果(!string.IsNullOrEmpty(buttonID))
{
INT comparisonResult = timespan.CompareTo(TimeSpan.FromHours( Convert.ToDouble(buttonID)));
按钮= Page.FindControl(buttonID)的按钮;
如果(键= NULL&放大器;!&安培; comparisonResult == 1)
{
button.BackColor = Color.Yellow; //选择
}
$其他b $ b {
button.BackColor = Color.Red; //取消
}
}
}


I have dynamically created buttons as shown in code all all the button from 10:00 to 12:00 with 15 min of interval and deselect in lifo order like if i have selected 10:0 and 10:15 then i can not select 10:45 .. and i i have selected 10:0,10:15,10:30,10:45 and then i have to deselect 10:15 then first we have to deselect 10:30 and 10:45 ...please help me out.

   private void GetControls()
    {
        count++;
        for (int i = 10; i < 12; i++)
        {
            for (int j = 0; j < 60; j += 15)
            {
                Button btn = new Button();
                btn.Text = i + "-" + j;
                btn.ID = i + "-" + j;
                btn.Command += new CommandEventHandler(this.btn_Click);
               // btn.Click += btn_Click;
                flag = true;
                btn.CommandName = i + "-" + j;
                if (count==1)
                {
                    PlaceHolder1.Controls.Add(btn);
                }
             }
       }   

    }

    private void btn_Click(object sender, CommandEventArgs e)
    {
        count++;

        string ID = (sender as Button).ID;
        Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
        Label1.Visible = false;
        Button btn = sender as Button;


        if (btn.BackColor == Color.Green)
        {
            btn.BackColor = System.Drawing.Color.Yellow;
            getStatus(sender);

        }

        else
        {
            btn.BackColor = System.Drawing.Color.Green;

        }

解决方案

Correct me if I am wrong! You want to only select one button at a time so that if one button is clicked, the rest should reset? if that's the case, here's a code to do so:


[Edit]

Added a Session to remember dynamic button ids and used (.) instead of (-) for button Ids

private void GetControls()
    {
        count++;
        for (int i = 10; i < 12; i++)
        {
            for (int j = 0; j < 60; j += 15)
            {
                Button btn = new Button();
                btn.Text = i + "-" + j;
                btn.ID = i + "." + j;
                btn.Command += new CommandEventHandler(this.btn_Click);
                // btn.Click += btn_Click;
                flag = true;
                btn.CommandName = i + "-" + j;
                if (count == 1)
                {
                    PlaceHolder1.Controls.Add(btn);
                    List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
                    if (!createdControls.Contains(btn.ID)) createdControls.Add(btn.ID);
                    Session["Controls"] = createdControls;
                }
            }
        }

    }

[Edited] Replace the btn-Click event with the following.

private void btn_Click(object sender, CommandEventArgs e)
    {
        count++;
        string ID = (sender as Button).ID;
        ResetButton(Convert.ToDouble(ID));
        Label1.Text = " Congrates! Your meeting time has been sheduled upto " + ID;
        Button btn = sender as Button;

        if (btn.BackColor == Color.Green)
        {
            btn.BackColor = System.Drawing.Color.Yellow;


        }

        else
        {
            btn.BackColor = System.Drawing.Color.Green;

        }
    }

[Edited] And this is the method implementation

private void ResetButton(double selectedButtonID)
        {
            List<string> createdControls = Session["Controls"] != null ? Session["Controls"] as List<string> : new List<string>();
            TimeSpan timespan = TimeSpan.FromHours(selectedButtonID);
            string currentSelectedTime = timespan.ToString("h\\:mm");
            foreach (string buttonID in createdControls)
            {
                if (!string.IsNullOrEmpty(buttonID))
                {
                    int comparisonResult = timespan.CompareTo(TimeSpan.FromHours(Convert.ToDouble(buttonID)));
                    Button button = Page.FindControl(buttonID) as Button;
                    if (button != null && comparisonResult==1)
                    {
                        button.BackColor = Color.Yellow;// selected
                    }
                    else
                    {
                        button.BackColor = Color.Red;// deselected
                    }
                }
            }

这篇关于选择/由最后一次选择的首先取消在asp.net中取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆