Link to home
Start Free TrialLog in
Avatar of Jay
Jay

asked on

The name 'chkSelect' does not exist in the current context/server tag not well formed

Im trying to get checkboxes in one gridview to filter to another gridview. I think I got this error because of ' (ListItem item in chkSelect.Items)' when I didn't use checkboxlist. What do I change in the codes??


 <asp:GridView ID="grdEventType" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="EventType_ID" datasourceid="sdsEventType" ForeColor="Black" GridLines="Vertical" Width="50%" OnSelectedIndexChanged="grdEventType_SelectedIndexChanged">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <Columns>                                    
                <asp:TemplateField HeaderText="Select">
        <HeaderTemplate>
            <asp:CheckBox ID="chkAll" runat="server" onclick="javascript:SelectAllCheckboxes(this);" />
    </HeaderTemplate>
    <ItemTemplate>
       <asp:CheckBox ID="chkSelect" runat="server"AutoPostBack="true" OnSelectedIndexChanged="Type_Selected" />
    </ItemTemplate>
   </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"/>
                <asp:BoundField DataField="EventType_ID" HeaderText="EventType_ID" ReadOnly="True" SortExpression="EventType_ID" />
                <asp:BoundField DataField="EventTypeName" HeaderText="EventTypeName" SortExpression="EventTypeName" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#808080" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView>
        <asp:SqlDataSource ID="sdsEventType" runat="server" ConnectionString="<%$ ConnectionStrings:EventContext %>" DeleteCommand="DELETE FROM [EventType] WHERE [EventType_ID] = @EventType_ID" InsertCommand="INSERT INTO [EventType] ([EventType_ID], [EventTypeName]) VALUES (@EventType_ID, @EventTypeName)" SelectCommand="SELECT * FROM [EventType]" UpdateCommand="UPDATE [EventType] SET [EventTypeName] = @EventTypeName WHERE [EventType_ID] = @EventType_ID">
            <DeleteParameters>
                <asp:Parameter Name="EventType_ID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="EventType_ID" Type="Int32" />
                <asp:Parameter Name="EventTypeName" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="EventTypeName" Type="String" />
                <asp:Parameter Name="EventType_ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <script lang="javascript">
 function SelectAllCheckboxes(spanChk){
   // Added as ASPX uses SPAN for checkbox
   var oItem = spanChk.children;
   var theBox= (spanChk.type=="checkbox") ? 
        spanChk : spanChk.children.item[0];
   xState=theBox.checked;
   elm=theBox.form.elements;

   for(i=0;i<elm.length;i++)
     if(elm[i].type=="checkbox" && 
              elm[i].id!=theBox.id)
     {
       if(elm[i].checked!=xState)
         elm[i].click();
     }
 }
</script>   

Open in new window


 private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["EventContext"].ConnectionString;
        string query = "SELECT EventDetail_ID, EventType_ID, EventDetailName, eventDate, eventPrice, eventDesc, eventLocation, startTime, endTime, eventImage, eventParticipants FROM EventDetail";

        string condition = string.Empty;
        foreach (ListItem item in chkSelect.Items)
        {
            condition += item.Selected ? string.Format("'{0}',", item.Value) : string.Empty;
        }

        if (!string.IsNullOrEmpty(condition))
        {
            condition = string.Format(" WHERE EventType_ID IN ({0})", condition.Substring(0, condition.Length - 1));
        }

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query + condition))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    cmd.Connection = con;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        grdDetails.DataSource = dt;
                        grdDetails.DataBind();
                    }
                }
            }
        }
    }

    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDetails.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }

    protected void Type_Selected(object sender, EventArgs e)
    {
        this.BindGrid();
    }

Open in new window

Avatar of jitendra patil
jitendra patil
Flag of India image

<ItemTemplate>
       <asp:CheckBox ID="chkSelect" runat="server"AutoPostBack="true" OnSelectedIndexChanged="Type_Selected" />
    </ItemTemplate>

There is no space between "Server" and AutopostBack property.

where is chkSelect declared ?

if you are not using checkboxlist then you should remove the foreach  loop. Either you will have to maintain the checkbox checked status in a global variable which could hold the checkboxes checked status.

Hope this helps.
Avatar of Jay
Jay

ASKER

Error: The name 'chkSelect' does not exist in the current context is still here~ I didn't use checkboxlist!! how should I modify my codes?:( Below is the part where there is an error!! Thankyou btw!!

 private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["EventContext"].ConnectionString;
        string query = "SELECT EventDetail_ID, EventType_ID, EventDetailName, eventDate, eventPrice, eventDesc, eventLocation, startTime, endTime, eventImage, eventParticipants FROM EventDetail";

        string condition = string.Empty;
        foreach (ListItem item in chkSelect.Items)
        {
            condition += item.Selected ? string.Format("'{0}',", item.Value) : string.Empty;
        }

        if (!string.IsNullOrEmpty(condition))
        {
            condition = string.Format(" WHERE EventType_ID IN ({0})", condition.Substring(0, condition.Length - 1));
        }

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query + condition))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    cmd.Connection = con;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        grdDetails.DataSource = dt;
                        grdDetails.DataBind();
                    }
                }
            }
        }
    }

Open in new window

The above error is because the chkselect is a checkbox that is inside the Gridview, which will be rendered in all the rows, so in the bindgrid function, how would you know, which checkbox is checked of which row.

to get this you will have to loop through the gridview rows find the checkbox control and cast is as checkbox then get the checked status.

for example
foreach (GridViewRow row in grdDetails.Rows)
{
   if (((CheckBox)row.FindControl("chkselect")).Checked)
   {
    //read the label            
   }            
}

I've Modified your code(Not Tested), please check below
 private void BindGrid()
    {
        string constr = ConfigurationManager.ConnectionStrings["EventContext"].ConnectionString;
        string query = "SELECT EventDetail_ID, EventType_ID, EventDetailName, eventDate, eventPrice, eventDesc, eventLocation, startTime, endTime, eventImage, eventParticipants FROM EventDetail";

        string condition = string.Empty;

	foreach (GridViewRow row in grdDetails.Rows)
	{
           CheckBox chk = (CheckBox)row.FindControl("chkselect");
	   if (chk.Checked)
	   {
	     condition += string.Format("'{0}',", chk.Value);            
	   }            
	}

        if (!string.IsNullOrEmpty(condition))
        {
            condition = string.Format(" WHERE EventType_ID IN ({0})", condition.Substring(0, condition.Length - 1));
        }

        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query + condition))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    cmd.Connection = con;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        grdDetails.DataSource = dt;
                        grdDetails.DataBind();
                    }
                }
            }
        }
    }

Open in new window

Hope this helps
Avatar of Jay

ASKER

Hi!! Now it says
'System.Web.UI.WebControls.CheckBox' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Web.UI.WebControls.CheckBox' could be found (are you missing a using directive or an assembly reference?)

for
 condition += string.Format("'{0}',", chk.Value);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jitendra patil
jitendra patil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay

ASKER

[NullReferenceException: Object reference not set to an instance of an object.]
for
if (chk.Checked)

Open in new window

now :/
Avatar of Jay

ASKER

its been solved but thanks for yr help !!:)