-1

I have added a like button on my product listview, and the like button suppose to get the value of the ads title and Adsid and the userid whose clicked on this like btn and then the code should store this add in favorite table as it mention in the code but i have a problem as i am still biggner in c# and i dont know how fix it very well could you please help with that(fix the below code cuz I am getting error message:

"Error 3 The name 'Adstitlinkbtn' does not exist in the current context

Error 2 The name 'Labeladsid' does not exist in the current context"

and aslo i will be thankfull if you add a message to user that in case if he didnt login and he click on the likebtn will recive a message " Please login to add this ads to your favorite list".

you can find this screen record that may can explain what i am meaning

screen record

Many thanks for all of you

 protected void likebtn_Click(object sender, ImageClickEventArgs e)
    {

        SqlConnection likecn = new SqlConnection(cs);
        SqlCommand likecmd = new SqlCommand();

        string sqlstatment = "INSERT INTO favourite (AdsID, UID, AdsTit) VALUES (@AdsID,@UID,@AdsTit)";

        likecmd.Connection = likecn;
        likecmd.CommandType = CommandType.Text;
        likecmd.CommandText = sqlstatment;

        //Insert the parameters first
        likecmd.Parameters.AddWithValue("@AdsID", Labeladsid);
        likecmd.Parameters.AddWithValue("@UID", Session["UsrNme"]);
        likecmd.Parameters.AddWithValue("@AdsTit", Adstitlinkbtn.Text);

        SqlDataAdapter ad = new SqlDataAdapter(likecmd);
        DataSet ds = new DataSet();
        ad.SelectCommand = likecmd;
        ad.Fill(ds);

        Response.Write("This Ads has been added to your Fovarite List");



    }


<asp:ListView ID="adsshow" runat="server" DataSourceID="locationdatalistshow" 
        style="text-align: left" >
<ItemTemplate>


            <div class="templist">

                <asp:Label ID="Labeladsid" runat="server" Text='<%# Eval("AdsID") %>' style="color: #ffffff"></asp:Label>
                <asp:ImageButton ID="ImageButton3" runat="server" Height="88px" Width="91px" 
                CssClass="imag1" ImageUrl='<%# "/images/AdsImgs/" + Eval("Img1") %>'  
                PostBackUrl='<%# "AdsDetails.aspx?Img1=" + Eval("AdsID") %>' />


                <asp:LinkButton ID="Adstitlinkbtn" runat="server" 
                style="font-weight: 700; color: #0066FF" Text='<%# Eval("AdsTit") %>' 
                CssClass="adstit" onclick="Adstitlinkbtn_Click"   
                 PostBackUrl='<%# "AdsDetails.aspx?AdsTit=" + Eval("AdsID") %>' ></asp:LinkButton>      

            <br />
            <asp:Label ID="AdsDescLabel" runat="server" Text='<%# Eval("AdsDesc") %>' 
                CssClass="adsdisc" />
            <br /><br />
            <br /><br />

            <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' 
                    style="font-family: Arial, Helvetica, sans-serif; font-size: small" />
            &nbsp;-
            <asp:Label ID="StateLabel" runat="server" Text='<%# Eval("State") %>' 
                    style="font-family: Arial, Helvetica, sans-serif; font-size: small" />
            &nbsp;-
            <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' 
                    style="font-size: small; font-family: Arial, Helvetica, sans-serif" />


            <div class="adsprice">Price:
            <asp:Label ID="AdsPriceLabel" runat="server" style="color: #FF0000" 
                Text='<%# Eval("AdsPrice") %>' /></div>
           <br /> 
           <div class="iconadsbox">
            <asp:ImageButton ID="likebtn" runat="server" 
                   ImageUrl="~/iconsimg/favoritestar2.png" OnClick="likebtn_Click" CommandName="like" />
                &nbsp;&nbsp;&nbsp;
                <asp:ImageButton ID="Sndmailtoadder" runat="server" 
                   ImageUrl="~/iconsimg/mailposter.png" OnClick="Sndmailtoadder_Click" />
                &nbsp;&nbsp;&nbsp;

                </div>


            <asp:Image ID="Image1" runat="server" CssClass="divideline"/>

            </div>


        </ItemTemplate>
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="adsshow" PageSize="7">
    <Fields>
    <asp:NumericPagerField />
        <asp:NextPreviousPagerField />
    </Fields>
    </asp:DataPager>

        <br />
        </div>
    </LayoutTemplate>
    </asp:ListView>
Mike
  • 15
  • 7
  • 2
    At least add what's your problem... – Gusman Jun 10 '14 at 17:47
  • @Gusman thanks for your reply, I have update my post hopefully will give more explain about my problem – Mike Jun 10 '14 at 18:11
  • I saw your video, it's very strange that the intellisense says the controls do not exists. Try to clean the solution and recompile and if it fails remove the controls (labelsid and Adstitlinkbtn) and add them again with another name, if it works then rename to the correct one and it should work. I pressume it's an IDE bug. – Gusman Jun 10 '14 at 18:18

2 Answers2

0

Create a bool value to represent whether user has logged in or not (call it bool loggedIn). Wherever your code is to login (you did not post this code), set that bool to be true. Then when the user clicks the 'like' button, in your 'likebtn_Click' method, check the bool:

if(!loggedIn)
{
 MessageBox.Show("Please login to add this ads to your favorite list");
 return;
 }
TheBlindSpring
  • 631
  • 5
  • 22
  • Thanks @TheBlindSpring but what about the code I need some one to help me to fix it and store the values inside the table please check my update on my post many thanks for you – Mike Jun 10 '14 at 18:16
0

I believe you have not shown us all your markup. It appears you are inside a grid view or some other collection control. In those controls, the template layout is dynamically generated for each item. So your label and link button controls don't really exist. What you have to do is create a GridView_Command event and in that code you will have access to the controls in that item.

So basically you created a template item and those controls are generated dynamically at runtime, which is why they are not available to you at design time. If you post the rest of your mark up me or anyone else who wants can give you more specific help.

protected void adsshow_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    if (e.CommandName == "like")
    {
        var lblAds = e.Item.FindControl("Labeladsid") as Label;
        var lbtn = e.Item.FindControl("Adstitlinkbtn") as LinkButton;

        var id = lblAds.Text;
        var title = lbtn.Text;


    }
}

<asp:ListView ID="adsshow" runat="server" DataSourceID="locationdatalistshow"
        style="text-align: left" onitemcommand="adsshow_ItemCommand" >
<ItemTemplate>

...

    <asp:ImageButton ID="likebtn" runat="server" 
           ImageUrl="~/iconsimg/favoritestar2.png" CommandName="like" />

...

Noticed I added a onCommand event to the ListView, and I removed your OnClick event from the imagebutton. I'm not 100% sure about the removing the OnClick event, so if you can please confirm for me that the OnCommand event fires correctly with the OnClick removed from the ImageButton.

Adam Heeg
  • 1,704
  • 1
  • 14
  • 34
  • thanks for your reply, but there are nothing more in html code only the listview and the header and footer of the page that's it – Mike Jun 10 '14 at 19:05
  • and also I have attach the sqlconnectionstring hope that may help to find a solution – Mike Jun 10 '14 at 19:12
  • In your youtube video I see an tag. What control are you inside of? Post the whole markup please. – Adam Heeg Jun 10 '14 at 19:46
  • As I said you are not understanding that the ListView creates each control in your template for each databound item. That means whatever the ID is of each item in your ItemTemplate has a different ID at runtime, which is why you can't reference them at compile time. I noticed you gave one of your buttons a command name, and that is where you need to put your codebehind. I haven't tested this in runtime, I'm sorry I don't have time to create a test solution for you, however my updated answer should help. – Adam Heeg Jun 12 '14 at 18:04