PDA

View Full Version : مقدار دادن به یک لیبل داخل datalist با استفاده از یک تابع



b.paseban
پنج شنبه 22 اردیبهشت 1390, 16:15 عصر
سلام.
من یه دیتا لیست دارم که یسری دیتا رو بصورت category نمایش میده و میخوام برای هر دسته بندی از اون دیتا ها تعداد دیتا های زیر مجموعه هر دسته رو نشون بدم.
یه تابع نوشتم که درست عمل میکنه و تعداد هر زیر مجموعه(مثلا تعداد عکسهای هر مجموعه) رو پیدا میکنه.من میخوام این مقدار رو توی دیتا لیست مربوطه نشون بدم اما نمیدونم چطوری باید اون تابع رو به اون کنترل مربوطه (مثلا یه lable) وصل کنم.
این کد منه برای تابع:


public static int GalleryCount(int CategoryGalleryID)
{
SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrin gs["CS_Hairdresser"].ConnectionString);
connect.Open();
string commandinsert = "sp_Tbl_GalleryImg_SelectCountImages";
SqlCommand command = new SqlCommand(commandinsert, connect);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("CategoryGalleryID", CategoryGalleryID);
int result = (int)command.ExecuteScalar();
return result;
}

اینم SP:


CREATE Procedure [dbo].[sp_Tbl_GalleryImg_SelectCountImages]
@CategoryGalleryID Int
As
Select Count (*) From Tbl_GalleryImg WHERE [CategoryGalleryID] = @CategoryGalleryID

اینم کدای مربوط به DataList:


<asp:DataList ID="DtcatGallery" runat="server" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyField="CategoryGalleryID"
DataSourceID="SQDSCategoryGallery" GridLines="Vertical" RepeatColumns="5"
OnItemCommand="DtcatGallery_ItemCommand"
onitemdatabound="DtcatGallery_ItemDataBound" >
<ItemTemplate>
<div>
<asp:Literal ID="CatID" runat="server" Text='<%#Eval("CategoryGalleryID") %>' Visible="false"></asp:Literal>
</div>
<div>
<asp:ImageButton ID="ImgBtnShow" runat="server" ImageUrl='<%#Eval("ImgShow")%>' CommandArgument='<%#Eval("CategoryGalleryID")%>'
CommandName="DoCounter" />
</div>
<div>
<%#Eval("Title")%>
</div>
<div>
<%#Eval("CountViewed")%>
</div>
<div>
<asp:Label ID="lblcount" runat="server"></asp:Label>
</div>
</ItemTemplate>
</asp:DataList>

برای نمایش هر دسته با CategoryGalleryID منحصربفرد اون گروه داخل لیبل با ID lblcount چطوری باید عمل کنیم؟
آیا چیزی مثل GridView_RowDataBound توی DataList هم هست که ما بتونیم از اون برای پیدا کردن کنترل مورد نظر و مقدار دهی توی CodeBehind ازش استفاده کنیم؟

b.paseban
پنج شنبه 22 اردیبهشت 1390, 16:35 عصر
حل شد.


protected void DtcatGallery_ItemDataBound(object sender, DataListItemEventArgs e)
{
Literal CatID = (Literal)e.Item.FindControl("CatID");
Label lblcount = (Label)e.Item.FindControl("lblcount");
lblcount.Text = SDKClass.GalleryCount( Int32.Parse(CatID.Text)).ToString();
}