S
shiv
Hey group:
I am adding an event to a checkbox in a TemplateColumn inside a grid, but cant seem to fire the event, or maybe I am not capturing the value of the checkbox. This is how the code is currently:
The default page loads a control in the body, that contains a DropDownList. User selects a value and DataGrid is populated. The Grid is created at design time and so are the checkboxes:
<code>
<div style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 250px"><asp:datagrid id="DataGrid1" runat="server" CssClass="BodyText" ShowHeader="False" Width="100%"
AutoGenerateColumns="False">
<HeaderStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" BackColor="Gray"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="c_Category" SortExpression="Category" HeaderText="Category">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_Sub_Category" SortExpression="SubCategory" HeaderText="Sub Category">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="c_WebNum">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="20%"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Task">
<HeaderStyle Width="30%"></HeaderStyle>
<ItemStyle Width="30%"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" ID="Label5" Text='<%# DataBinder.Eval(Container, "DataItem.c_Task") %>'>
</asp:Label><br>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.c_Task") %>' >
</asp:TextBox><br>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="*TC">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle Width="25%"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"></asp:CheckBox><BR>
<aspropDownList id=ComplianceList runat="server" CssClass="BodyText" DataSource="<%# GetCompliance() %>" DataTextField="c_Desc" DataValueField="i_ColVal">
</aspropDownList><BR>
<asp:TextBox id="Textbox5" runat="server" EnableViewState="False" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="*DO">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle Width="25%"></ItemStyle>
<ItemTemplate>
<INPUT id="shiv2" type="checkbox" runat="server"><BR>
<asp:Label id="Label6" Runat="server">If not compliant,enter directions:</asp:Label><BR>
<asp:TextBox id="Tbox1" runat="server" CssClass="BodyText" EnableViewState="False" Text="Compliant by 8/19/2004">Compliant by 8/19/2004</asp:TextBox><BR>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False"></asp:BoundColumn>
</Columns>
<PagerStyle VerticalAlign="Bottom" PageButtonCount="15" Mode="NumericPages"></PagerStyle>
</asp:datagrid></div>
</code>
In the codebehind, this is how I BindData() and add the event:
-The page load method:
<code>
private void Page_Load(object sender, System.EventArgs e)
{
GetCity();
}
</code>
BindData method:
<code>
private void BindData()
{
SqlCommand cm;
SqlConnection cn;
cn = new SqlConnection("..;");
cn.Open();
cm = new SqlCommand("dbo.GetAudit",cn);
cm.CommandType=CommandType.StoredProcedure;
//Add the parameters to be passed
cm.Parameters.Add("@city",SqlDbType.Int,4);
//Assign values to the parameters to be input
cm.Parameters["@city"].Value=CityList.SelectedItem.Value;
//Execute the stored procedure
cm.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cm);
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
Session.Add("myset2",ds);
cn.Close();
lblShow.Text = "Showing Tasks for " + CityList.SelectedItem.Text + " office.";
}
-Adding the event upon ItemCreated:
private void grid_ItemCreated( object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType elemType = e.Item.ItemType;
if ((elemType == ListItemType.Item)||(elemType == ListItemType.AlternatingItem))
{
// find the checkbox inside the Grid
CheckBox CheckBox1 = (CheckBox) e.Item.Cells[5].FindControl("CheckBox1");
// register event in the check box
CheckBox1.CheckedChanged += new EventHandler(this.OnChangeHandler);
}
}
protected void OnChangeHandler(object sender, System.EventArgs e)
{
CheckBox CheckBox1 = (CheckBox)sender;
DataGridItem GridItem = (DataGridItem)CheckBox1.Parent.Parent;
///if (CheckBox1.Checked == true)
//{
Label2.Text = CheckBox1.Checked.ToString();
//}
}
</code>
-Init component:
<code>
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grid_ItemCreated);
this.Load += new System.EventHandler(this.Page_Load);
</code>
Now the grid renders just fine when i click on the submit button after selecting from the dropdown. But as soon as I click on the Checkbox, the label doesnt get updated and the grid disappears. And here is the output as redered:
<div style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 250px"><table class="BodyText" cellspacing="0" rules="all" border="1" id="_ctl5_DataGrid1" style="width:100%;border-collapse:collapse;">
<tr>
<td style="width:10%;">General Office Procedures</td><td style="width:10%;">Office Maintenance</td><td style="width:30%;">
<span id="_ctl5_DataGrid1__ctl2_Label5">Ensure Regulatory and HR Compliance in the Field Office</span><br>
</td><td style="width:25%;">
<input id="_ctl5_DataGrid1__ctl2_CheckBox1" type="checkbox" name="_ctl5ataGrid1:_ctl2:CheckBox1" onclick="__doPostBack('_ctl5$DataGrid1$_ctl2$CheckBox1','')" language="javascript" /><BR>
<select name="_ctl5ataGrid1:_ctl2:ComplianceList" id="_ctl5_DataGrid1__ctl2_ComplianceList" class="BodyText">
<option value="1">Procedures are not followed</option>
<option value="2">Wrong person is performing the task</option>
<option value="3">Task is performed at the wrong frequency</option>
<option value="4">Not applicable - please provide details</option>
<option value="5">Other - please provide details</option>
</select><BR>
<input name="_ctl5ataGrid1:_ctl2:Textbox5" type="text" id="_ctl5_DataGrid1__ctl2_Textbox5" />
</td><td style="width:25%;">
<input name="_ctl5ataGrid1:_ctl2:shiv2" id="_ctl5_DataGrid1__ctl2_shiv2" type="checkbox" /><BR>
<span id="_ctl5_DataGrid1__ctl2_Label6">If not compliant,enter directions:</span><BR>
<input name="_ctl5ataGrid1:_ctl2:Tbox1" type="text" value="Compliant by 8/19/2004" id="_ctl5_DataGrid1__ctl2_Tbox1" class="BodyText" /><BR>
</td>
</tr><tr>
I am adding an event to a checkbox in a TemplateColumn inside a grid, but cant seem to fire the event, or maybe I am not capturing the value of the checkbox. This is how the code is currently:
The default page loads a control in the body, that contains a DropDownList. User selects a value and DataGrid is populated. The Grid is created at design time and so are the checkboxes:
<code>
<div style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 250px"><asp:datagrid id="DataGrid1" runat="server" CssClass="BodyText" ShowHeader="False" Width="100%"
AutoGenerateColumns="False">
<HeaderStyle Font-Size="Smaller" Font-Names="Arial" HorizontalAlign="Center" BackColor="Gray"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="c_Category" SortExpression="Category" HeaderText="Category">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="c_Sub_Category" SortExpression="SubCategory" HeaderText="Sub Category">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="c_WebNum">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Width="20%"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Task">
<HeaderStyle Width="30%"></HeaderStyle>
<ItemStyle Width="30%"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" ID="Label5" Text='<%# DataBinder.Eval(Container, "DataItem.c_Task") %>'>
</asp:Label><br>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.c_Task") %>' >
</asp:TextBox><br>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="*TC">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle Width="25%"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"></asp:CheckBox><BR>
<aspropDownList id=ComplianceList runat="server" CssClass="BodyText" DataSource="<%# GetCompliance() %>" DataTextField="c_Desc" DataValueField="i_ColVal">
</aspropDownList><BR>
<asp:TextBox id="Textbox5" runat="server" EnableViewState="False" Text=""></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="*DO">
<HeaderStyle Width="25%"></HeaderStyle>
<ItemStyle Width="25%"></ItemStyle>
<ItemTemplate>
<INPUT id="shiv2" type="checkbox" runat="server"><BR>
<asp:Label id="Label6" Runat="server">If not compliant,enter directions:</asp:Label><BR>
<asp:TextBox id="Tbox1" runat="server" CssClass="BodyText" EnableViewState="False" Text="Compliant by 8/19/2004">Compliant by 8/19/2004</asp:TextBox><BR>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False"></asp:BoundColumn>
</Columns>
<PagerStyle VerticalAlign="Bottom" PageButtonCount="15" Mode="NumericPages"></PagerStyle>
</asp:datagrid></div>
</code>
In the codebehind, this is how I BindData() and add the event:
-The page load method:
<code>
private void Page_Load(object sender, System.EventArgs e)
{
GetCity();
}
</code>
BindData method:
<code>
private void BindData()
{
SqlCommand cm;
SqlConnection cn;
cn = new SqlConnection("..;");
cn.Open();
cm = new SqlCommand("dbo.GetAudit",cn);
cm.CommandType=CommandType.StoredProcedure;
//Add the parameters to be passed
cm.Parameters.Add("@city",SqlDbType.Int,4);
//Assign values to the parameters to be input
cm.Parameters["@city"].Value=CityList.SelectedItem.Value;
//Execute the stored procedure
cm.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cm);
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
Session.Add("myset2",ds);
cn.Close();
lblShow.Text = "Showing Tasks for " + CityList.SelectedItem.Text + " office.";
}
-Adding the event upon ItemCreated:
private void grid_ItemCreated( object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType elemType = e.Item.ItemType;
if ((elemType == ListItemType.Item)||(elemType == ListItemType.AlternatingItem))
{
// find the checkbox inside the Grid
CheckBox CheckBox1 = (CheckBox) e.Item.Cells[5].FindControl("CheckBox1");
// register event in the check box
CheckBox1.CheckedChanged += new EventHandler(this.OnChangeHandler);
}
}
protected void OnChangeHandler(object sender, System.EventArgs e)
{
CheckBox CheckBox1 = (CheckBox)sender;
DataGridItem GridItem = (DataGridItem)CheckBox1.Parent.Parent;
///if (CheckBox1.Checked == true)
//{
Label2.Text = CheckBox1.Checked.ToString();
//}
}
</code>
-Init component:
<code>
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grid_ItemCreated);
this.Load += new System.EventHandler(this.Page_Load);
</code>
Now the grid renders just fine when i click on the submit button after selecting from the dropdown. But as soon as I click on the Checkbox, the label doesnt get updated and the grid disappears. And here is the output as redered:
<div style="OVERFLOW: auto; WIDTH: 100%; HEIGHT: 250px"><table class="BodyText" cellspacing="0" rules="all" border="1" id="_ctl5_DataGrid1" style="width:100%;border-collapse:collapse;">
<tr>
<td style="width:10%;">General Office Procedures</td><td style="width:10%;">Office Maintenance</td><td style="width:30%;">
<span id="_ctl5_DataGrid1__ctl2_Label5">Ensure Regulatory and HR Compliance in the Field Office</span><br>
</td><td style="width:25%;">
<input id="_ctl5_DataGrid1__ctl2_CheckBox1" type="checkbox" name="_ctl5ataGrid1:_ctl2:CheckBox1" onclick="__doPostBack('_ctl5$DataGrid1$_ctl2$CheckBox1','')" language="javascript" /><BR>
<select name="_ctl5ataGrid1:_ctl2:ComplianceList" id="_ctl5_DataGrid1__ctl2_ComplianceList" class="BodyText">
<option value="1">Procedures are not followed</option>
<option value="2">Wrong person is performing the task</option>
<option value="3">Task is performed at the wrong frequency</option>
<option value="4">Not applicable - please provide details</option>
<option value="5">Other - please provide details</option>
</select><BR>
<input name="_ctl5ataGrid1:_ctl2:Textbox5" type="text" id="_ctl5_DataGrid1__ctl2_Textbox5" />
</td><td style="width:25%;">
<input name="_ctl5ataGrid1:_ctl2:shiv2" id="_ctl5_DataGrid1__ctl2_shiv2" type="checkbox" /><BR>
<span id="_ctl5_DataGrid1__ctl2_Label6">If not compliant,enter directions:</span><BR>
<input name="_ctl5ataGrid1:_ctl2:Tbox1" type="text" value="Compliant by 8/19/2004" id="_ctl5_DataGrid1__ctl2_Tbox1" class="BodyText" /><BR>
</td>
</tr><tr>