Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am displaying datalist with checkboxes. Dispalying all subcategories using this code..
XML
<asp:DataList ID="subcategorydatalist" runat="server" RepeatColumns="3" RepeatDirection="Vertical">
    <ItemTemplate>
        <asp:CheckBox ID="chksubcategory" runat="server" Text='<%# Eval("SubCategory") %>' />
    </ItemTemplate>
</asp:DataList>


My requirement is that the subcategories are displayed from subcategories table. Subcategories which are present in ypinformation1 table should be checked and other will be unchecked.

aspx.cs:--
-----------
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserID"] != null && Session["UserID"] != "")
        {
            UPOBJ.U_UserID = Convert.ToString(Session["UserID"]);
            UPOBJ.U_UserType = Convert.ToInt32(Session["UserType"]);
            Session["ID"] = UPOBJ.U_UserID;
        }
        else
        {
            Response.Redirect("~/Login.aspx?ReturnUrl=" + Request.Url.PathAndQuery.Replace("&", "^"));
        }

        if (Request.QueryString["YPID"] != null && Request.QueryString["YPID"] != "")
        {
            LPOBJ.L_YPID = Convert.ToInt32(Request.QueryString["YPID"]);
        }
        else
        {
            AlertMessage("Please Select the Record For Editing");
            Response.Redirect("EditListing.aspx");
        }
        if (!IsPostBack)
        {
            dr = ABLOBJ.GetYPInformationListingByID(LPOBJ);
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    txtYPID.Text = dr["YPID"].ToString();
                    txtCategory.Text = dr["Category"] != null ? Convert.ToString(dr["Category"]) : "";
                    txtSubCategory.Text = dr["SubCategory"] != null ? Convert.ToString(dr["SubCategory"]) : "";
                    txtCompanyName.Text = dr["CompanyName"] != null ? Convert.ToString(dr["CompanyName"]) : "";
                    UPOBJ.U_Category = txtCategory.Text.ToString();
                }
            }
            ds = ABLOBJ.GetRelatedSubCategories(UPOBJ);
            ds1 = ABLOBJ.GetSelectedSubCategories(LPOBJ);


            subcategorydatalist.DataSource = ds;
            subcategorydatalist.DataBind();

        }

    }


all subcategories are displaying are displaying by the dataset ds..but my requirement which subcategories are present in ds1 that subcategories are checked remaining are unchecked..there is any chance for comparing the datasets
Posted
Updated 25-Apr-11 21:04pm
v3
Comments
Ankur\m/ 22-Apr-11 3:32am    
You need to write a query and get required results from database. Then bind that table to your datalist.
BHANU PRAKASH BYSANI 22-Apr-11 4:30am    
my requirement is dispalying all subcategories along with selected subcategories
Ankur\m/ 22-Apr-11 5:12am    
So write a query to do that. What's the problem?
BHANU PRAKASH BYSANI 22-Apr-11 5:52am    
iam dispalying all subcategories with chesckboxes..when dispalying how can put the condition already they are in ypinformation table taht subcategories checkboxes were chekced true..
mksharma38 22-Apr-11 7:21am    
unable to understand ur problem, what is ypinformation1

First create a finction in ur database

IF EXISTS(SELECT * FROM sysobjects WHERE ID = OBJECT_ID('StringSplitInTableFunction'))
DROP FUNCTION StringSplitInTableFunction
GO
CREATE FUNCTION StringSplitInTableFunction
(
@inputString VARCHAR(8000)
)
RETURNS @splitStringTable TABLE(sID VARCHAR(20))
AS
BEGIN
DECLARE @sTemp VARCHAR(10)
WHILE LEN(@inputString) > 0
BEGIN
SET @sTemp = LEFT(@inputString, ISNULL(NULLIF(CHARINDEX(',', @inputString) - 1, -1),
LEN(@inputString)))
SET @inputString = SUBSTRING(@inputString,ISNULL(NULLIF(CHARINDEX(',', @inputString), 0),
LEN(@inputString)) + 1, LEN(@inputString))
INSERT INTO @splitStringTable VALUES (@sTemp)
END
RETURN
END
Go

And replace ur query
select SubCategory from SubCategories where convert(nvarchar,SubCategoryID) in (@subcategorys)
by
select SubCategory from SubCategories where SubCategoryID in (StringSplitInTableFunction(@subcategorys))

and try, if any problem then reply
 
Share this answer
 
Comments
BHANU PRAKASH BYSANI 22-Apr-11 8:36am    
'StringSplitInTableFunction' is not a recognized built-in function name. THIS ERROR IAM GETTING
mksharma38 22-Apr-11 8:38am    
replace query
select SubCategory from SubCategories where SubCategoryID in (StringSplitInTableFunction(@subcategorys))


by

select SubCategory from SubCategories where SubCategoryID in (select sId from StringSplitInTableFunction(@subcategorys))

mksharma38 22-Apr-11 8:40am    
now r u geting subcategory names
BHANU PRAKASH BYSANI 22-Apr-11 9:00am    
ya.iam getting ..thanks..
BHANU PRAKASH BYSANI 22-Apr-11 9:03am    
plz help in condition also for checking checkboxes.iam eriting function for checked condition
SQL
declare @subcategorys nvarchar(100)

declare @sQuery NVarchar(max)

set @subcategorys =(select othersubcategories from ypinformation1 where ypid=10)

set @sQuery ='select SubCategory from SubCategories where SubCategoryID in ('+@subcategorys+')'

exec sp_executesql @sQuery
 
Share this answer
 
Here is my sample code, u can go through


<asp:datalist id="subcategorydatalist" runat="server" repeatcolumns="3" repeatdirection="Vertical" xmlns:asp="#unknown">
<itemtemplate>
<asp:checkbox id="chksubcategory" runat="server" text="<%# Eval(" id")="" %&gt;"="" checked="<%# Eval(" selected")="">




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim empTable As DataTable = New DataTable()
Dim dr As DataRow
empTable.Columns.Add(New DataColumn("Id"))
empTable.Columns.Add(New DataColumn("Selected", System.Type.GetType("System.Boolean")))
empTable.Columns("Selected").DefaultValue = False
dr = empTable.NewRow()
dr("Id") = "2"
empTable.Rows.Add(dr)
dr = empTable.NewRow()
dr("Id") = "3"
empTable.Rows.Add(dr)
dr = empTable.NewRow()
dr("Id") = "4"
empTable.Rows.Add(dr)

Dim empTable1 As DataTable = New DataTable()
empTable1.Columns.Add(New DataColumn("Id"))
dr = empTable1.NewRow()
dr("Id") = "2"
empTable1.Rows.Add(dr)
dr = empTable1.NewRow()
dr("Id") = "4"
empTable1.Rows.Add(dr)
For Each dr In empTable.Rows
If empTable1.Select("Id=" & dr("Id")).Length > 0 Then
dr("Selected") = True
End If
Next

subcategorydatalist.DataSource = empTable
subcategorydatalist.DataBind()
End Sub

Here i used directely datatable. in ur case u can have a boolen column in table of ds, which have all checkbox.
And set value true for those check box, those are in ds1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900