E_name not exist in current context - asp.net

this is my design part.
<asp:Panel ID="eventshow" Visible="false" runat="server" onclick="eventshow_click">
<asp:Repeater ID="allshowevent" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate runat="server">
<div id="eventab" runat="server">
<div>
<a runat="server" href='Event_Explore.aspx?CID=<%#Eval("CID") %>&FID=<%#Eval("FESTID") %>'>
<asp:Label ID="E_name" runat="server" Text='<%#Eval("EVENT_NAME") %>' onclick="E_nameClick"></asp:Label>
</a>
</div>
<div id="eventdate">
<p>Start Date:<%#Eval("START_DATE") %></p>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
this is .cs code
protected void E_nameClick(object sender, EventArgs e)
{
if (E_name.Text == "GROUP DANCE")
{
dan.Visible = true;
}
}
it shows e_name not exist in current context.please help me to solve this.
Thank you

Try using something like this:
protected void E_nameClick(object sender, EventArgs e)
{
Label myE_name = (Label)sender;
////if dan is inside the repeater then add the following line and replace Control with your control type
//Control dan = (Control)(((Label)sender).NamingContainer).FindControl("dan");
if (myE_name.Text == "GROUP DANCE")
{
dan.Visible = true;
}
}

Related

How to get Reference to the label in repeater item in code behind

<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
Code Behind:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
how i get the reference to the "lblMessage" in Delete_Command.
Try this:
protected void Delete_Command(object sender, CommandEventArgs e)
{
LinkButton button = (LinkButton)sender;
Label label = (Label)button.NamingContainer.FindControl("lblMessage");
// do something with the label
}
If you:
Have bound the repeater
Have ViewState enabled
Do not re-bind the repeater earlier in the post back
this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.
Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.
means are you want find a lable in Delete_Command event?
in aspx
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
<asp:Label ID="lblMessage" run="server">
</ItemTemplate>
</asp:Repeater>
in aspx.cs
protected void Delete_Command(object sender, CommandEventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
Label lblMessage = item.FindControl("lblMessage") as Label;
if (lblMessage != null)
{
lblMessage.Text = "";
}
}
}
If You want to make it in your way use following code in
protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
{
(((LinkButton)source).NamingContainer).FindControl("lblName")
}
Another approach.. But something that you can buy
aspx
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
<asp:LinkButton runat="server" CommandName="Delete_Command" Text="sd"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
protected void Delete_Command(object sender, CommandEventArgument e)
{
if(e.CommandName != null)// Conditional Check
{
Label label = e.Item.FindControl("lblMessage");
// do something with the label
}
}

Cannot access checkbox checkedchanged event inside DataList

I have checkboxes inside a datalist itemtemplate..But i cant access checkbox chechedchange event. I set AutoPostBack as true. But still cant fire event.
Here my codes.
<ul class="commentlist" >
<asp:DataList ID="datalistYorum" runat="server" DataSourceID="ods_yorumlar"
RepeatLayout="Flow" ItemStyle-Wrap="True" RepeatDirection="Horizontal"
onitemcreated="datalistYorum_ItemCreated"
onitemdatabound="datalistYorum_ItemDataBound" onload="datalistYorum_Load"
ondatabinding="datalistYorum_DataBinding">
<ItemTemplate>
<li class="comment">
<div class="comment-body">
<div class="comment-author vcard">
<div class="lightbox-photo">
<a class="image-overlay" href='<%# "Foto/profil/foto_buyuk/" + Eval("Yorum_Profil_Foto_Buyuk") %>' data-rel="prettyPhoto" title='<%# Eval("Yorum_UserName")%>'>
<img src='<%# "Foto/profil/foto_kucuk/" + Eval("Yorum_Profil_Foto_Kucuk") %>' alt='<%# Eval("Yorum_UserName")%>' class="avatar" />
</a>
</div>
<cite class="fn"><asp:HyperLink ID="linkProfil" runat="server" Text='<%# Eval("Yorum_UserName")%>' NavigateUrl='<%# "~/profil.aspx?user_id="+ Eval("User_ID") %>'></asp:HyperLink></cite>
<cite class="fn-time"><%# Eval("Yorum_Gecen_Zaman")%></cite>
</div>
<p><%# Eval("Yorum_Text")%></p>
<div class="reply"><asp:CheckBox ID="checkLike" runat="server" CssClass="comment-reply-link" AutoPostBack="True" />
<asp:ToggleButtonExtender ID="ToggleButtonLike" runat="server" TargetControlID ="checkLike" ImageHeight="32" ImageWidth="52" CheckedImageUrl="~/images/liked.png" UncheckedImageUrl="~/images/like.png" CheckedImageAlternateText="Like">
</asp:ToggleButtonExtender>
</div>
<div class="reply"><asp:CheckBox ID="checkDislike" runat="server" CssClass="comment-reply-link" AutoPostBack="True" />
<asp:ToggleButtonExtender ID="ToggleButtonDislike" runat="server" TargetControlID="checkDislike" ImageHeight="32" ImageWidth="62" UncheckedImageUrl="~/images/dislike.png" CheckedImageUrl="~/images/disliked.png">
</asp:ToggleButtonExtender>
</div>
</div>
</li>
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="ods_yorumlar" runat="server"
DataObjectTypeName="Yorum" TypeName="yonet" SelectMethod="PostYorumlariGetir"
ondatabinding="ods_yorumlar_DataBinding"
onselecting="ods_yorumlar_Selecting" onselected="ods_yorumlar_Selected">
<SelectParameters>
and code behind:
protected void datalistYorum_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
{
CheckBox checkLike = (CheckBox)e.Item.FindControl("checkLike");
CheckBox checkDislike = (CheckBox)e.Item.FindControl("checkDislike");
if (!Page.IsPostBack)
{
checkLike.CheckedChanged += new EventHandler(checkLike_CheckedChanged);
checkDislike.CheckedChanged += new EventHandler(checkDislike_CheckedChanged);
}
}
}
public void checkLike_CheckedChanged(object sender, EventArgs e)
{
object user_id = Membership.GetUser().ProviderUserKey;
DateTime event_date = DateTime.Now;
CheckBox checkLike = (CheckBox)datalistYorum.FindControl("checkLike");
if (checkLike.Checked == true)
{
try
{
using (SqlConnection baglanti = new SqlConnection(ConfigurationManager.ConnectionStrings["xxx"].ConnectionString.ToString()))
{
SqlCommand komut = new SqlCommand("sp_likeordislike", baglanti);
komut.CommandType = CommandType.StoredProcedure;
komut.Parameters.AddWithValue("#comment",1);
komut.Parameters.AddWithValue("#user_id", user_id);
komut.Parameters.AddWithValue("#likeordislike", 1);
komut.Parameters.AddWithValue("#event_date", event_date);
baglanti.Open();
komut.ExecuteNonQuery();
baglanti.Close();
}
}
catch (Exception)
{
throw;
}
}
}
But nothing happen.Thanks!
Unless you are adding the event handler for the 'checklike' Checkbox in Page_Load it looks as though the assignment is missing.
Change the 'checklike' Checkbox declaration as follows:
<asp:CheckBox ID="checkLike" runat="server" CssClass="comment-reply-link" AutoPostBack="True" OnCheckedChanged="checkLike_CheckedChanged" />
Try this:
protected void checkDislike_CheckedChanged(Object sender, EventArgs e)
{
CheckBox cb = (CheckBox) sender;
DataListItem item = (DataListItem) cb.NamingContainer;
//
//
}
Your markup will look like this:
<asp:CheckBox ID="checkDislike" runat="server" CssClass="comment-reply-link" AutoPostBack="True" OnCheckedChanged="checkDislike_CheckedChanged" />
Before trying this comment out all the other checkbox events
Check this link:

how do i get access to the label inside gridview/repeater

as you can see in my code... i have a label inside ItemTemplate and what i want is when i click on that particular control i would like to access to the label so that i can update the status...
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1" OnItemCreated="Repeater1_ItemCreated" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
Book:
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<Mycontrol:Content1 ID="EmpControl" runat="server" OnMyControlClick="EmpControl_clicking" />
<br />
</ItemTemplate>
</asp:Repeater>
protected void EmpControl_clicking(object sender, EmployeeEventArgs e)
{
// how do i get access to the lblStatus???
}
You will need to use the FindControl method to access controls within templates:
protected void EmpControl_clicking(object sender, EmployeeEventArgs e)
{
MyControl myControl = (MyControl)sender;
Label c = (Label)myControl.Parent.FindControl("lblStatus");
}

Loading repeater when binding gridview in asp.net

I have a gridview like this:
<asp:GridView ID="gv1" AutoGenerateColumns="false" BorderWidth="0" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<span style="font-family:Tahoma; font-size:14px;">
<u> <a href="<%#DataBinder.Eval(Container.DataItem,"ShUrl")%>">
<%#DataBinder.Eval(Container.DataItem,"PostTitle")%>
</a>
</u>
<br />
</span>
<asp:Repeater ID="rp1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li >
<%# Eval("TagName")%>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now I am able to load successfull ShUrl and PostTitle. I am bringing titleId from the database also.
Now as usual, a post might have several tag. So I want to load the repeater for the particular titleId.
On server side, I am simply binding the gv1 from the datable.
Now how to load tags for the titleid:
I have already written the function on the server side, might help you all to guide me:
private void LoadtagList(int titleId)
{
// calling DAL
rp1.DataSource = db.GetAllTagsForPost(titleId);
rp1.DataBind();
}
you have to use GridView RowDataBound Event for that
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
if (Convert.ToString(dr["titleId"]) != "")
{
Repeater rp1 = (Repeater)e.Row.Findcontrol("rp1");
rp1.DataSource = db.GetAllTagsForPost(titleId);
rp1.DataBind();
}
}
}

display many rows from DB by normal line?

When I have a table in database with many rows
and I want to display them by normal line (Not with Gridview). Like:
You teach these classes:
class 1/1, class 2/1, class 3/1
we take (1/1 and 2/1 and 3/1) from database.
how can I do it ?
note: I use LINQ to deal with database.
Using a repeater seems to be the easiest way to me. It would look something like this.
<div>
You teach these classes:
<asp:Repeater
ID="rptListOfClasses"
runat="server"
DataSourceID="linqDataSource" >
<ItemTemplate>
<span> class <%# Eval("ClassDate") %>, </span>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:ListView ID="lvClass" runat="server" DataSourceID="LinqDataSource1" OnItemDataBound ="lvResult_ItemDataBound" >
<LayoutTemplate>
You teach these classes:
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" ></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblClass" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Class") + ", " %>' ></asp:Label>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyDataContext" TableName="ClassList"
onselected="LinqDataSource1_Selected">
</asp:LinqDataSource>
Code Behind:
//remove the last comma(,)
public class Test : System.Web.UI.Page
{
int iCount = 0;
protected void lvResult_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label lblClass = e.Item.FindControl("lblClass") as Label;
ListViewDataItem lvItem = e.Item as ListViewDataItem ;
if (lblClass != null)
if (lvItem.DataItemIndex == (iCount - 1))
lblClass.Text = lblClass.Text.Substring(0, lblClass.Text.ToString().Length - 2);
}
}
protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
iCount = e.TotalRowCount;
}
}

Resources