Passing variables to usercontrol inside the listview (asp.net, c#) - c#

I want to pass some dynamic information from the listview to UserControl, but I guess I'm missing something.
.aspx page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource"
DataKeyNames="id_Image">
<ItemTemplate>
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description")%>' ID="info1" runat="server" />
</ItemTemplate>
</asp:ListView>
.ascx file:
Name:
<asp:Label ID="NameLabel" runat="server" />
Description:
<asp:Label ID="DescriptionLabel" runat="server" />
.ascx codebehind file:
public string Name_Lbl { get; set; }
public string Description_Lbl { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Text = Name_Lbl;
DescriptionLabel.Text = Description_Lbl;
}
Everything is working fine if Im trying to get value from string text like:
<uc1:Info Name_Lbl="Name" Description_Lbl="Description" ID="info1" runat="server" />
But when I'm trying to get value from Datasource, the string values in usercontrol are "null"
Can anyone see what I'm doing wrong? Thanks, Jim Oak

DataBinding occurs a lot later in the Control Life cycle than Load.
You assign your text on Load, but your control only receives the text on DataBind
To fix this you can set your text OnPreRender. This occurs after DataBind
protected override void OnPreRender(EventArgs e)
{
NameLabel.Text = Name_Lbl;
DescriptionLabel.Text = Description_Lbl;
}

Everything looks fine in your code just check the code line:
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description"%>' ID="info1" runat="server" />
You are missing the closing bracket ")" against Description_Lbl. It should be:
<uc1:Info Name_Lbl='<%# Bind("Name") %>' Description_Lbl='<%# Bind("Description")%>' ID="info1" runat="server" />

Related

how can I get the label where in the ItemTemplate with code behind C#?

I have a label where in the Itemtemplate ,I want to get the value of the label but
just can't find control with the label, please somebody help me? thank you
aspx code:
<asp:TemplateField HeaderText="工號" SortExpression="BS_ID">
<ItemTemplate>
<asp:Label ID="lblBSID" runat="server"
Text='<%# Eval("BS_ID") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox ID="tbBS_ID_edit" runat="server" Text='<%# Eval("BS_ID") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="BS_ID_tb" Text="SK" MaxLength="7" runat="server" />
</FooterTemplate>
</asp:TemplateField>
code
This is what I var
//string lb_BS_ID = ((Label)GridView1.FindControl("lblBSID")).Text;
//this._userID = lb_BS_ID;
and I also try
//string lb_BS_ID = ((Label)GridView1.Rows[0].FindControl("lblBSID")).Text;
//this._userID = lb_BS_ID;
Also
//string lb_BS_ID =((Label)GridView1.Rows[e.RowIndex].Cells[0]FindControl("lblBSID")).Text;
//this._userID = lb_BS_ID;
but those are not work for me please help me><
instead of this:
string lb_BS_ID = ((Label)GridView1.FindControl("lblBSID")).Text;
Try this. :
Lable label = (Label)GridView1.Rows[e.RowIndex].FindControl("lblBSID");
string value = label.Text;
Note : If you just need to find the value of the label in first row then use Rows[0] and so on instead of e.RowIndex.
I hope it helps.
for updating the record use "OnRowUpdating" event of gridview;
while using your code 1,2 and 3 make sure that your grid is binded to some data.
you can not access any cntrol until your grid is binded to some data
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int trackingno = (int)GridView1.DataKeys[e.RowIndex].Value;
Label trackingno = (Label)GridView1.Rows[e.RowIndex].FindControl("yourLabelControlID");
//perform your update function
}
for other operations you can use "OnRowDataBound"
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddeditpickuprider = (DropDownList)e.Row.FindControl("yourDropdownControlID");
}

How to change the layout of QueryableFilterRepeater?

I am using an ASP.NET Dynamic Data Entities web application which contains a QueryableFilterRepeater server control inside the content placeholder on page List.aspx, when I execute the website, the filter control shows all the filters (Label Name along with the corresponding Drop Down) vertically.
Is there any way we can change the layout and display the filters horizontally?
Please find the .aspx code below
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged" />
</ItemTemplate>
</asp:QueryableFilterRepeater>
The corresponding .cs file code:-
protected void Label_PreRender(object sender, EventArgs e)
{
Label label = (Label)sender;
DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
if (fuc != null && fuc.FilterControl != null)
{
label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
}
}
protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
{
GridView1.PageIndex = 0;
}
Need suggestions.
Thanks in advance.
I usually wrap the control in a span element and then use CSS to style it the way that i want:
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<span class="filter">
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged"/>
</span>
</ItemTemplate>

Creating a query string when clicking on image (asp.net/c#)

My application is an image gallery and with a Repeater control i'm listing the thumbnails (that's in a separate folder, apart from the full scale images). When clicking on a thumbnail a full scale image should be shown in the Image control "fullSizeImage" and a query string should be created which (with a GET of the page) shows that specific image in full scale.
The code for the query string is done, but the problem is that I don't have a clue where to put it (the creation of the query), because the HyperLink control doesn't support event clicks. Is there a way to use for example Repeater ItemCommand, or how could I accomplish what I want here?
Thanks!
from default.aspx:
<asp:Image ID="fullSizeImage" runat="server" />
<asp:Repeater ID="ImageRepeater" runat="server" DataSourceID="" >
<ItemTemplate>
<asp:HyperLink ID="ImageHyperLink" NavigateUrl='<%# Eval("Name", "~/Images/{0}") %>' runat="server" CssClass="thumbnails" >
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Name", "~/Images/Thumbnails/{0}") %>' CssClass="thumbnail" />
</asp:HyperLink>
</ItemTemplate>
</asp:Repeater>
from code behind:
protected void Page_Load(object sender, EventArgs e) {
var directory = new DirectoryInfo(Gallery.PhysicalApplicationPath + "/Images");
var theFiles = directory.GetFiles();
ImageRepeater.DataSource = theFiles;
ImageRepeater.DataBind();
var dataName = Request.QueryString["name"];
fullSizeImage.ImageUrl = dataName;
}
the creation of the query string (that I don't know where to put):
string str = ImageUrl; <- the url of the clicked image
Response.Redirect("default.aspx?name=" + Server.UrlEncode(str);
This works with me
<asp:HyperLink ID="ImageHyperLink" NavigateUrl='<%# "~/default.aspx?name=" + Server.UrlEncode(Eval("Name","~/Images/{0}")) %>' runat="server" CssClass="thumbnails" >
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Name", "~/Images/Thumbnails/{0}") %>' CssClass="thumbnail" />
</asp:HyperLink>
In the code behind you can set up a method tied to the Repeater's ItemDataBound event. In that method you can retrieve the current file, find the HyperLink, and set the link's NavigateUrl to be the string you are generating. Something like the following:
ImageRepeater.ItemDataBound += new RepeaterItemEventHandler(ImageRepeater_ItemDataBound);
private void ImageRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
[File] f = (File)e.Item.DataItem;
HyperLink ImageHyperLink = (HyperLink)e.Item.FindControl("ImageHyperLink");
string str = f.ImageUrl;
ImageHyperLink.NavigateUrl = "default.aspx?name=" + Server.UrlEncode(str);
}

Why Link Button control variable doesn't get any value?

I created LinkButton that located inside of Repeater Control.
CategoryID is a variable in LinkButton Control that have to get value after Repeater Control was bound to data. But CategoryID always get zero.
I have the following ASP and C# code:
<asp:Repeater ID="rpt1" runat="server"
OnItemDataBound="rpt1_ItemDataBound"
OnItemCommand="rpt1_ItemCommand">
<ItemTemplate>
<div>
<%# Eval("Name") %>-<%# Eval("CollectionType")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="[edit item]"
PostBackUrl='AddItem.aspx?CategoryID=<%# Eval("CollectionID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<GlassesCollection> gc = BL.GetDataBL.GetCollection();
rpt1.DataSource = gc;
rpt1.DataBind();
}
}
Any idea why CategoryID variable doesn't get any value and how can I fix the problem?
A server control parameter cannot contain a mixture of literal text and evaluated expressions.
The code you have will literally be posting back to AddItem.aspx?CategoryID=<%# Eval("CollectionID")%> and it will not be evaluating the code within the angle brackets.
You need to change your parameter like so
PostBackUrl='<%# "AddItem.aspx?CategoryID=" + Eval("CollectionID")%>' />

If statement in repeater

I have this repeater on my page..Under the default column what I want is that there
should be an IF condition that checks my table's "IsDEfault" field value.
If IsDefault=True then the lable below "label1" ie "Yes" should be
displayed inside the repeater else "Make DEfault" link should be displayed..
Now how do I include this IF statement as inline code in my repeater to accomplish what I want ?
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
I have an idea :-
<%# If DataBinder.Eval(Container.DataItem,"IsDefault") = "True"
Then%>
<%End If%>
How should I form the "Then" statement now?
Please help me with the proper syntax..thnx
Do I need to like make a method that checks if "IsDefault" is true or not and then call it inside of inline code in my repeater ? How do I go about it ?
[EDIT]
I tried as follows:-
<% If (Eval("Container.DataItem,"IsDefault"")="True"?
("<asp:LinkButton ID="lnk1" Text="Set as Default" CommandName="SetDefault1" runat="server" CommandArgument='<%#Eval("User1ID") %>'
CausesValidation="false" Visible=true></asp:LinkButton>") : ("<asp:Label ID="label1" Text="Yes" runat="server" Visible=true></asp:Label>")
)%>
didnt work :( Help!!
If you want some control to be visible only on some condition, set the Visible property according to that condition:
<asp:Label ID="label1" Text="Yes" runat="server"
Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault") %>" />
EDIT
If you want the control INvisible for the "IsDefault" situation, reverse the test with something like Visible="<%# DataBinder.Eval(Container.DataItem,"IsDefault")==False %>".
I'm not quite sure about the exact syntax, but you should get the idea.
Here's your repeater markup. Notice both controls are hidden at the start:
<asp:Repeater runat="server" ID="rpt1" OnItemDataBound="rpt1_ItemDataBound" onitemcommand="rpt1_ItemCommand">
<ItemTemplate>
<p>
ID: <%# Eval("Id") %>
IsDefault: <%# Eval("IsDefault") %>
Name: <%# Eval("Name") %>
<asp:Label BackColor="Blue" ForeColor="White" runat="server" ID="lDefault" Text="DEFAULT" Visible="false" />
<asp:Button runat="server" ID="btnMakeDefault" Text="Make Default" Visible="false" CommandArgument='<%# Eval("Id") %>' />
</p>
</ItemTemplate>
</asp:Repeater>
And some code to go with it. Note I've simulated the retrieval of your collection of blluser objects, so there is some additional code there relating to this which you won't require as, presumably the bllusers collection that you bind to is coming from a db or something?
Anyway I think this is what you're looking for, but let me know if its not ;-)
//Dummy object for illustrative purposes only.
[Serializable]
public class bllUsers
{
public int Id { get; set; }
public bool isDefault { get; set; }
public string Name { get; set; }
public bllUsers(int _id, bool _isDefault, string _name)
{
this.Id = _id;
this.isDefault = _isDefault;
this.Name = _name;
}
}
protected List<bllUsers> lstUsers{
get
{
if (ViewState["lstUsers"] == null){
ViewState["lstUsers"] = buildUserList();
}
return (List<bllUsers>)ViewState["lstUsers"];
}
set{
ViewState["lstUsers"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
buildGui();
}
}
private List<bllUsers> buildUserList(){
lstUsers = new List<bllUsers>();
lstUsers.Add(new bllUsers(1, false, "Joe Bloggs"));
lstUsers.Add(new bllUsers(2, true, "Charlie Brown"));
lstUsers.Add(new bllUsers(3, true, "Barack Obama"));
return lstUsers;
}
private void buildGui()
{
rpt1.DataSource = lstUsers;
rpt1.DataBind();
}
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
bllUsers obj = (bllUsers)e.Item.DataItem;//this is the actual bllUser the row is being bound to.
//Set the labels
((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault;
((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault;
//Or use a more readable if/else if you want:
if (obj.isDefault)
{
//show/hide
}
else
{
//set visible/invisible
}
}
}
Hope this helps :-)
Sorry to say you to be honest i was unable to understand what actually you wanted to do
If your looking to use the condition in the Item Templet then i think
the following systax will help you
<asp:LinkButton ID="Label1" runat="server"
Text='<%# ((Eval("Cond"))="True" ? Eval("Result for True") : Eval("Result for False") )%>'></asp:LinkButton>

Categories

Resources