I have a Repeater control, that I have now reduced to just changing text in a text box when clicking the associated button.
However, this is not happening.
Here is my code so far:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Repeater ID="rptPdfList" runat="server" OnItemCommand="rptPdfList_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<td>File Name</td>
<td></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text=<%#Eval("FileName") %>></asp:Label>
</td>
<td>
<asp:Button ID="btnLoad" runat="server" Text="Load" CommandName="LoadDoc"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnLoad" runat="server" Text="Load" OnClick="btnLoad_Click" /><br />
<iframe runat="server" id="pdfHolder"></iframe>
<br />
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetFiles();
}
private void GetFiles()
{
rptPdfList.DataSource = Pdf();
rptPdfList.DataBind();
}
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
Label lblName = (Label)e.Item.FindControl("lblName");
switch (e.CommandName)
{
case "LoadDoc":
//xpdfHolder.Attributes.Add("src", "PDF/" + lblName.Text);
lblTest.Text = "test";
lblName.Text = "oops";
break;
}
}
public static List<PdfList> Pdf()
{
string pdfDir = HostingEnvironment.MapPath("~") + #"PDF\";
DirectoryInfo directory = new DirectoryInfo(pdfDir);
FileInfo[] pdfFiles = directory.GetFiles("*.pdf", SearchOption.AllDirectories);
List<PdfList> pdfLists = pdfFiles.Select(pdfFile => new PdfList
{
FileName = pdfFile.Name
}).ToList();
return pdfLists;
}
}
public class PdfList
{
public string FileName { get; set; }
}
Ca anyone see where I went wrong?
Edit, added all the code
Change this:
protected void Page_Load(object sender, EventArgs e)
{
GetFiles();
}
to this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetFiles();
}
You are calling the GetFiles() each time so it is always returning to the initial state.
I am binding your repeater like this and it works fine for me, Just place your binding function in
if (!Page.IsPostBack)
condition :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (DataClassesDataContext dc = new DataClassesDataContext())
{
var v = (from s in dc.t_employees select s).ToList();
rptPdfList.DataSource = v;
rptPdfList.DataBind();
}
}
}
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
Label lblName = (Label)e.Item.FindControl("lblName");
switch (e.CommandName)
{
case "LoadDoc":
//xpdfHolder.Attributes.Add("src", "PDF/" + lblName.Text);
lblTest.Text = "test";
lblName.Text = "oops";
break;
}
}
Related
I am creating a quiz application in ASP.net (c#) and I currently have one question at a time displaying with the use of a datapager. However, when i move from question 1 to question 2 and then back to question 1 the radiobutton selection disappears.
EDIT - I now have the radio button retaining a selection whenever I move from next to previous, HOWEVER, If I chose radiobutton1 and then move on to question 2, It has radiobutton1 already selected, even though I am yet to answer that question. So for some reason whatever selection I make on question 1 is being duplicated to the other questions.
Question1 example
Question2 example
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging" OnPreRender="ListPager_PreRender">
<LayoutTemplate>
<div id="itemPlaceHolder1" runat="server">
</div>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="1">
<Fields>
<asp:NextPreviousPagerField runat="server" ButtonType="Link"
ShowFirstPageButton="false"
ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link"
ShowNextPageButton="true"
ShowLastPageButton="false"
ShowPreviousPageButton="false" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("QuestionID")%>'></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text='<%#Eval("QuestionText")%>'></asp:Label><br />
<li>
<asp:RadioButton ID="Radio1" Text='<%#Eval("Answer1") %>' GroupName="radiobtns" EnableViewState="true" runat="server" />
</li>
<li>
<asp:RadioButton ID="Radio2" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer2") %>' />
</li>
<li>
<asp:RadioButton ID="Radio3" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer3") %>' />
</li>
<li>
<asp:RadioButton ID="Radio4" runat="server" GroupName="radiobtns" EnableViewState="true" Text='<%#Eval("Answer4") %>' />
</li>
<br />
</ItemTemplate>
</asp:ListView>
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</p>
</form>
</body>
</html>
namespace WebApplication2.WebForms
{
public partial class _1QuestionQuiz : System.Web.UI.Page
{
string userAns;
int QuestionID;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindListView();
if (Session["QuizID"] != null)
{
int QuizID = Convert.ToInt32(Session["QuizID"]);
}
}
}
protected void ListPager_PreRender(object sender, EventArgs e)
{
if (Session["DTSource"] != null)
{
lvCustomers.DataSource = Session["DTSource"];
lvCustomers.DataBind();
}
GetSelections();
}
private void BindListView()
{
SqlConnection conn = new SqlConnection();
string connString = ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString;
SqlCommand Cmd = new SqlCommand();
conn.ConnectionString = connString;
conn.Open();
if (lvCustomers != null)
{
foreach (ListViewItem item in lvCustomers.Items)
{
Label lbl = (Label)item.FindControl("Label2");
if (lbl != null)
{
QuestionID = Convert.ToInt32(lbl.Text);
ViewState["qstion"] = QuestionID;
}
RadioButton rd1 = (RadioButton)item.FindControl("Radio1");
RadioButton rd2 = (RadioButton)item.FindControl("Radio2");
RadioButton rd3 = (RadioButton)item.FindControl("Radio3");
RadioButton rd4 = (RadioButton)item.FindControl("Radio4");
if (rd1.Checked)
{
userAns = rd1.Text;
ViewState["Radio1"] = rd1.Text;
}
else if (rd2.Checked)
{
userAns = rd2.Text;
ViewState["Radio2"] = rd2.Text;
}
else if (rd3.Checked)
{
userAns = rd3.Text;
ViewState["Radio3"] = rd3.Text;
}
else if (rd4.Checked)
{
userAns = rd4.Text;
ViewState["Radio4"] = rd4.Text;
}
SqlCommand comm = new SqlCommand("InsertSelections", conn);
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(comm);
SqlParameter p1 = new SqlParameter("Answer", userAns);
SqlParameter p2 = new SqlParameter("QuestionID", QuestionID);
SqlParameter p3 = new SqlParameter("QuizID", (Session["QuizID"]));
comm.Parameters.Add(p1);
comm.Parameters.Add(p2);
comm.Parameters.Add(p3);
comm.ExecuteNonQuery();
}
}
}
private void GetSelections()
{
foreach (ListViewItem item in lvCustomers.Items)
{
Label lbl = (Label)item.FindControl("Label2");
if (lbl != null)
{
QuestionID = Convert.ToInt32(lbl.Text);
}
RadioButton rd1 = (RadioButton)item.FindControl("Radio1");
RadioButton rd2 = (RadioButton)item.FindControl("Radio2");
RadioButton rd3 = (RadioButton)item.FindControl("Radio3");
RadioButton rd4 = (RadioButton)item.FindControl("Radio4");
//Try radiobutton 1 as a tester
if (rd1 != null)
{
if (lbl != null && ViewState["Radio1"] != null)
{
rd1.Checked = true;
}
}
if (rd2 != null)
{
if (lbl != null && ViewState["Radio2"] != null)
{
rd2.Checked = true;
}
}
if (rd3 != null)
{
if (lbl != null && ViewState["Radio3"] != null)
{
rd3.Checked = true;
}
}
if (rd4 != null)
{
if (lbl != null && ViewState["Radio4"] != null)
{
rd4.Checked = true;
break;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Score.aspx", false);
}
protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindListView();
}
}
}
Try overriding LoadViewState and SaveViewState to maintain the state of your radio buttons. For example, try something like the following, and pull the values back out of ViewState wherever you need them:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (ViewState["Radio1"] != null)
Radio1.Text = (int)ViewState["Radio1"];
...
}
protected override object SaveViewState()
{
ViewState["Radio1"] = Radio1.Text;
...
return base.SaveViewState();
}
I made a Webform that displays time and date, and I wanted to do the same for a Stopwatch, but for some reason the Stopwatch doesn't work.
The time is supposed to start ticking when I click "start", it's supposed to stop when I click "stop", and it is supposed to clear when I click "reset". Instead, when I click start, nothing happens.
Here is my code (C#):
{
public partial class WebForm1 : System.Web.UI.Page
{
private short _secs, _ms;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Timerz.Text = DateTime.Now.ToString();
}
protected void Timer2_Tick(object sender, EventArgs e)
{
IncreaseMS();
ShowTime();
}
private void IncreaseMS()
{
if (_ms == 99)
{
_ms = 0;
IncreaseSecs();
}
else
{
_ms++;
}
}
private void IncreaseSecs()
{
if (_secs == 59)
{
_secs = 0;
}
else
{
_secs++;
}
}
protected void start_Click(object sender, EventArgs e)
{
start.Enabled = false;
Timer2.Enabled = true;
}
protected void reset_Click(object sender, EventArgs e)
{
_secs = 0;
_ms = 0;
ShowTime();
}
private void ShowTime()
{
secsText.Text = _secs.ToString("00");
msText.Text = _ms.ToString("00");
}
protected void stop_Click(object sender, EventArgs e)
{
start.Enabled = true;
Timer2.Enabled = false;
}
}
}
Webform, ASP.NET Code:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Current time:
<asp:Label ID="Timerz" runat="server"></asp:Label>
</div>
<asp:Timer ID="Timer1" runat="server" Interval="1000"
OnTick="Timer1_Tick">
</asp:Timer>
<div>
<asp:Label ID="secsText" runat="server" Text="00"></asp:Label><asp:Label
ID="colon" runat="server" Text=":"></asp:Label><asp:Label ID="msText"
runat="server" Text="00"></asp:Label>
<br />
<asp:Button ID="start" runat="server" Text="Start" OnClick="start_Click"
/><asp:Button ID="stop" runat="server" Text="Stop" OnClick="stop_Click" />
<asp:Button ID="reset" runat="server" Text="Reset" OnClick="reset_Click" />
</div>
<asp:Timer ID="Timer2" runat="server" OnTick="Timer2_Tick">
</asp:Timer>
</form>
</body>
You haven't set the Interval property on Timer2. The default interval is 60 seconds and you are probably not waiting that long.
I have the following code-behind:
Random rndNum;
int inFirstOne, inSecondOne;
protected void Page_Load(object sender, EventArgs e)
{
rndNum = new Random();
int inFirst = rndNum.Next(1, 51);
int inSecond = rndNum.Next(50, 101);
lblFirst.Text = inFirst.ToString();
inFirstOne = inFirst;
lblSecond.Text = inSecond.ToString();
inSecondOne = inSecond;
}
public void ValidateForm(object sender, EventArgs e)
{
string strNum = tbValidation.Text;
int inCalc = inFirstOne + inSecondOne;
if (inCalc.ToString() == strNum)
{
lblIsValid.Text = "Correct";
}
else
{
lblIsValid.Text = "Please enter the correct result";
lblIsValid.ForeColor = Color.DarkRed;
}
}
ASP.net code:
...
<tr>
<td><h2>What is <asp:Label ID="lblFirst" Text="" CssClass="numGen" ClientIDMode="Static" runat="server" /> + <asp:Label ID="lblSecond" Text="" CssClass="numGen" ClientIDMode="Static" runat="server" />?</h2></td>
<td><asp:TextBox ID="tbValidation" ClientIDMode="Static" CssClass="tbTech" runat="server"></asp:TextBox> <asp:Label ID="lblIsValid" runat="server" /></td>
</tr>
<tr>
<td colspan="2" class="setRight">
<asp:Button ID="SubmitForm" ClientIDMode="Static" runat="server" Text="Submit" OnClick="ValidateForm" CssClass="btn" UseSubmitBehavior="false" />
</td>
</tr>
...
I am always seeing Please enter the correct result message.
How can I modify to ensure when the result is entered it works to validate the calculation from code-behind.
It's not clear how you're filling tbValidation but in any case unless you execute your page_load logic only if request it's not postback, variables inFirstOne and inSecondOnewill be overwritten on button click.
It seems you need to have this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostback)
{
// do this only if it's the first request
rndNum = new Random();
int inFirst = rndNum.Next(1, 51);
int inSecond = rndNum.Next(50, 101);
lblFirst.Text = inFirst.ToString();
inFirstOne = inFirst;
lblSecond.Text = inSecond.ToString();
inSecondOne = inSecond
}
}
int inCalc = Convert.ToInt32(lblFirst.Text) + Convert.ToInt32(lblSecond.Text); worked.
below is the markup and codebehind.
I am trying to access dropdownlist in the markup from codebehind.
<asp:Repeater runat="server" ID="cataloguesRepeater">
<FooterTemplate>
<table>
<tbody>
<tr>
<td>
<asp:DropDownList runat="server" ID="dropDownList1" />
</td>
</tr>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Codebehind
private void CatalogueItemCommand(object sender, RepeaterCommandEventArgs e)
{
DropDownList dd1 =(DropDownList)e.Item.FindControl("dropDownList1");
}
I always get dd1 as null for some reason. How can access this dropdownlist dd1?
You have to react on the ItemCreated Event:
protected void Page_Load(object sender, EventArgs e)
{
cataloguesRepeater.ItemCreated += cataloguesRepeater_ItemCreated;
cataloguesRepeater.DataSource = new [] { new { title = "item1"}, new { title = "item2" } };
cataloguesRepeater.DataBind();
}
void cataloguesRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
var ddl = e.Item.FindControl("dropDownList1");
}
}
I want to bind the CourseFee[] array to repeater.
I want to bind Amount and CourseFeeType.Descr in my repeater.
How do I bind it?
Sample Class
public class Order
{
public CourceFeeType FeeType;
public int Amount;
public int CourseFee;
public void AddFeeTypeDetails(CourceFeeType Fees)
{
FeeType = new CourceFeeType();
FeeType.Code = Fees.Code;
FeeType.Desc = Fees.Desc;
}
// Nested class
public class CourceFeeType
{
public String Code;
public String Desc;
}
}
Sample Form Load Code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Order> List = new List<Order>();
Order OrderObj = new Order();
Order.CourceFeeType Fees = new Order.CourceFeeType();
Fees.Code = "1";
Fees.Desc = "w2s";
OrderObj.Amount = 1;
OrderObj.AddFeeTypeDetails(Fees);
List.Add(OrderObj);
OrderObj = new Order();
OrderObj.Amount = 2;
Fees = new Order.CourceFeeType();
Fees.Code = "2";
Fees.Desc = "w22s";
OrderObj.AddFeeTypeDetails(Fees);
List.Add(OrderObj);
rpt.DataSource = List;
rpt.DataBind();
}
}
Repeater Item Bound Data Event Code
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lbl = (Label)e.Item.FindControl("lblDescription");
lbl.Text = ((Order)e.Item.DataItem).FeeType.Desc;
Label lblAmount = (Label)e.Item.FindControl("lblAmount");
lblAmount.Text = ((Order)e.Item.DataItem).Amount.ToString();
}
}
Sample HTML
<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rpt_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>
Amount
</td>
<td>
Description
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblAmount" runat="server"></asp:Label>
</td>
<td>
<asp:Label ID="lblDescription" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>