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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why doesn't my radiobutton or checkbox event fire

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have stripped down my GridView to do some testing with radiobuttons and checkboxes but the event doesn't fire and neither object shows checked.

I have a break set on the "i++;" statement in both of the event handlers, but neither gets there.

What am I missing?

Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestGridView.aspx.cs" Inherits="SpearASCWeb.TestGridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView visible="true" 
			            border="1"		
			            id="GridView1" 
			            runat="server" 
			            AutoGenerateColumns="False" 
			            CssClass="testsDatagrid"
			            gridlines="both"
                        onRowCommand="GridView_RowCommand"
                        ShowHeaderWhenEmpty="true"
			            style="margin:0">
	            <AlternatingRowStyle CssClass="testsDatagridAlternate"/>
	            <HeaderStyle CssClass="testsDatagridHeader"/>
	            <Columns>
                    <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" OnCheckedChanged="MyCheckBox1_CheckedChanged" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
	            </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using DataLayer;

namespace SpearASCWeb
{
    public partial class TestGridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet dataSet;
            DbObject dbo = new DbObject();

            dbo.Parameters.Add(database.SetParameter("@SurveyTestQuestionID", SqlDbType.Int, 50));

            dataSet = dbo.GetDataSet("GetSurveyTestAnswers", "TempTable");

            if (dataSet.Tables[0].Rows.Count == 0)
            {
                DataRow dr = dataSet.Tables[0].NewRow();
                dataSet.Tables[0].Rows.Add(dr);
            }

            GridView1.DataSource = dataSet.Tables["tempTable"];
            GridView1.DataBind();

        }
        public void GridView_RowCommand(object s, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridView oGrid = (GridView)s;


            if (e.CommandName == "Insert")
            {
                DbObject myDbObject = new DbObject();

                GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);//catching the row in which thhe link button is clicked.

                TextBox txtAnswer2 = (TextBox)gvrow.FindControl("txtAnswerFooter");
                Label qU = (Label)((GridView)e.CommandSource).Rows[index].FindControl("lblQuestionUniqueFooter");

            }
        }
        public void MyRadioButton1_CheckedChanged(object s, EventArgs e)
        {
            int i = 10;
            i++;
        }
        public void MyCheckBox1_CheckedChanged(object s, EventArgs e)
        {
            int i = 10;
            i++;
        }
    }
}

Thanks,

Tom
 
Nevermind.

I figured out why.

The databind in the Page_Load is rebinding so that apparently causes it not to fire.

If I put a "!IsPostBack" around it, it works fine.

Tom
 
Bingo! That is the cause of many problems.
However, the event is still firing, but it happens AFTER the Page_Load fires.

To understand this better, look up ASP.NET order of events. It gets even crazier when you use usercontrols and masterpages. It's good to know and helpful when things don't seem to work the way you expect. It's an important concept to understand.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top