How to make a link to show all data - c#

I would like to make a task as follow: show the data as links, I only show 3 rows at beginning, so I Take(3). Then, I want a link "Show all", and when I click on it, it will show the rest of data.
I don't know how to do that. Can anyone help me.
Thanks in advance.
Detail.aspx.cs
vertragsnehmer.DataSource = vertrag.Vertragsnehmer.Take(3).Select(x=> new { x.Id, Name = x.GetFullName(), Typ = x.GetType().Name });
vertragsnehmer.DataBind();
Detail.aspx
<asp:Repeater ID="vertragsnehmer" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:HyperLink NavigateUrl='<%# "~/Redirect.aspx?Id=" + Eval("Id") + "&Typ=" + Eval("Typ") %>' Text='<%# Eval("Name") %>' runat="server"></asp:HyperLink>
</td>
<td> </td>
</tr>
</ItemTemplate>

Try this:
place linkbutton on apsx page:
<asp:LinkButton ID="ShowALL" runat="server" onclick="ShowAll_Click">Show All</asp:LinkButton>
on .cs file:
protected void ShowAll_Click(object sender, EventArgs e)
{
vertragsnehmer.DataSource = vertrag.Vertragsnehmer.Select(x=> new { x.Id, Name = x.GetFullName(), Typ = x.GetType().Name });
vertragsnehmer.DataBind();
}

Can you make showAll button postback and rebind the grid with everything in the onCLick event?

Related

Dynamically create ASPxButtons

I want to create a download list where each available file has its own ASPxButton and I would like to dynamically create them rather than creating like 30 manually and dynamically setting them to visible.
Since I don't have a form within my .aspx.cs file but create my buttons within a table in my .aspx code, I can't use the form.AddButton method. Any ideas on how to do something like that?
Not sure if that helps but here is the code for the 2 buttons I already have:
<dx:TabPage Name="Downloads" Text="Downloads" Enabled="false">
<ContentCollection>
<dx:ContentControl runat="server">
<table class="grid_centered">
<tr>
<td>
<dx:ASPxButton ID="btnDownload0" runat="server" Text="Anschreiben" Theme="Metropolis" CssClass="button centered" OnClick="btn_Click" Width="300px"></dx:ASPxButton>
</td>
</tr>
<tr>
<td>
<dx:ASPxButton ID="btnDownload1" runat="server" Text="Serie 1" Theme="Metropolis" CssClass="button centered" OnClick="btn_Click" Width="300px" Visible="false" Enabled="false"></dx:ASPxButton>
</td>
</tr>
</table>
</dx:ContentControl>
</ContentCollection>
</dx:TabPage>
The solution to this is to use an asp:Repeater. This way, I only have to write the details for one and use the OnItemCommand event to handle button clicks. I had to switch to using asp:Button instead of dx:ASPxButton, though since I needed the CommandName field to pass the info which button was pressed along to my event handler.
<asp:Repeater
ID="MyRepeater"
runat="server"
DataSourceID="MyDataSource"
OnItemCommmand="MyClickedEventHandler">
<ItemTemplate>
<asp:Button
ID="MyButton"
runat="server"
Text="Download"
CommandName='<%# $"{Eval("keyField")}" %>' />
</ItemTemplate>
</asp:Repeater>
I am not too familiar with dx:ASPxButton but I am assuming it would be similar to an aspx:Button. To dynamically create controls you have to create the controls in the Page_Load() event.
protected void Page_Load()
{
//get your files
string targetDirectory = "your directory";
string [] fileEntries = Directory.GetFiles(targetDirectory);
int i = 0;
foreach(string fileName in fileEntries)
{
ASPxButton btn = new Button();
btn.Text = fileName;
btn.ID = "btnDownload" + i.ToString();
btn.CssClass = "button centered";
btn.Attributes.Add("Theme", "Metropolis");
//add any other properties you need
//add the event handler
btn.Click += new EvenHandler(btn_Clik);
form.AddButton(btn);
}
}

Problems with Radio button in the datalist

Guys before asking this questions I did a lot of research on this topic but I am not able to detect the problem.I have radio button in the datalist and I am trying to get the selected radio button value.But it is showing me false for all radio button on submit button.My datalist is like this:
<asp:DataList ID="dlEmails" RepeatLayout="Flow" runat="server" >
<HeaderTemplate>
<table>
<tr>
<th>Select Email Address </th>
<th>Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:RadioButton ID="rbtnSelect" Text='<%#Eval("Emails") %>' onclick='fnrad(this);' GroupName="a" Checked='<%#Eval("Primary") %>' runat="server" /><br />
(<asp:Label ID="lablel" runat="server" Text='<%#Eval("Verified") %>'> </asp:Label>)
</td>
<td valign="middle">
<asp:Label ID="lblID" Style="display: none;" runat="server" Text='<%#Eval("Id") %>'> </asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Main") %>'> </asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Javascript for allowing only single radio button selection at a time is like this:
<script>
function fnrad(rbtn) {
var radioList = document.getElementsByTagName("input");
for (var i = 0 ; i < radioList.length; i++) {
if (radioList[i].type == "radio") {
radioList[i].name = 'a';
radioList[i].checked = false;
}
}
rbtn.checked = true;
rbtn.setAttribute("Checked","checked");
}
</script>
And my code behind is like this:
public partial class Member_EmailList : System.Web.UI.Page
{
EmailsBAL _mbl = new EmailsBAL(SessionContext.SystemUser);
DataSet _ds = new DataSet();
URLMessage URLMessage = new URLMessage();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
_mbl.LoadByUser(_ds, 1);//SessionContext.SystemUser;
dlEmails.DataSource = _ds.Tables[_mbl.SqlEntityX];
dlEmails.DataBind();
}
protected void lnkConfirm_Click(object sender, EventArgs e)
{
RadioButton rb;
Label lbl;
int id = 0;
foreach (DataListItem di in dlEmails.Items)
{
rb = (RadioButton)di.FindControl("rbtnSelect");
if (rb.Checked == true)
{
lbl = (Label)di.FindControl("lblID");
id = WebHelper.Cast(lbl.Text, 0);
}
}
//Response.Redirect("~/Member/ConfirmEmail.aspx?" + URLMessage.Encrypt("SystemUser=" + SessionContext.SystemUser + "Id=" + id.ToString()));
}}
I tried your javascript function and experienced your problem
I would like to suggest to use Jquery on your javascript function. I refactored your jav fuction to this.
function fnrad(rbtn) {
$('input').removeAttr('checked');
$(rbtn).prop('checked', true);
}
This works perfectly on my end. Please let me know if you still encountering the issue.

Label ASP not being set

I have a label that is dynamically generated via a repeater, rollNo is a label that's a part of the itemTemplate. When I check the value of l, it goes in the if block but l.Text is still empty. check.Text only returns "d". Why?
Label l = (Label)item.FindControl("rollNo");
TextBox t = (TextBox)item.FindControl("quiz1");
if (l != null)
{
string a = l.Text;
check.Text = "d"+a;
}
Your code sample isn't complete as it has no rollNo in it, but I can tell you something...
You are using repeater and with that using template...The id you use inside the template is never will be the run-time id of any of your controls! Think about it! Let say you assigned rollNo to one of the elements in the template and you have 10 rows to pass to the repeater. Are you expecting to have 10 controls with the the same id of rollNo?! I hope not!
For that reason FindControl will return nothing while using on id inside a template...
You have to rethink what do you want or use a different approach to find the controls (loop)...
Repeater Markup:
<asp:Repeater id="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<tr onclick="rowReturn(this)">
<td><asp:Label CssClass="form-control" runat="server" ID="rollNo"><%# DataBinder.Eval(Container.DataItem, "sid") %></asp:Label></td>
<td><asp:TextBox CssClass="form-control" runat="server" ID="quiz1" required></asp:TextBox></td>
<td><asp:TextBox CssClass="form-control" runat="server" ID="quiz2" required></asp:TextBox></td>
<td><asp:Button CssClass="btn btn-success btn-sm form-control" ID="add" CommandName="add" runat="server" Text="Add" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "sid") %>' /></td>
</tr>
</ItemTemplate>
Code Behind:
TextBox t1;
TextBox t2;
string rollNumber, T1, T2;
if (e.CommandName == "add")
{
// get CommandArgument you have selected on the button
string roll = e.CommandArgument.ToString();
rollNumber = roll;
foreach (RepeaterItem item in Repeater1.Items)
{
t1 = (TextBox)item.FindControl("quiz1");
t2 = (TextBox)item.FindControl("quiz2");
T1 = t1.Text;
T2 = t2.Text;
//...DB code or any other code
}
}

Locate control in asp:listview when I have the clientid for the control?

VS2012, .NET 4.51, WebForms
From a click event in the code behind I have the client ID for a control within the ItemTemplate (naturally said control is created for each of the records within the listview):
var lastControlWithFocusClientId = "cphContainer_ucTakeTest_lvData_txtAnswer_0";
I need to find that control so I tried:
lvData.FindControl(lastControlWithFocusClientId)
and
Page.FindControl(lastControlWithFocusClientId)
However both return null (ie control not found.) So what am I missing here?
EDIT ListView markup added:
<asp:ListView runat="server"
ID="lvData"
ItemType="MyItemType"
SelectMethod="GetQuestions"
OnItemDataBound="lvData_OnItemDataBound" OnItemCreated="lvData_ItemCreated">
<ItemTemplate>
<tr>
<td><span class="label label-info"><%#: Item.QuestionNumber %></span></td>
<td>
<asp:Label ID="lblQuestionText" runat="server" Text='<%# Eval("QuestionText") %>' />
</td>
<td>
<img src='/ImageHandler.ashx?questionNo=<%#: Item.QuestionNumber %>'>
</td>
<td>
<div class="input-group">
<asp:TextBox data-sessionid='<%# SessionsId %>' data-question-no='<%#: Item.QuestionNumber %>' Enabled='<%# !ShowStudentResults %>' CssClass="form-control FlyoutCandidate" ID="txtAnswer" runat="server"></asp:TextBox>
<span data-content='<%#: GetAnswerInstructions(Item.SolutionType) %>' class="input-group-addon flyout-candidate-hint"><span class="glyphicon glyphicon-comment"></span></span>
<span class="input-group-addon special-character-toggle"><span class="glyphicon glyphicon-credit-card"></span></span>
</div>
</td>
<td>
<img runat="server" data-sessionid='<%# SessionsId %>' data-qno='<%# Item.QuestionId %>' data-id='<%# Item.QuestionNumber %>' onclick="javascript: TakeTestJs.DisplayQuestionHelp(this); return false;" src="/Images/help-icon.png" width="32" height="32" alt="" />
</td>
<td>
<asp:Panel runat="server" ID="imgHint"></asp:Panel>
</td>
<td>
<asp:Label CssClass="label label-info" ID="lblStudentMark" runat="server" Text='<%# Item.StudentMark %>' />
</td>
<td>
<asp:Label CssClass="label label-primary" ID="lblOutOf" runat="server" Text='<%# Item.QuestionOutOf %>' />
</td>
<td>
<asp:Label ID="lblSolutionText" runat="server" Text='<%# Item.SolutionText %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
findcontrol is not recursive, so you have to do a bit of workaround. Here is my code, modified version of what I got from somehwere around here:
private Control RecursiveFindControl(Control targetControl, string findControlId) {
if (targetControl.HasControls()) {
foreach (Control childControl in targetControl.Controls) {
if (childControl.ID == findControlId) {
return childControl;
}
RecursiveFindControl(childControl, findControlId);
}
}
return null;}
just use:
RecursiveFindControl(this, ControlName)
Since you are using .NET4 and higher, you can actually set the ClientMode property and set this yourself so you don't even have to work with the confusing ClientIDs that are generated by ASP.NET and nested server controls.
Check out this MSDN article for the ClientIDMode property
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.110%29.aspx
Otherwise, to maybe more accurately answer your question, you can't do a FindControl on the controls collection itself because it will fail.
Typically when I work with a ListView, I end up taking the control that triggers the event for that row.
So if I have a LinkButton in the row, and I have a click event, I will do the following
protected void btnConfirm_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
ListViewDataItem row = btn.NamingContainer as ListViewDataItem;
if (row != null)
{
HiddenField hiddenFieldWithSomething = row.FindControl("hiddenControl") as HiddenField;
//var lastControlWithFocusClientId = cphContainer_ucTakeTest_lvData_txtAnswer_0";
if (hiddenFieldWithSomething.ClientID == lastControlWithFocusClientId)
{
//Do something here
}
}
}
When doing it this way, it's a little more fine-tuned and you still go through your typical event handler.
The RecursiveFindControl helper that was posted in this question will give you a much more robust and reusable solution.
I ended up implementing the following recursive search as #user2930100 did not work for me:
private Control RecursiveFindControl(Control aRootControl, string aFindControlClientId)
{
if (aRootControl.ClientID == aFindControlClientId)
return aRootControl;
foreach (Control ctl in aRootControl.Controls)
{
Control foundControl = RecursiveFindControl(ctl, aFindControlClientId);
if (foundControl != null)
return foundControl;
}
return null;
}

How I use the ItemCommand Event for my ListView in my ASP.NET Application

I have a ASP.NET Application with a ListView. In every Row in my ListView I have a LinkButton that open a new webform "Benutzer.aspx". my Problem is that I don't get the Index of this Row. I use the ItemCommand Event but it not work :(
Here my Code:
ASPX:
...
<ItemTemplate>
<tr runat="server">
<td align="left" ><asp:Label ID="Label1" Text='<%# Eval("Benutzer") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label2" Text='<%# Eval("eMail") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label3" Text='<%# Eval("Vorname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label4" Text='<%# Eval("Nachname") %>' runat="server" /></td>
<td align="left"><asp:Label ID="Label5" Text='<%# Eval("Telefon") %>' runat="server" /></td>
<td align="left"><asp:LinkButton runat="server" Text="Anzeigen" CommandName="Anzeigen" OnCommand="ListView1_ItemCommand" CommandArgument="myArguments"></asp:LinkButton></td>
</tr>
</ItemTemplate>
...
cs file:
...
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Anzeigen")
{
Label lbText = (Label)e.Item.FindControl("Label2");
string email = lbText.Text;
Session["email"] = email;
Response.Redirect("Benutzer.aspx");
}
}
...
What is the matter :(
tarasov
Try this:
First you need to have the index of the button. So in the html code add this in the CommandArgument of the button to get the index:
CommandArgument='<%# Container.DataItemIndex %>'
Then in the codebehind:
if (e.CommandName == "Anzeigen")
{
Label lbText = ListView1.Item[e.CommandArgument].FindControl("Label2");
string email = lbText.Text;
Session["email"] = email;
Response.Redirect("Benutzer.aspx");
}
Hope I Helped
You cannot find the control because it is contained in the child control collection of another server control:
<tr runat="server">
You need to try to find the control recursively:
Take a look
Better way to find control in ASP.NET
Or you can use this extension method:
public static class ControlExtensions
{
public static Control FindControlRecursively(this Control control, string targetControlID)
{
if (control == null)
{
return null;
}
var ctrl = control.FindControl(targetControlID);
if (ctrl == null)
{
foreach (Control child in control.Controls)
{
ctrl = FindControlRecursively(child, targetControlID);
if (ctrl != null)
{
break;
}
}
}
return ctrl;
}
}
Usage:
var ctrl = e.Item.FindControlRecursively("your control ID");
The code you have furnished is simply fine... "just remove the 'CommandArgument' from your listview property , bcoz..its already have the dataindex you are looking for. By specifying a command argument you are overriding the default one.
So just remove the command argument and your code will work fine... :)
I am a VB programmer Check this method may b it gives you some idea
after binding the list with datasource, In the itemCommand do this
Dim <sometext> As Label = TryCast(e.Item.FindControl("Anzeigen"), Label)
If e.CommandName = "Anzeigen" Then
'do what ever you like
'also you can use <sometext> if you want to extract data from list
'simply use <sometext>.<whatproperty>, you can also store it in sessions like the email you are using.
Session("email") = email
Response.Redirect("Benutzer.aspx");
End If
let me know if it helps you solve your problem.
This is the HTML, then build the OnItemCommand.
<asp:ListView ID="lvFiles" runat="server" DataKeyNames="FileName" OnItemCommand="lvFiles_ItemCommand">
<ItemTemplate>
<tr runat="server">
<td style="width:80px">
<asp:LinkButton runat="server"
ID="SelectEmployeeButton"
Text="Download File"
CommandName='<%#Eval("FileName")%>'
CommandArgument='<%#Eval("FileName")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Here is the code behind...
protected void lvFiles_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string v = e.CommandArgument.ToString();
}

Categories