Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to disable 2 radiobuttons from a list that is generated from a table. There are a lot of articles on this but few if any address the issue when the list comes from a table. The RadioButtonlist is generated from an SQL table that contains Job Units or Teams. I want to disable two of the Teams (Marketing and SRT) from the list. The Job Units table is used by other applications so removing the 2 Job Units from the table would affect the other applications. I could do it in the SelectMethod but that is also shared.

How can I disable the two Teams from this generated list?

Here is my code:

.ASPX
ASP.NET
<asp:Label ID="Label1" runat="server" Text="Team:" ></asp:Label>             
        <asp:RadioButtonList ID="rblTeam" runat="server" AutoPostBack="True" DataSourceID="odsTeam" 
             Font-Size="X-Small" RepeatDirection="Horizontal"  BorderStyle="Outset"  RepeatColumns="6"  
            DataValueField="JobUnitID" DataTextField = "JobUnitAbbreviation" OnLoad="Page_Load" >
        </asp:RadioButtonList>
        
        <!-- data source for the team drop down list -->
        <asp:ObjectDataSource ID="odsTeam" runat="server" 
            SelectMethod="GetCSOJobUnits" TypeName="SupportBAL.SupportBAL">
        </asp:ObjectDataSource><br />


C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Page.SetFocus(txtCaseNumber);
            rblTeam.SelectedIndex = 0;
            int TodaysDay2 = DateTime.Today.Day;
            int TodaysMonth2 = DateTime.Today.Month;
            int TodaysYear2 = DateTime.Today.Year;
            txtLogDate.Text = TodaysMonth2 + "/" + TodaysDay2 + "/" + TodaysYear2;
        }
    }


If I was creating the list in the .ASPX code, no problem. Since the list is being generated, I am stuck.

Any help and advise will be much appreciated.
Posted
Comments
Ashraff Ali Wahab 12-Sep-12 14:41pm    
You can do this in the DataBinding event right.Are you facing any sort of problem while doing this in DataBinding event.
JTRizos 12-Sep-12 14:54pm    
Hi Ashraff, Do you mean in the SelectMethod? I could do it in the GetCSOJobUnits code that retrieves the list items from the SQL table but that is shared by other applications. I could create new SelectMethod code for this app but was hoping to do it in the code behind.

Thank you for your reply.


1 solution

1) Disabling items:

Serach for your item in the list (you can do it or by text or by value) and disable it:

ListItem srtItem = rblTeam.Items.FindByValue("SRT");

if (srtItem != null)
   srtItem.Enabled = false;


This code should work.

2) Rewriting some code:

This:

C#
int TodaysDay2 = DateTime.Today.Day;
int TodaysMonth2 = DateTime.Today.Month;
int TodaysYear2 = DateTime.Today.Year;
txtLogDate.Text = TodaysMonth2 + "/" + TodaysDay2 + "/" + TodaysYear2;


can easily be written as (if you are trying to avoid a current culture):

txtLogDate.Text = DateTime.Now.ToString("MM\\/dd\\/yyyy");


More information here:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^]
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^]


Cheers
 
Share this answer
 
Comments
JTRizos 12-Sep-12 17:29pm    
Hi Mario, #1 doesn't work. srtItem comes back as null. Tried it with Text and Value. Thanx for the suggestions.
Mario Majčica 13-Sep-12 3:42am    
In which part of code did you tried to find the item? Can you set a break point on FindByValue and see if the control contains items?
JTRizos 13-Sep-12 12:13pm    
I placed it in the Page_Load Event (see above). Here's part of the code

rblTeam.SelectedIndex = 0;

ListItem tmpItem = rblTeam.Items.FindByValue("SRT");
if (tmpItem != null)
{
rblTeam.Enabled = false;
}

int TodaysDay2 = DateTime.Today.Day;

I placed a control break on the if line and the tmpItem contained a null. I am thinking of doing this in the SelectMethod code that retrieves the table entries.

Thanx again for your help
Mario Majčica 14-Sep-12 3:17am    
OK, tmpItem was null, but what was the count of rblTeam.Items?
JTRizos 14-Sep-12 12:21pm    
The count is 0. There should be 14 entries.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900