locked
Disable or Enable CheckBoxList items depending on RadioButtonList item RRS feed

  • Question

  • User-1392235324 posted

    Dear All

    I developed a page that sends messages to my groups and channels on Telegram App by using Telegram Bot API ( for example ) as following :

    Method Name : sendMessage

    Required Parameters : chat_id, and text

    Optional Parameters : parse_mode, disable_notification, disable_web_page_preview, reply_markup, and reply_to_message_id

    https://api.telegram.org/bot000000000:AAAAa0aAA_aaA-Aaaa0A0Aa_a0aa0A0AAAA/sendMessage?chat_id=1234567&text=abc

    https://api.telegram.org/bot{token}/{method}{parameters}

    I developed another page to use any of methods for Telegram .

    The beneath is my ASPX page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TMs.aspx.cs" Inherits="WebApplication2.TMs" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Telegram Methods</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table style="width: 100%;">
                    <tr>
                        <td>
                            <asp:RadioButtonList ID="rblMethod" RepeatDirection="Horizontal"
                                RepeatColumns="5" runat="server">
                                <asp:ListItem Text="answerCallbackQuery" />
                                <asp:ListItem Text="editMessageCaption" />
                                <asp:ListItem Text="editMessageReplyMarkup" />
                                <asp:ListItem Text="editMessageText" />
                                <asp:ListItem Text="forwardMessage" />
                                <asp:ListItem Text="getFile" />
                                <asp:ListItem Text="getMe" />
                                <asp:ListItem Text="getUpdates" />
                                <asp:ListItem Text="getUserProfilePhotos" />
                                <asp:ListItem Text="kickChatMember" />
                                <asp:ListItem Text="sendAudio" />
                                <asp:ListItem Text="sendChatAction" />
                                <asp:ListItem Text="sendContact" />
                                <asp:ListItem Text="sendDocument" />
                                <asp:ListItem Text="sendLocation" />
                                <asp:ListItem Text="sendMessage" />
                                <asp:ListItem Text="sendPhoto" />
                                <asp:ListItem Text="sendSticker" />
                                <asp:ListItem Text="sendVenue" />
                                <asp:ListItem Text="sendVoice" />
                                <asp:ListItem Text="setWebHook" />
                                <asp:ListItem Text="unbanChatMember" />
                            </asp:RadioButtonList>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <hr />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:CheckBoxList ID="cblParameter" RepeatDirection="Horizontal"
                                RepeatColumns="5" runat="server">
                                <asp:ListItem Text="action" />
                                <asp:ListItem Text="address" />
                                <asp:ListItem Text="audio" />
                                <asp:ListItem Text="caption" />
                                <asp:ListItem Text="certificate" />
                                <asp:ListItem Text="chat_id" />
                                <asp:ListItem Text="disable_notification" />
                                <asp:ListItem Text="disable_web_page_preview" />
                                <asp:ListItem Text="document" />
                                <asp:ListItem Text="duration" />
                                <asp:ListItem Text="file_id" />
                                <asp:ListItem Text="first_name" />
                                <asp:ListItem Text="foursquare_id" />
                                <asp:ListItem Text="from_chat_id" />
                                <asp:ListItem Text="height" />
                                <asp:ListItem Text="inline_message_id" />
                                <asp:ListItem Text="last_name" />
                                <asp:ListItem Text="latitude" />
                                <asp:ListItem Text="limit" />
                                <asp:ListItem Text="longitude" />
                                <asp:ListItem Text="message_id" />
                                <asp:ListItem Text="offset" />
                                <asp:ListItem Text="parse_mode" />
                                <asp:ListItem Text="performer" />
                                <asp:ListItem Text="phone_number" />
                                <asp:ListItem Text="photo" />
                                <asp:ListItem Text="reply_markup" />
                                <asp:ListItem Text="reply_to_message_id" />
                                <asp:ListItem Text="show_alert" />
                                <asp:ListItem Text="sticker" />
                                <asp:ListItem Text="text" />
                                <asp:ListItem Text="timeout" />
                                <asp:ListItem Text="title" />
                                <asp:ListItem Text="url" />
                                <asp:ListItem Text="user_id" />
                                <asp:ListItem Text="video" />
                                <asp:ListItem Text="voice" />
                                <asp:ListItem Text="width" />
                            </asp:CheckBoxList>
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>
    

    My question : by C# code, when I choose a specific radio button item, then I want to let some parameters ( Check Box Items ) chosen as mandatory, some mandatory disabled, and the remaining that can I enable or disable them .

    Thursday, May 19, 2016 8:48 AM

Answers

  • User61956409 posted

    Hi kareemva,

    You could refer to the following sample code to dynamically enable/disable CheckBoxList items depending on RadioButtonList item on RadioButtonList SelectedIndexChanged event.

    <asp:RadioButtonList ID="rblMethod" RepeatDirection="Horizontal"
    RepeatColumns="5" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblMethod_SelectedIndexChanged">
    
    protected void rblMethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        //get selected item of RadioButtonList
        string val = rblMethod.SelectedValue.ToString();
    
        //dynamically set Enabled property to enable/disable items of CheckBoxList
        cblParameter.Items[0].Enabled = false;
    }
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 20, 2016 2:36 AM
  • User-1780421697 posted

    Radio button list event handler is required 

    OnSelectedIndexChanged="rblMethod_onChanged"

    Make sure to set AutoPostback="true"

    Now you can get the selected radio button and its value 

    like

    //asp.net side
    <asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
                    enabled="true" OnSelectedIndexChanged="onAckTypeChanged" AutoPotBack="true">
        <asp:ListItem Text="None" />
        <asp:ListItem Text="SHOW" />   
        <asp:ListItem Text="HIDE" />
    </asp:RadioButtonList>
    
    //code behind
    protected void onAckTypeChanged(object sender, EventArgs e)
    {
        if (_indicatorAckType.SelectedItem.Text == "SHOW")
            _myTextboxID.Visible = true;
        else
            _myTextboxID.Visible = false;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 20, 2016 4:05 AM
  • User-1392235324 posted

    I solved it!

            protected void rblMethod_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (rblMethod.SelectedItem.Text == "sendMessage")
                {
                    cblParameter.Items[5].Selected = true;
                    cblParameter.Items[30].Selected = true;
    
                    for (int i = 0; i <= 37; i++)
                    {
                        if (i != 6 && i != 7 && i != 22 && i != 26 && i != 27)
                        {
                            cblParameter.Items[i].Enabled = false;
                        }
                    }
                }
    
                Label lbl1 = new Label();
                lbl1.Text = "Enter chat_id : ";
                form1.Controls.Add(lbl1);
    
                TextBox txt1 = new TextBox();
                txt1.ID = "tbChat_id";
                form1.Controls.Add(txt1);
    
                Label lbl2 = new Label();
                lbl2.Text = "<br /><br />Enter text : ";
                form1.Controls.Add(lbl2);
    
                TextBox txt2 = new TextBox();
                txt2.ID = "tbText";
                form1.Controls.Add(txt2);
    
                Label lbl3 = new Label();
                lbl3.Text = "<br /><br />";
                form1.Controls.Add(lbl3);
    
                Button btn = new Button();
                btn.Text = "Submit";
                form1.Controls.Add(btn);
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 29, 2016 9:50 AM

All replies

  • User61956409 posted

    Hi kareemva,

    You could refer to the following sample code to dynamically enable/disable CheckBoxList items depending on RadioButtonList item on RadioButtonList SelectedIndexChanged event.

    <asp:RadioButtonList ID="rblMethod" RepeatDirection="Horizontal"
    RepeatColumns="5" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rblMethod_SelectedIndexChanged">
    
    protected void rblMethod_SelectedIndexChanged(object sender, EventArgs e)
    {
        //get selected item of RadioButtonList
        string val = rblMethod.SelectedValue.ToString();
    
        //dynamically set Enabled property to enable/disable items of CheckBoxList
        cblParameter.Items[0].Enabled = false;
    }
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 20, 2016 2:36 AM
  • User-1780421697 posted

    Radio button list event handler is required 

    OnSelectedIndexChanged="rblMethod_onChanged"

    Make sure to set AutoPostback="true"

    Now you can get the selected radio button and its value 

    like

    //asp.net side
    <asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
                    enabled="true" OnSelectedIndexChanged="onAckTypeChanged" AutoPotBack="true">
        <asp:ListItem Text="None" />
        <asp:ListItem Text="SHOW" />   
        <asp:ListItem Text="HIDE" />
    </asp:RadioButtonList>
    
    //code behind
    protected void onAckTypeChanged(object sender, EventArgs e)
    {
        if (_indicatorAckType.SelectedItem.Text == "SHOW")
            _myTextboxID.Visible = true;
        else
            _myTextboxID.Visible = false;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 20, 2016 4:05 AM
  • User-1392235324 posted

    Thanks to Khuram.Shahzad and Fei Han - MSFT

    How can I create Text Boxes by code when I choose a Radio Button and when I choose a Check Box ?

    Now my code is as :

            protected void rblMethod_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (rblMethod.SelectedItem.Text == "sendMessage")
                {
                    cblParameter.Items[5].Selected = true;
                    cblParameter.Items[30].Selected = true;
    
                    for (int i = 0; i <= 37; i++)
                    {
                        if (i != 6 && i != 7 && i != 22 && i != 26 && i != 27)
                        {
                            cblParameter.Items[i].Enabled = false;
                        }
                    }
                }
            }
    

    For example when I choose sendMessage Radio Button, then dynamically tbChat_id and tbText Text Boxes created. And when I check reply_to_message_id then tbReply_to_message_id Text Box created etc.

    Sunday, May 29, 2016 8:53 AM
  • User-1392235324 posted

    I solved it!

            protected void rblMethod_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (rblMethod.SelectedItem.Text == "sendMessage")
                {
                    cblParameter.Items[5].Selected = true;
                    cblParameter.Items[30].Selected = true;
    
                    for (int i = 0; i <= 37; i++)
                    {
                        if (i != 6 && i != 7 && i != 22 && i != 26 && i != 27)
                        {
                            cblParameter.Items[i].Enabled = false;
                        }
                    }
                }
    
                Label lbl1 = new Label();
                lbl1.Text = "Enter chat_id : ";
                form1.Controls.Add(lbl1);
    
                TextBox txt1 = new TextBox();
                txt1.ID = "tbChat_id";
                form1.Controls.Add(txt1);
    
                Label lbl2 = new Label();
                lbl2.Text = "<br /><br />Enter text : ";
                form1.Controls.Add(lbl2);
    
                TextBox txt2 = new TextBox();
                txt2.ID = "tbText";
                form1.Controls.Add(txt2);
    
                Label lbl3 = new Label();
                lbl3.Text = "<br /><br />";
                form1.Controls.Add(lbl3);
    
                Button btn = new Button();
                btn.Text = "Submit";
                form1.Controls.Add(btn);
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 29, 2016 9:50 AM
  • User-1392235324 posted

    Now this is my solution to send a message to a channel or a group or a person :

            TextBox txt0 = new TextBox();
            TextBox txt1 = new TextBox();
            TextBox txt2 = new TextBox();
    
            protected void Page_Load(object sender, EventArgs e)
            {
            }
    
            protected void rblMethod_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (rblMethod.SelectedItem.Text == "sendMessage")
                {
                    cblParameter.Items[5].Selected = true;
                    cblParameter.Items[30].Selected = true;
    
                    for (int i = 0; i <= 37; i++)
                    {
                        if (i != 6 && i != 7 && i != 22 && i != 26 && i != 27)
                        {
                            cblParameter.Items[i].Enabled = false;
                        }
                    }
                }
    
                Label lbl0 = new Label();
                lbl0.Text = "Enter token : ";
                form1.Controls.Add(lbl0);
    
                txt0.ID = "token";
                form1.Controls.Add(txt0);
    
                Label lbl1 = new Label();
                lbl1.Text = "<br /><br />Enter chat_id : ";
                form1.Controls.Add(lbl1);
    
                txt1.ID = "tbChat_id";
                form1.Controls.Add(txt1);
    
                Label lbl2 = new Label();
                lbl2.Text = "<br /><br />Enter text : ";
                form1.Controls.Add(lbl2);
    
                txt2.ID = "tbText";
                form1.Controls.Add(txt2);
    
                Label lbl3 = new Label();
                lbl3.Text = "<br /><br />";
                form1.Controls.Add(lbl3);
    
                Button button = new Button();
                button.Text = "Submit";
                button.Click += new EventHandler(button_Click);
    
                form1.Controls.Add(button);
            }
    
            protected void button_Click(object sender, EventArgs e)
            {
                string token = txt0.Text;
                string chat_id = txt1.Text;
                token = "https://api.telegram.org/bot" +
                  token +
                  "/sendMessage?chat_id=" + chat_id +
                  "&text=" + txt2.Text;
    
                byte[] byteArray = Encoding.UTF8.GetBytes(token);
    
                // Create a request for the URL.
                WebRequest request = WebRequest.Create(token);
                ((HttpWebRequest)request).UserAgent = ".NET Framework Example Client";
                request.Method = "POST";
                request.ContentLength = byteArray.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();
                WebResponse response = request.GetResponse();
                Response.Write(((HttpWebResponse)response).StatusDescription + "<br/>");
                Stream data = response.GetResponseStream();
                response.Close();
            }
    

    But, when I press Submit button then the message didn't send and and all Text Boxes and Button disappeared !

    Sunday, May 29, 2016 10:57 AM