复选框将在数据列表中的特定条件下被选中 [英] checkboxes will be checked on particular condition in datalist
问题描述
我正在显示带有复选框的数据列表.使用此代码处理所有子类别..
< asp:DataList ID =" ="span> runat =" 服务器" RepeatColumns 3" RepeatDirection =" >>
< ItemTemplate >
< asp:CheckBox ID =" runat 服务器" 文本 <%#Eval(" )%> ' / >
< /ItemTemplate >
< /asp:DataList >
我的要求是从子类别表中显示子类别. ypinformation1表中存在的子类别应选中,其他子类别将不选中.
aspx.cs:-
-----------
受保护的 无效 Page_Load(对象发件人,EventArgs e)
{
如果(会话[" ] != 空&&会话[" ]!= " )
{
UPOBJ.U_UserID = Convert.ToString(会话[" ]);;
UPOBJ.U_UserType = Convert.ToInt32(会话[" ]));
会话[" ] = UPOBJ.U_UserID;
}
其他
{
Response.Redirect(" + Request.Url.PathAndQuery.Replace (" ," ^"));
}
如果(Request.QueryString [" ]!= 空&& Request.QueryString [" ]!= " )
{
LPOBJ.L_YPID = Convert.ToInt32(Request.QueryString [" ]);
}
其他
{
AlertMessage(" );
Response.Redirect(" );
}
如果(!IsPostBack)
{
dr = ABLOBJ.GetYPInformationListingByID(LPOBJ);
如果(dr.HasRows)
{
while (dr.Read())
{
txtYPID.Text = dr [" ].ToString();
txtCategory.Text = dr [" ]!= 空? Convert.ToString(dr [" ]): " ;
txtSubCategory.Text = dr [" ]!= 空? Convert.ToString(dr [" ]): " ;
txtCompanyName.Text = dr [" ]!= 空? Convert.ToString(dr [" ]): " ;
UPOBJ.U_Category = txtCategory.Text.ToString();
}
}
ds = ABLOBJ.GetRelatedSubCategories(UPOBJ);
ds1 = ABLOBJ.GetSelectedSubCategories(LPOBJ);
subcategorydatalist.DataSource = ds;
subcategorydatalist.DataBind();
}
}
所有正在显示的子类别都由数据集ds显示..但我对ds1中存在哪些子类别的子类别已被检查剩余的要求未选中. div>
首先在您的数据库中创建一个函数
IF EXISTS(SELECT * FROM sysobjects WHERE ID = OBJECT_ID(``StringSplitInTableFunction''))
删除功能StringSplitInTableFunction
GO
创建函数StringSplitInTableFunction
(
@inputString VARCHAR(8000)
)
返回@splitStringTable TABLE(sID VARCHAR(20))
AS
开始
宣告@sTemp VARCHAR(10)
当LEN(@inputString)> 0
开始
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))
插入@splitStringTable值(@sTemp)
END
返回
END
转到
并替换您的查询
从SubCategories中选择SubCategory,其中(@subcategorys)中的convert(nvarchar,SubCategoryID)
由
从SubCategories中选择SubCategory,其中(StringSplitInTableFunction(@subcategorys))中的SubCategoryID
并尝试,如果有任何问题,然后回复
声明 nvarchar (声明 @ sQuery NVarchar (最大值) 设置 @ subcategorys =(选择其他子类别 from ypinformation1 where ypid = 10) 集合 @ sQuery = ' 从SubCategories中选择SubCategory,其中SubCategoryID位于(' + @ subcategorys + ' )' exec sp_executesql @ sQuery
这是我的示例代码,您可以通过
< asp:datalist id ="subcategorydatalist" runat ="server" repeatcolumns ="3" repeatdirection ="Vertical" xmlns:asp =#unknown">
< itemtemplate>
< asp:checkbox id ="chksubcategory" runat ="server" text =<%#Eval(" id)="%& gt;" ="="已选中=<%#Eval("已选择)=">
受保护的子Page_Load(以对象的ByBy发件人身份,以System.EventArgs的ByVal e身份)处理Me.Load
将empTable变暗为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)
将empTable1变暗为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)
对于empTable.Rows中的每个dr
如果empTable1.Select("Id ="& dr("Id")).Length> 0然后
dr("Selected")= True
如果结束
下一个
subcategorydatalist.DataSource = empTable
subcategorydatalist.DataBind()
结束子
在这里,我直接使用数据表.在您的情况下,您可以在ds表中有一个boolen列,其中包含所有复选框.
并将这些复选框的值设置为true,这些复选框位于ds1中.
Hi I am displaying datalist with checkboxes. Dispalying all subcategories using this code..
<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:--
-----------
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
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
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
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")="" %>"="" 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.
这篇关于复选框将在数据列表中的特定条件下被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!