Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same method for checkbox and radiobox

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have a GridView with 2 columns, one a CheckBox and one a RadioButton.

But I have to do everything twice, one for each.

For example, I have the following:

For the CheckBox:
Code:
public void SelectGridViewCB(GridView oGrid, string rbName, int answerBits)
{
    CheckBox cb = default(CheckBox);
    int iKtr = 0;
    //deselect all radiobutton in gridview
    foreach (GridViewRow gvr in oGrid.Rows)
    {
        cb = (CheckBox)oGrid.Rows[iKtr].FindControl(rbName);
        if (BitIsSet(answerBits, iKtr))
            cb.Checked = true;
        iKtr += 1;
    }
}

For the RadioButton:
Code:
public void SelectGridViewRB(GridView oGrid, string rbName, int answerBits, object selection)
{
    int iKtr = 0;
    //deselect all radiobutton in gridview
    foreach (GridViewRow gvr in oGrid.Rows)
    {
        RadioButton rb = default(RadioButton);
        rb = (RadioButton)oGrid.Rows[iKtr].FindControl(rbName);
        if(BitIsSet(answerBits,iKtr))
            rb.Checked = true;
        iKtr += 1;
    }
}

The code is the same except for object type.

Is there a simpler way to consolidate the code?

Thanks,

Tom
 
Why are you looping through the gridviewrows this way? When is this being called? Most likely this should be done in one of the gridview's events.
 
You have to or set up some other sort of handling RadioButtons. In a GridView and DataGrid, the RadioButtons don't work as groups. Even if a radio button, the user can press multiple radio buttons and they will all be set. So I clear all the buttons and only set the one that is set on the radio button event.

But depending on the type of column (true/false, single choice or multiple choice), the behavior is different and in the case of multiple choice would be a checkbox.

Tom
 
What I have in my GridView is 2 objects, one RadioButton and one CheckBox. I can either have it in one TemplateField where I would hide one of them during DataBind or separate them into two TemplateFields where I would hide whichever column I didn't need based on the QuestionType, where I would use the CheckBox if allowing multiple Selection.

Code:
<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="center">
    <ItemTemplate>
        <asp:Radiobutton ID="myRadioButton" AutoPostBack="True" GroupName="tom" OnCheckedChanged="MyRadioButton1_CheckedChanged" runat="server"/>
        <asp:CheckBox ID="myCheckBox" AutoPostBack="True" GroupName="tom" OnCheckedChanged="MyRadioButton1_CheckedChanged" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

OR

Code:
<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="center">
    <ItemTemplate>
        <asp:RadioButton ID="myRadioButton" AutoPostBack="True" OnCheckedChanged="MyRadioButton1_CheckedChanged" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="center">
    <ItemTemplate>
        <asp:CheckBox ID="myCheckBox" AutoPostBack="True" GroupName="tom" OnCheckedChanged="MyRadioButton1_CheckedChanged" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

Not sure how I would hide the template field since I have no ID for it so maybe doing the 1st option and hiding the object I don't want would be easier.

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top