views:

40

answers:

3

hi, I have a GRIDVIEW and with several CHECKBOXS.

When i selected a CHECKBOX I need run some code.

To detect it, I use an EVENT HANDLER for the CHECKBOX included in a GRIDVIEW.

I cannot access the CHECKBOX with my wrong code.

Do you have any idea what I am doing wrong? Thanks for your help. Bye

ASPX

    <asp:Label ID="uxMessageDisplayer" runat="server" Visible="False" EnableViewState="False"></asp:Label>
<asp:GridView ID="uxUserListDisplayer" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="uxUserListDisplayer_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Active">
            <ItemTemplate>
                <asp:CheckBox ID="uxActiveCheckBoxSelector" runat="server" AutoPostBack="true" OnCheckedChanged="uxRoleCheckBoxSelector_CheckChanged" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Users">
            <ItemTemplate>
                <asp:Label runat="server" ID="uxUserNameLabelDisplayer" Text='<%# Container.DataItem %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkEditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CODE BEHIND

    protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
    {
         // Reference the CheckBox that raised this event   
        //CheckBox uxActiveCheckBoxSelector = sender as CheckBox;

        CheckBox activeCheckBox = (CheckBox)FindControl("uxActiveCheckBoxSelector");

        if (activeCheckBox.Checked == true)
        {
            uxMessageDisplayer.Text = "T - Aproved User";
            uxMessageDisplayer.Enabled = false;
        }
        else
        {
            uxMessageDisplayer.Text = "F - NOT Aproved User";
            uxMessageDisplayer.Enabled = false;
        }
       }
+1  A: 

I'm assuming the event handler is actually registered to the checkbox.

CheckBox activeCheckBox = (CheckBox)sender;

what is "uxActiveCheckBoxSelector" and why are you ignoring sender?

lincolnk
uxActiveCheckBoxSelector is the CHECKBOX I need to FINDOUT; could you please send me an example of code I do not understand I am a beginner :-)
GIbboK
The sender parameter is the object that triggered the event, in this case, it would be the checkbox you're "looking for"
Keith
@GibboK for the code you've provided, you need to swap out `CheckBox activeCheckBox = (CheckBox)FindControl("uxActiveCheckBoxSelector");` for the statement in my answer. the `Label` you're accessing is outside the grid, so you can access it directly.
lincolnk
+2  A: 

If I am not mistaken by your question, you are trying to set the text of the label on the same row with the checkbox based on its checked status.

Below is the code snippet I tried on my pc, hope it helps.


.aspx:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
        return;

    //create dummy data
    List<string> rows = new List<string>();
    Enumerable.Range(1, 5).ToList().ForEach(x => rows.Add(x.ToString()));

    //bind dummy data to gridview
    GridView1.DataSource = rows;
    GridView1.DataBind();
}

    protected void CheckBox1_CheckChanged(object sender, EventArgs e)
    {
        //cast sender to checkbox
        CheckBox CheckBox1 = (CheckBox)sender;

        //retrieve the row where checkbox is contained
        GridViewRow row = (GridViewRow)CheckBox1.NamingContainer;

        //find the label in the same row
        Label Label1 = (Label)row.FindControl("Label1");

        //logics
        if (CheckBox1 != null)  //make sure checkbox1 is found
        {
            if (CheckBox1.Checked)
            {
                if (Label1 != null) //make sure label1 is found
                {
                    Label1.Text = "Checked";
                }

            }
            else
            {
                if (Label1 != null)
                {
                    Label1.Text = "Unchecked";
                }
            }
        }
    }
Lee Sy En
A: 

Code corrected as suggest! Usefull resource for beginners

        protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
    {
        // Cast sender to CheckBox
        CheckBox activeCheckBox = (CheckBox)sender;

        // Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
        GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;

        if (activeCheckBox.Checked == true)
        {
            uxMessageDisplayer.Text = "T - Aproved User";
        }
        else
        {
            uxMessageDisplayer.Text = "F - NOT Aproved User";
        }
    }
GIbboK