最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Is it possible to disable one listItem from radioButton list in asp.net- Stack Overflow

programmeradmin0浏览0评论

I want to disable one listItem from RadioButtonList depend on condition like if querystring contain true then it will display both the ListItem or else it disabled 2nd listItem ie(External). So user can't access 2nd list item

I have url like

abc/Account.aspx?type=true

abc/Account.aspx?type=false

Code On page Account.aspx

  <asp:RadioButtonList ID="rdodomiantype" runat="server" RepeatDirection="Horizontal" onclick="setTextbox()">
            <asp:ListItem Selected="True" Value="0" >Default</asp:ListItem>
            <asp:ListItem Value="1" >External</asp:ListItem>
    </asp:RadioButtonList>

I want to disable one listItem from RadioButtonList depend on condition like if querystring contain true then it will display both the ListItem or else it disabled 2nd listItem ie(External). So user can't access 2nd list item

I have url like

abc./Account.aspx?type=true

abc./Account.aspx?type=false

Code On page Account.aspx

  <asp:RadioButtonList ID="rdodomiantype" runat="server" RepeatDirection="Horizontal" onclick="setTextbox()">
            <asp:ListItem Selected="True" Value="0" >Default</asp:ListItem>
            <asp:ListItem Value="1" >External</asp:ListItem>
    </asp:RadioButtonList>
Share Improve this question edited Jul 13, 2012 at 9:35 Satinder singh asked Jul 13, 2012 at 7:58 Satinder singhSatinder singh 10.2k18 gold badges64 silver badges102 bronze badges 2
  • I think the reason you have been downvoted is that you start your "question" with My requirement is ... this is a Q and A site - not a rent a developer site ... please rephrase it so that it reads like a question and shows effort on your part to resolve your issue – Manse Commented Jul 13, 2012 at 9:10
  • @ManseUK: Thank you and i apologies for that – Satinder singh Commented Jul 13, 2012 at 9:28
Add a ment  | 

3 Answers 3

Reset to default 7

I didn't want to remove the item from a list, I wanted to disable it like the original poster asked. As such, here's the code I was able to use (EDIT - converted to C# and checking for querystring param based on original post:

if (!string.IsNullOrEmpty(Request.QueryString["type"]) && Request.QueryString["type"].ToUpper() == "TRUE")
            {
                foreach (ListItem item in rdoList.Items)
                {
                    if (item.Text == "External")
                    {
                        item.Enabled = false;
                        item.Attributes.Add("style", "color:#999;");
                        break;
                    }
                }
            }

Here how i solved with the help of HariHaran

 protected void Page_Load(object sender, EventArgs e)
 {
   string planType=Request.QueryString["type"];
   if (planType == "False")
    {
      rdodomiantype.Items.Remove(rdodomiantype.Items.FindByValue("1"));
    }
 }

Is it necessary to disable one list item? My suggestion is according to the query string value you can bind the dropdown. ie if the query string value is true then you van bind the dropdown with both list item values;

or if the query string value is false you can bind the dropdown with first list item only.

发布评论

评论列表(0)

  1. 暂无评论