DNN LinkButton OnClick event not firing (using asp.net, C#) - c#

I have a page that I would like to display a calendar of events that is stored in a MySQL database. I am pulling the events using asp:SqlDataSource and asp:Repeater. I am able to pull the events but I can't get the events to be sorted into the specific day. I"m using a asp:LinkButton with an OnClick event handler, but it doesn't seem to be firing when the LinkButton is clicked. I have included my code below. Any help on what i'm doing wrong and why my OnClick event isn't firing will be greatly appreciated./
ascx - page
<div class="Calendar_Container">
<ul class="Calendar_Tabs">
<li><asp:LinkButton runat="server" Text="All<br /> Week" OnClick="ShowEvents" CommandArgument="9"></asp:LinkButton></li>
<li class="Calendar_Active"><asp:LinkButton runat="server" Text="Sun<br />Nov 1" OnClick="ShowEvents" CommandArgument="1"></asp:LinkButton></li>
<li><asp:LinkButton runat="server" Text="Fri<br />Nov 6" OnClick="ShowEvents" CommandArgument="6"></asp:LinkButton></li>
</ul>
</div>
<div class="Calendar_Content">
<div id="Calendar_AllWeek" class="Calendar_Tab">
<asp:SqlDataSource runat="server" ID="sqlGetEventsAllWeek" />
<asp:Repeater runat="server" ID="rptGetEventsAllWeek" DataSourceID="sqlGetEventsAllWeek">
<ItemTemplate>
<div class="Calendar_Item">
<div class="Calendar_ItemInfo">
<div class="Calendar_ItemTitle">
<h4><a href='<%# Eval("link") %>'><%# Eval("title") %></a></h4><br />
<strong><%# Eval("event_start_time") %> <%# Eval("event_end_time") %><br /><%# Eval("event_location") %></strong><br /></div>
<div class="Calendar_ItemDescription">
<p><%#System.Web.HttpUtility.HtmlDecode(Eval("description").ToString()) %><br />
<%# Eval("event_day") %>
</p>
</div>
</div>
<div class="clear_float"> </div>
</div>
</ItemTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
</asp:Repeater>
</div>
<div id="Calendar_EventsSun" class="Calendar_Tab">
<asp:SqlDataSource runat="server" ID="sqlGetEventSun" />
<asp:Repeater runat="server" ID="rptGetEventSun" DataSourceID="sqlGetEventSun">
<ItemTemplate>
<div class="Calendar_Item">
<div class="Calendar_ItemInfo">
<div class="Calendar_ItemTitle">
<h4><a href='<%# Eval("link") %>'><%# Eval("title") %></a></h4><br />
<strong><%# Eval("event_start_time") %> <%# Eval("event_end_time") %><br /><%# Eval("event_location") %></strong><br /></div>
<div class="Calendar_ItemDescription">
<p><%# System.Web.HttpUtility.HtmlDecode(Eval("description").ToString()) %>
<br /><strong><%# Eval("event_day") %></strong>
</p>
</div>
</div>
<div class="clear_float"> </div>
</div>
</ItemTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
</asp:Repeater>
</div>
<div id="Calendar_EventsFri" class="Calendar_Tab">
<asp:SqlDataSource runat="server" ID="sqlGetEventFri" />
<asp:Repeater runat="server" ID="rptGetEventFri" DataSourceID="sqlGetEventFri">
<ItemTemplate>
<div class="Calendar_Item">
<div class="Calendar_ItemInfo">
<div class="Calendar_ItemTitle">
<h4><a href='<%# Eval("link") %>'><%# Eval("title") %></a></h4>
<strong><%# Eval("event_start_time") %> <%# Eval("event_end_time") %><br /><%# Eval("event_location") %></strong><br />
</div>
<div class="Calendar_ItemDescription">
<p><%# System.Web.HttpUtility.HtmlDecode(Eval("description").ToString()) %>
<br /><strong><%# Eval("event_day") %></strong>
</p>
</div>
</div>
<div class="clear_float"> </div>
</div>
</ItemTemplate>
<SeparatorTemplate><hr /></SeparatorTemplate>
</asp:Repeater>
</div>
</div>
ascx.cs - code behind
namespace Christoc.Modules.Calendar {
public partial class View : CalendarModuleBase, IActionable
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
GetAllWeek("9");
GetHomecomingWeekSun("1");
GetHomecomingWeekFri("6");
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
protected void ShowEvents(object sender, EventArgs e)
{
string dayId = ((LinkButton)sender).CommandArgument;
GetAllWeek(dayId);
GetHomecomingWeekSun(dayId);
GetHomecomingWeekFri(dayId);
}
private void GetAllWeek(string dayId)
{
sqlGetEventsAllWeek.ConnectionString = stampConnStr;
sqlGetEventsAllWeek.ProviderName = stampProvName;
sqlGetEventsAllWeek.SelectCommand = string.Format(#"
SELECT *
FROM homecoming_program
WHERE event_day = ?
ORDER BY event_day DESC;");
sqlGetEventsAllWeek.SelectParameters.Clear();
sqlGetEventsAllWeek.SelectParameters.Add(#"day_id", dayId);
rptGetEventsAllWeek.DataBind();
}
private void GetHomecomingWeekSun(string dayId)
{
sqlGetEventSun.ConnectionString = stampConnStr;
sqlGetEventSun.ProviderName = stampProvName;
sqlGetEventSun.SelectCommand = string.Format(#"
SELECT *
FROM homecoming_program
WHERE event_day = ?
ORDER BY event_day DESC;");
sqlGetEventSun.SelectParameters.Clear();
sqlGetEventSun.SelectParameters.Add(#"day_id", dayId);
rptGetEventSun.DataBind();
}
private void GetHomecomingWeekFri(string dayId)
{
sqlGetEventFri.ConnectionString = stampConnStr;
sqlGetEventFri.ProviderName = stampProvName;
sqlGetEventFri.SelectCommand = string.Format(#"
SELECT *
FROM homecoming_program
WHERE event_day = ?
ORDER BY event_day DESC;");
sqlGetEventFri.SelectParameters.Clear();
sqlGetEventFri.SelectParameters.Add(#"day_id", dayId);
rptGetEventFri.DataBind();
}
}
}

Related

How can I get the lblmessage to display only if Confirm button is clicked before Calculate button?

<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Confirm.aspx.cs" Inherits="XEx04Quotation.Confirm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Confirm quotation</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/site.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server" class="form-horizontal">
<main class="container">
<h1 class="jumbotron">Quotation confirmation</h1>
<div class="form-group">
<label class="col-sm-3 control-label">Sales price</label>
<asp:Label ID="lblSalesPrice" runat="server" CssClass="col-sm-3 bold"></asp:Label>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Discount amount</label>
<asp:Label ID="lblDiscountAmount" runat="server" CssClass="col-sm-3 bold"></asp:Label>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Total price</label>
<asp:Label ID="lblTotalPrice" runat="server" CssClass="col-sm-3 bold"></asp:Label>
</div>
<div class="row">
<h3 class="col-sm-offset-2 col-sm-10">Send confirmation to</h3>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Name</label>
<div class="col-sm-3">
<asp:TextBox ID="txtName" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="col-sm-6">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
Display="Dynamic" ErrorMessage="Required" CssClass="text-danger"></asp:RequiredFieldValidator>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email address</label>
<div class="col-sm-3">
<asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="col-sm-6">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail"
Display="Dynamic" ErrorMessage="Required" CssClass="text-danger"></asp:RequiredFieldValidator>
</div>
</div>
<%-- Quotation and Return buttons --%>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<%-- buttons go here --%>
<asp:Button ID="Button1" runat="server" Text="Send Quotation" CssClass="btn btn-primary"/>
<asp:Button ID="Button2" runat="server" Text="Return" CssClass="btn btn-primary" PostBackUrl="~/Default.aspx" CausesValidation="false"/>
</div>
</div>
<%-- message label --%>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-11">
<%-- message label goes here --%>
<asp:Label ID="lblmessage" runat="server" Text="Click the Send Quotation button to send the quotation via email" CssClass="text-info"></asp:Label>
</div>
</div>
</main>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace XEx04Quotation
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
}
protected void btnCalculate_Click(object sender, EventArgs e)
{
if (IsValid)
{
decimal salesPrice = Convert.ToDecimal(txtSalesPrice.Text);
// save the salesPrice into a session state variable
Session["SalesPrice"] = salesPrice;
decimal discountPercent = Convert.ToDecimal(txtDiscountPercent.Text) / 100;
decimal discountAmount = salesPrice * discountPercent;
// save the discountAmount into a session state variable
Session["Discount"] = discountAmount;
decimal totalPrice = salesPrice - discountAmount;
// save the totalPrice into a session state variable
Session["TotalPrice"] = totalPrice;
lblDiscountAmount.Text = discountAmount.ToString("c");
lblTotalPrice.Text = totalPrice.ToString("c");
}
}
protected void Button1_Confirm(object sender, EventArgs e)
{
if(Session["SalesPrice"] != null)
{
Response.Redirect("Confirm.aspx");
}
else
{
// This is the part I am concerned about
lblmessage2.Text = "Click the Calculate button before you click confirm";
}
}
}
}
Hello Everyone,
I am building multi-web application pages using the Default.aspx, Default.aspx.cs and Confirm.aspx and Confirm.aspx.cs. The problem that I am having is that when I click "Confirm" before clicking "Calculate," the event handler displays the "Click the Calculate button before the confirm" at the bottom of the page. It does that even if the Calculate button is clicked before the Confirm and also displays again when I reload the page. How can I get this to only display if the session state value of SalesPrice is null? Otherwise, it will redirect to the confirm page. Here is Default.aspx.cs and other file:
You can do one thing. If you want to get the total calculation first, then disable the confirm button and clicking on the calculate button will enable the button as follows:
<asp:Button ID="Button1" runat="server" Enabled="false" />
protected void btnCalculate_Click(object sender, EventArgs e)
{
Button1.Enabled = True;
if(Session["TotalPrice"] != null)
{
lblMsg.Text = "Please click here to see total calculation";
}
}
protected void Button1_Confirm(object sender, EventArgs e)
{
if(Session["SalesPrice"] != null)
{
Response.Redirect("Confirm.aspx");
}
}
Note: Please follow the naming convention for the event names. Button1 To btnConfirm.
Update 1 - Try the below tested code:
<asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" />
<asp:Button ID="btnConfirm" runat="server" Text="Confirm" OnClick="btnConfirm_Click" />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
private int buttonWasClicked = 0; //Kept a variable type of int and with a default value 0
protected void btnCalculate_Click(object sender, EventArgs e)
{
buttonWasClicked = 1; //If clicked, then 1
Session["value"] = Convert.ToInt32(buttonWasClicked); //Then kept in session
lblMsg.Text = "Total Price - 10,000";
}
protected void btnConfirm_Click(object sender, EventArgs e)
{
if (Session["value"] != null) //If session not null, then redirect page
{
Response.Redirect("CustomerDetails.aspx");
Session["value"] = null; //Clears the session
}
else
{
lblMsg.Text = "Click the calculate button first";
}
}

Repeater control with IF Else statement always reture true or false in aspx page

I need to show HTML based on the Container.ItemIndex % 2 ==0 condition
But for me it always return either true or false based on the coding in the code
<asp:Repeater ID="rptNews" runat="server" EnableViewState="False" OnItemDataBound="rptNews_ItemDataBound">
<ItemTemplate>
<div style="display:none;"><%# _repeaterIndex = Container.ItemIndex %></div>
<% if (_repeaterIndex % 2 == 0)
{%>
<div class="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
<div class="blog-item style-3">
<div class="blog-image">
<img alt="image" class="img-responsive center-block" src='<%# Eval("Image") %>' />
</div>
<div class="blog-content">
<div class="text-center">
<h5 class="date"><%# getDate(Eval("Date")) %></h5>
<h3 class="title-news blog-title newslisttitle"><%# Eval("Heading") %></h3>
<a href='<%# getURL(Eval("ID")) %>'><strong> MORE </span></strong></a>
</div>
</div>
</div>
</div>
<% }
else
{ %>
<div class="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
<div class="blog-item style-3">
<div class="blog-content content-top">
<div class="text-center">
<h5 class="date"><%# Eval("Date") %></h5>
<h3 class="title-news blog-title newslisttitle"><%# _repeaterIndex %> <%# Eval("Heading") %></h3>
<a href='<%# getURL(Eval("ID"),Eval("Heading")) %>'><strong> MORE </strong></a>
</div>
</div>
<div class="blog-image">
<img alt="image" class="img-responsive center-block" src='<%# Eval("Image") %>' />
</div>
</div>
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
CODE BEHIND
protected void rptNewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
_repeaterIndex = _repeaterIndex + 1;
if (e.Item.ItemIndex % 2 == 0)
{
_showTop = true;
}
else
{
_showTop = false;
}
}
}
By now i have tried several thing but it is not working properly if (_repeaterIndex % 2 == 0) this is always true or false based on teh total row count as in my case it always show the 13 as i have 13 records if i display for testing _repeaterIndex it show the the correct value but in if it always take the final value of Container.ItemIndex not the current value
<h3 class="title-news blog-title newslisttitle"><%# _repeaterIndex%> <%# Eval("Heading") %></h3>
What is the best way to solve this issue i am facing
Much easier would be to use Container.ItemIndex directly instead of using the ItemDataBound Event. You can do this by using two PlaceHolders and set their Visibility based on the value of ItemIndex.
<ItemTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Container.ItemIndex % 2 == 0 %>'>
Even content<br />
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# Container.ItemIndex % 2 == 1 %>'>
Odd content<br />
</asp:PlaceHolder>
</ItemTemplate>
After spending hours trying to find other-ways of solving this issue i finally used two Panel and i hide & show based on the e.Item.ItemIndex % 2 == 0 value in teh code behind. I thing this is the most effective way of doing it.
<asp:Repeater ID="rptNewsList" runat="server" EnableViewState="False" OnItemDataBound="rptNewsList_ItemDataBound">
<ItemTemplate>
***HTML WILL GO HERE
<asp:Panel runat="server" ID="PanelTopImage" CssClass="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
</asp:Panel>
<asp:Panel runat="server" ID="PanelBottomImage" CssClass="col-md-4 col-lg-3 col-sm-6 no-padding newslist-col">
***HTML WILL GO HERE
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
CODE BEHIND
protected void rptNewsList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel pnlTopImage = e.Item.FindControl("PanelTopImage") as Panel;
Panel pnlBottomImage = e.Item.FindControl("PanelBottomImage") as Panel;
if (e.Item.ItemIndex % 2 == 0)
{
pnlTopImage.Visible = true;
pnlBottomImage.Visible = false;
}
else
{
pnlTopImage.Visible = false;
pnlBottomImage.Visible = true;
}
}
}

Page Loading Multiple times

Having a unusual problem with an application I'm building, so what I'm looking to do is a user can view preview of their adverts they have created, click the advert which is a dynamic link and be brought to a new page which will show the advert in more detail. The more detail page is then runs a to check the query string, pass the parameters of the query sting to a method which pulls the correct details form the database. This part works perfectly, but when the object is returned the pages loads again, calls the same method 3 times, but with null parameters which in returns a null reference to an object that doesn't exist.
I've tried to refactor the code so when a page loads it won't retrieve form the database unless the search parameters are valid. When the page loads, components won't work then, for example the Image Carousel.
I've tried hard coding the parameters into the method, will work perfectly. will only load once.
I've created two new pages which do not inherit form a master page and used the exact same methods. works perfectly. I tried the method on a two different pages that are inherited form the master page and same problem.
Detailed Page.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeFile="DetailAdvert.aspx.cs" Inherits="Lenders.ie.DetailAdvert" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="col-lg-9">
<div class="container">
<div class="row">
<!--Advert Image -->
<div class="container">
<div class="row">
<!-- Carosel goes here -->
<div class="row carousel-holder">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-10">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img class="slide-image" id="Img1" runat="server" src="http://placehold.it/800x380" alt=""/>
</div>
<div class="item">
<img class="slide-image" id="Img2" runat="server" src="http://placehold.it/800x380" alt=""/>
</div>
<div class="item">
<img class="slide-image" id="Img3" runat="server" src="http://placehold.it/800x380" alt=""/>
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
</div>
</div>
</div>
<!--User Details-->
<br />
<div class="container">
<div class="row">
<!-- Descriptin and profile view -->
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-5">
<div class="well well-sm">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
<img src="" runat="server" id="imgProfilePic" alt="" class="img-rounded img-responsive" />
</div>
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
<h4>
<asp:Label ID="lblFristName" runat="server" Text=""></asp:Label>
<asp:Label ID="lblSecondName" runat="server" Text=""></asp:Label>
</h4>
<small><cite title="Location"><asp:Label ID="lblLocation" runat="server" Text=""></asp:Label> <i class="glyphicon glyphicon-map-marker">
</i></cite></small>
<p>
<i title="Email" class="glyphicon glyphicon-envelope"></i> <asp:Label ID="lblEmail" runat="server" Text=""></asp:Label>
<br />
<i title="Join Date" class="glyphicon glyphicon-user"></i> <asp:Label ID="lblJoinDate" runat="server" Text=""></asp:Label><br />
<i title="User Rating" class="glyphicon glyphicon-stats"> </i><asp:Label ID="lblUserRating" runat="server" Text=""></asp:Label><br />
<i title="Contact Number" class="glyphicon glyphicon-phone"> </i><asp:Label ID="lblContactNumber" runat="server" Text=""></asp:Label>
</p>
<!-- Split button -->
<asp:Button ID="btnMessage" runat="server" Text="Message" CssClass="btn btn-success" />
</div>
</div>
</div>
</div>
<!-- Description / Product Details-->
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-5">
<div class="well well-sm">
<h4> Description: </h4>
<p>
<asp:Label ID="lblDesc" runat="server" Text=""></asp:Label>
</p>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<!-- Comment Section -->
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-10">
<div class="well well-sm">
<h4>Comments</h4>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
</asp:Content>
Detailed Aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DataModels;
namespace Lenders.ie
{
public partial class DetailAdvert : System.Web.UI.Page
{
public Advert_User ReturnedUserAdvet = new Advert_User();
public BalAdvertManger BALMan = new BalAdvertManger();
protected void Page_Load(object sender, EventArgs e)
{
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.ExpiresAbsolute = DateTime.Now.AddMonths(-1);
//GetData();
//PopulatePage();
//int UserID = Convert.ToInt32(Request.QueryString["UiD"]);
//int AdvertID = Convert.ToInt32(Request.QueryString["AdID"]);
if (!Page.IsPostBack)
{
GetData();
}
}
public void GetData()
{
//int UserID = 41;
//int AdvertID = 2;
int UserID = Convert.ToInt16(Request.QueryString["UiD"]);
int AdvertID = Convert.ToInt16(Request.QueryString["AdID"]);
if (UserID != 0 || AdvertID != 0)
{
ReturnedUserAdvet = BALMan.ReturnDetailedAdvert(UserID, AdvertID);
PopulatePage();
}
//int UserID = Convert.ToInt32(Request.QueryString["UiD"]);
//int AdvertID = Convert.ToInt32(Request.QueryString["AdID"]);
//int UserID = 41;
//int AdvertID = 2;
//ReturnedUserAdvet = BALMan.ReturnDetailedAdvert(UserID,AdvertID);
}
public void PopulatePage()
{
}
}
}
This is where the link is and directs to the Detailed page.
Profile Page.aspx
<%# Page Title="" Language="C#" EnableEventValidation="false" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="ProfilePage.aspx.cs" Inherits="Lenders.ie.ProfilePage1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="container">
<div class="row">
<%-- Adverts View--%>
<asp:View ID="AdvertsView" runat="server">
<h2>Adverts</h2>
</asp:View>
<%-- View Adverts --%>
<asp:View ID="View_ViewAdverts" runat="server">
<h2>View Adverts</h2>
<div class="row">
<asp:Literal ID="Advert1" runat="server"></asp:Literal>
<asp:LinkButton ID="LinkButton1" OnClick="LinkButton1_Click" runat="server">LinkButton</asp:LinkButton>
</div>
</asp:View>
<%--There was three closing divs here--%>
<asp:View ID="MessageView" runat="server">
<h2>Messages</h2>
</asp:View>
</asp:MultiView>
</div>
</div>
</div>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
ProfilePage aspx.cs - Method that creates the link
public void DisplayUserAdverts()
{
int UserID = Convert.ToInt32(Session["UserID"]);
int i = 0;
ListOfUserAdverts = BalAdvertManager.ReturnUserAdvert(UserID);
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
foreach (Advert item in ListOfUserAdverts)
{
string ColDiv = "col-sm-4 col-lg-4 col-md-4";
string ThumbClass = "thumbnail";
string Image = "AdvertThumbnails/" + item.Img1.ToString();
string Caption = "caption";
string Link = "DetailAdvert.aspx/?AdID=" + item.AdvertID.ToString() + "&UiD=" + item.UserID.ToString();
Button btn = new Button();
writer.AddAttribute(HtmlTextWriterAttribute.Class, ColDiv);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.AddAttribute(HtmlTextWriterAttribute.Class, ThumbClass);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
//writer.AddAttribute(HtmlTextWriterAttribute.Src, Image);
writer.AddAttribute(HtmlTextWriterAttribute.Href, Link);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.AddAttribute(HtmlTextWriterAttribute.Src, Image);
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Class, Caption);
writer.RenderBeginTag(HtmlTextWriterTag.Div);
//writer.AddAttribute(HtmlTextWriterAttribute.Src, Image);
//writer.RenderBeginTag(HtmlTextWriterTag.Div);
//writer.AddAttribute(HtmlTextWriterAttribute.Class, Caption);
writer.RenderBeginTag("h4");
writer.Write(item.Title.ToString());
writer.RenderEndTag();
writer.RenderBeginTag("p");
//Does Something
writer.Write(item.Desc.ToString().Substring(0, 50));
writer.Write("</br>");
writer.Write("Testing" + i.ToString());
writer.Write("</br>");
this.Controls.Add(btn);
writer.AddAttribute(HtmlTextWriterAttribute.Href, Link);
// writer.AddAttribute(HtmlTextWriterAttribute.Id);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write("Link");
writer.RenderEndTag();
writer.RenderEndTag();
//writer.WriteBeginTag("p");
// // Does Something
//writer.WriteEndTag("p");
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
i++;
}
}
Advert1.Text = stringWriter.ToString();
}
Master Page.aspx
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="Lenders.ie.MasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Shop Homepage - Start Bootstrap Template</title>
<!-- Bootstrap Core Content -->
<link href="Content/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="Content/shop-homepage.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<form id="form1" runat="server">
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%-- <a class="navbar-brand" href="www.google.com">
</a>--%>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<asp:HyperLink ID="Home" runat="server">Home</asp:HyperLink>
</li>
<li>
<asp:HyperLink ID="Services" runat="server">Services</asp:HyperLink>
</li>
<li>
<asp:HyperLink ID="Contact" runat="server">Contact</asp:HyperLink>
</li>
<li>
<asp:HyperLink ID="Login" runat="server">Log In</asp:HyperLink>
</li>
</ul>
<div class="navbar-form navbar-right">
<div class="input-group input-sm">
<asp:TextBox ID="txtSearch" runat="server" CssClass="form-control form-control-sm " AutoPostBack="true"></asp:TextBox>
<span class="
</span>
</div>
</div>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<div class="container">
<div class="row">
<div class="col-md-3">
<p class="lead">Shop Name</p>
<div class="list-group">
<asp:HyperLink ID="HyperLink1" CssClass="list-group-item" runat="server">Sports</asp:HyperLink>
<asp:HyperLink ID="HyperLink2" CssClass="list-group-item" runat="server">Hobbies</asp:HyperLink>
<asp:HyperLink ID="HyperLink3" CssClass="list-group-item" runat="server">Life Style</asp:HyperLink>
<asp:HyperLink ID="HyperLink4" CssClass="list-group-item" runat="server">Another Example</asp:HyperLink>
<asp:HyperLink ID="HyperLink5" CssClass="list-group-item" runat="server"> Blah </asp:HyperLink>
</div>
</div>
<%-- <div class="col-md-9">
<div class="row carousel-holder">
<div class="col-md-12">--%>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
<%-- </div>
</div>
</div>--%>
<div class="container">
<hr>
<!-- Footer -->
<footer>
<div class="row">
<div class="col-lg-12">
<p>Copyright © Your Website 2014</p>
</div>
</div>
</footer>
</div>
<!-- /.container -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>enter code here
</form>
</body>
</html>
Master Page.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lenders.ie
{
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}

Selected Index Changed event not firing both Autopostback property

In my Dropdownlist Selected index change event not firing.Here i use auto post back true &
View state also true.But Selected Index Changed Event not firing
My Code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="AdminEagleViewLogin.aspx.cs" Inherits="AdminEagleViewLogin" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style>
body{padding-top:20px;}
</style>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<div class="row">
User : <asp:DropDownList ID="drpusr" runat="server" Visible="true" OnSelectedIndexChanged="drpusr_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true" ></asp:DropDownList>
Password: <asp:Label ID="lbluserpw" runat="server"></asp:Label>
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Please sign in</h3>
</div>
<div class="panel-body">
<form accept-charset="UTF-8" role="form">
<fieldset>
<div class="form-group">
<asp:TextBox ID="txtusr" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox ID="txtpw" runat="server" TextMode="Password"></asp:TextBox>
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me"> Remember Me
</label>
</div>
<asp:CheckBox ID="chkremember" runat="server" Visible="false" class="remchkbox" />
<asp:Button ID="submit" runat="server" class="btn btn-lg btn-success btn-block" Text="Submit" OnClick="submit_Click" />
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
ServerSide
User bind to Dropdown is working.
public partial class AdminEagleViewLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindUsers();
//lbluserpw.Text = Membership.Provider.GetPassword(drpusr.SelectedValue, String.Empty);
}
protected void submit_Click(object sender, EventArgs e)
{
if (Membership.ValidateUser(txtusr.Text, txtpw.Text))
{
FormsAuthentication.SetAuthCookie(txtusr.Text, chkremember.Checked);
string[] CurrentUserRole = Roles.GetRolesForUser(txtusr.Text);
var admin = "Administrator";
var manager = "Manager";
var user = "User";
if (CurrentUserRole.Contains(admin))
{
Response.Redirect("Administrator.aspx");
}
else if (CurrentUserRole.Contains(manager))
{
Response.Redirect("Manager.aspx");
}
else
{
Response.Redirect("UserPage.aspx");
}
}
else
{
Response.Redirect("AdminEagleViewLogin.aspx");
}
}
protected void BindUsers()
{
DataAccess da = new DataAccess();
drpusr.DataSource = da.GetUsers();
drpusr.DataTextField = "UserName";
drpusr.DataValueField = "UserId";
drpusr.DataBind();
drpusr.Items.Insert(0, new ListItem("-- Select User --", "0"));
drpusr.Items.RemoveAt(1);
}
protected void drpusr_SelectedIndexChanged(object sender, EventArgs e)
{
lbluserpw.Text = Membership.Provider.GetPassword(drpusr.SelectedValue, String.Empty);
}
}
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindUsers();
}
}
You shouldn't put a form element inside another form:
<form accept-charset="UTF-8" role="form">
<fieldset>
...
<asp:Button ID="submit" runat="server" class="btn btn-lg btn-success btn-block" Text="Submit" OnClick="submit_Click" />
</fieldset>
</form>
remove the inner form.
[Update]
Another problem is the "ID" property of the button.
change it to something else, other than "submit".

ASP button even not firing

I have set up a page with a submit button that the user can click and log in. I need to gather the field values and then do some backend stuff, but when the button is clicked, the even handler method I have set up doesn't fire. Heres the button code:
<asp:Button UseSubmitBehavior="false" CssClass="btn btn-primary" ID="btnTest" runat="server" Text="Submit" OnClick="btnTest_Click" />
And the codebehind:
public partial class SignIn : System.Web.UI.Page
{
protected void Page_Load(Object sender, EventArgs e)
{
btnTest.Click += new EventHandler(this.btnTest_Click);
}
protected void btnTest_Click(Object sender, EventArgs e)
{
string user = username.Value;
string pass = password.Value;
int userid = -1;
RoomSvc.Data.Person[] persons = RoomService.Service.ServiceClient.GetPersons();
RoomSvc.Data.Person pUser = null;
foreach (RoomSvc.Data.Person p in persons)
{
if (p.username == user && p.password == pass)
{
pUser = RoomService.Service.ServiceClient.GetPerson(userid.ToString());
}
else
{
return;
}
}
RoomService.Global.currentUser = pUser;
}
}
I have tried everything out there and nothing is making it work, could someone please enlighten.
EDIT: .aspx code:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="SignIn.aspx.cs" Inherits="RoomService.Account.SignIn" %>
<asp:Content ID="SignInHeader" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="SignInContent" ContentPlaceHolderID="MainContent" runat="server">
<form action="SignIn.aspx" class="form-horizontal well span5 offset5">
<fieldset>
<legend>Sign In</legend>
<div class="control-group">
<label class="control-label" for="input01">Username</label>
<div class="controls">
<input runat="server" type="text" class="input-medium" id="username" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="input01">Password</label>
<div class="controls">
<input runat="server" type="text" class="input-medium" id="password" />
</div>
</div>
<div class="form-actions">
<asp:Button CssClass="btn btn-primary" ID="btnTest" runat="server" Text="Submit" OnClick="btnTest_Click" />
</div>
</fieldset>
</form>
</asp:Content>
Your form tag is missing runat="server
I figured it out.
I had to add the even handler in Page_Init() not Page_Load()

Categories

Resources