Mike Simo

Mike Simo

  • NA
  • 17
  • 12.6k

Get values of Label - LinkButton - session

Jun 11 2014 9:48 PM
Hi
I have added a like button on my product listview, and the like button suppose to get the value of the ads title and id and the userid whose clicked on this btn and stored 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 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
Many thanks for all of you

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="content">
<br /><br />

<div id="leftsidemainadsshow">


<asp:ListView ID="adsshow" runat="server"
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" />
 -
<asp:Label ID="StateLabel" runat="server" Text='<%# Eval("State") %>'
style="font-family: Arial, Helvetica, sans-serif; font-size: small" />
 -
<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" />
   
<asp:ImageButton ID="Sndmailtoadder" runat="server"
ImageUrl="~/iconsimg/mailposter.png" OnClick="Sndmailtoadder_Click" />
   

</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>

</div>

<div id="primumads">



</div>

<br />


<br />

</div>
</asp:Content>


public partial class Berava : System.Web.UI.Page
{

string cs = ConfigurationManager.ConnectionStrings["BeravaConnectionString"].ConnectionString.ToString();

protected void Page_Load(object sender, EventArgs e)
{
string user = (string)(Session["UsrNme"]);


if (!IsPostBack)
{
// Check if your Session key exists (as it is required for your query)
if (Session["location"] != null)
{

using (SqlConnection shwadsone = new SqlConnection(cs))
{
// Open your connection
shwadsone.Open();

// Build your data adapter
SqlDataAdapter dashowadsone = new SqlDataAdapter("SELECT [AdsID], [Country], [State], [City], [AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [AdsPrice], [Img1] FROM [ads] WHERE ([Country] = @Country)", cs);

// Grab your location (guaranteed to exist from the above if-statement
string location = Convert.ToString(Session["location"]);

// Add your parameters to your data adapter
dashowadsone.SelectCommand.Parameters.AddWithValue("@Country", location);

// Define your data set
DataSet dsadsshowone = new DataSet();

// Fill your data set
dashowadsone.Fill(dsadsshowone);

// Bind your results
adsshow.DataSource = dsadsshowone.Tables[0];
adsshow.DataBind();
}
}
else
{
// Your Session key could not be found, consider displaying an error
}
}
}



protected void LinkButton1_Click(object sender, EventArgs e)
{

}

protected void Adstitlinkbtn_Click(object sender, EventArgs e)
{

}

protected void likebtn_Click(object sender, ImageClickEventArgs e)
{

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

if (Session["UsrNme"] == null)
{
string sqlstatment = "INSERT INTO favourite (AdsID, UID, AdsTit) VALUES (@AdsID,@UID,@AdsTit) WHERE ([UID] = @UsrNme)";

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

//Insert the parameters first
ImageButton likebtn = (ImageButton)sender; //the clicked button
ListViewItem adsshow = (ListViewItem)likebtn.NamingContainer; //listviewitem where the button clicked
Label Labeladsid = (Label)adsshow.FindControl("Labeladsid"); //get another control in same listviewitem

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


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

}

else
{
Response.Write("please login to add this ads to your favorite list");
Response.Redirect("login.aspx");
}

}



protected void Sndmailtoadder_Click(object sender, ImageClickEventArgs e)
{

}


}
}