Creating dynamic repeater in asp.net - c#

Hi I am building an online sports goods shop,
I have this plan of loading in all Sports Type in the Home page, like Football,Cricket,Basketball etc
And the administrator can create a game at his own will,
Here's the confusion
How do I display the SubCategories of each Game if clicked (inside the repeater).
I thought of adding an ImageButton to it!! But then how do I link that Image Button to the games, i.e. when the user clicks the respective Image button -> The subcategories of that game should be displayed
For example:
1. If I have games such as Cricket,Football etc.
2. The Repeater should show all the games in the repeater
3. When The User clicks on for instance Cricket
4. I wish to load all subcategories of cricket goods such as the BAT,BALL,STUMPS etc.
I attempted this by loading the games in Repeater as shown in below code snippet:
<asp:Repeater ID="RepDetails" runat="server"
ondatabinding="RepDetails_DataBinding">
<HeaderTemplate>
<table style="border: 1px solid #df5015; width: 500px" cellpadding="0">
<tr style="background-color: #df5015; color: White">
<td colspan="2">
<b>Type of Sports</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color: #EBEFF0">
<td>
<table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; width: 500px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>' />
</td>
<td>
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Category") %>' Font-Bold="true" />
</td>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton2_Click" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I've even added the ImageButton but confused about making it load the respective subcategories of that game!
Suggestions are welcome if there can be another work around which can be more effective..

You could try a nested repeater
In aspx
<asp:Repeater ID="RepDetails" runat="server" OnDataBinding="RepDetails_DataBinding">
<HeaderTemplate>
<table style="border: 1px solid #df5015; width: 500px" cellpadding="0">
<tr style="background-color: #df5015; color: White">
<td colspan="2">
<b>Type of Sports</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color: #EBEFF0">
<td>
<table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; width: 500px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>' />
</td>
<td>
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Category") %>' Font-Bold="true" />
</td>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton2_Click" />
</td>
</tr>
</table>
</td>
</tr>
<asp:Repeater ID="SportsProps" runat="server">
<ItemTemplate>
<tr style="background-color: #EBEFF0">
<td>
<table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; width: 500px">
<tr>
<td>
<asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Name") %>' Font-Bold="true" />
</td>
<td>
<asp:ImageButton ID="ImageButton3" runat="server" OnClick="ImageButton3_Click" />
</td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RepDetails.DataSource = GetData();
RepDetails.DataBind();
}
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Repeater repeater = ((ImageButton)sender).NamingContainer.FindControl("SportsProps") as Repeater;
Label catLabel = ((ImageButton)sender).NamingContainer.FindControl("lblSubject") as Label;
repeater.DataSource = GetDataDetail(catLabel.Text);
repeater.DataBind();
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
//do something to hide the
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("category", typeof(string));
dt.Rows.Add("1 ", "Basketball");
dt.Rows.Add("2 ", "Football");
dt.Rows.Add("3 ", "Soccer");
return dt;
}
private DataTable GetDataDetail(string category)
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Rows.Add("Bat");
dt.Rows.Add("Ball");
dt.Rows.Add("Stump");
return dt;
}

Related

How get data from database column as a heading using Repeater in ASP.NET

image
The Upper portion of present my database table structure and lower portion that in which form i want to show data.
<table class="table table-bordered table-hover">
<thead>
<tr class="text-center">
<th>Course Code</th>
<th>Subject</th>
<th>Cr.Hours</th>
<th>Grade</th>
</tr>
<tr>
<td colspan="4">
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</thead>
<tbody>
<asp:Repeater ID="outer" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:Label Text='<%#Eval("[SpringFall]")%>' Style="text-align: center; display: none; font-size: 18px; font-family: 'Times New Roman'" ID="Label4" runat="server" Font-Bold="true"> </asp:Label>
</td>
<td>
<asp:Label ID="AllCcode" runat="server" Text='<%#Eval("[Course_Code]") %>'></asp:Label>
</td>
<td>
<asp:Label ID="AllSubject" runat="server" Text='<%#Eval("[Subject]") %>'></asp:Label>
</td>
<td>
<asp:Label ID="AllCrHr" runat="server" Text='<%#Eval("[Credit_Hours]") %>'></asp:Label>
</td>
<td>
<asp:Label ID="AllGrade" Font-Bold="true" runat="server" Text='<%#Eval("[Total_Marks]") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
Here Is my code..bind repeater on button click event using c# with simple sql SELECT query.Any query for bind repeater with more efficient working and show data in given form.
protected void AttendencBtn_Click(object sender, EventArgs e)
{
// MultiView1.ActiveViewIndex = 8;
conn.Open();
sda = new SqlDataAdapter("SELECT * FROM Attendence where Roll_Number='" + Session["RollNumb"] + "' ", conn);
dt = new DataTable();
sda.Fill(dt);
AttendencRpt.DataSource = dt;
AttendencRpt.DataBind();
conn.Close();
}
I think you are looking for something like this. First create a public variable in code behind.
public int currentYear = 2000;
protected void Page_Load(object sender, EventArgs e)
{
}
Then change the Repeater to something like this:
<table border="1">
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("myYear")).Year != currentYear && Container.ItemIndex > 0 ? "<tr><td colspan=\"5\">" + Eval("myYear") + "</td></tr>" : "" %>
<tr>
<td>
normal rows here
</td>
</tr>
<%# currentYear = Convert.ToDateTime(Eval("myYear")).Year %>
</ItemTemplate>
</asp:Repeater>
</table>
What will happen is that the value of currentYear is compared to the current row value. If it does not match a new row will be created. Then the value of currentYear is updated to be checked in the next row bound to the Repeater.

Fix button onclick behind code with Formview

I make i my own mail interface system into my application and since the user click on Repsendbtn into listview called " viewmsgView " then he will hide the " viewmsgView" and show " repmsgform " and bind the data into repmsgform depending on mailno. What i am facing is as you all know that cant reach controls into listview or formview and the Repsendbtn is inside the formview and it will depend on others controls to process the below behind code but i am getting red-line under the controls id with message: " The name 'Label' does not exist in the current context " so how i can solve this case and make the button code work smoothly. Its may really not so clear my post, so i decide to make record screen which i hope will make it easily to explain what i am looking for. Please click on the below link and have a look of my screen record where i explain what i am looking for:
https://www.youtube.com/watch?v=OMKp7b6iMJ8&feature=youtu.be
<asp:FormView ID="repmsgform" runat="server" DataKeyNames="mailno" Width="100%" >
<ItemTemplate>
<div>
<table class="table table-bordered" style="margin-left:0px;" >
<tbody>
<tr>
<td style="border-collapse:collapse; border:2px solid white; color:#333333; background-color:#f0f0f0;">
Ads Number:
</td>
<td >
<asp:Label ID="adsnummsglbl" runat="server" Text='<%# Bind("AdsID") %>' ></asp:Label>
</td>
</tr>
<tr>
<td td style="border-collapse:collapse; border:2px solid white; color:#333333; background-color:#f0f0f0;">
Message Title:
</td>
<td>
<asp:Label ID="adstitmsglbl" runat="server" Color="#669900" Text='<%# Bind("Mestitle") %>'></asp:Label>
</td>
</tr>
<tr>
<td td style="border-collapse:collapse; border:2px solid white; color:#333333; background-color:#f0f0f0;">
From:
</td>
<td>
<asp:Label ID="Reciverlblnme" runat="server" Text='<%# Bind("Receiver") %>'></asp:Label>
</td>
</tr>
<tr>
<td style="border-collapse:collapse; border:2px solid white; color:#333333; background-color:#f0f0f0;">To:</td>
<td> <asp:Label ID="Tolbl" runat="server" Text='<%# Bind("sender") %>'></asp:Label></td>
</tr>
</tbody>
</table>
<div class="form-group">
<asp:Label ID="Label12" runat="server" Text="Label" CssClass="col-md-3 control-label">Your Message:</asp:Label>
<div class="col-md-9">
<asp:TextBox ID="TextBox1" runat="server" Height="150px" TextMode="multiline" CssClass="form-control"
></asp:TextBox>
</div>
</div>
<asp:Label ID="msgsentlbl" runat="server" Font-Size="Medium" ForeColor="#669900"></asp:Label>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="pull-right">
<asp:Button ID="Closebtn" runat="server" Text="Close"
CssClass="btn btn-default" onclick="Closebtn_Click"/>
<asp:Button runat="server" ID="Repsendbtn" Text="Send" CssClass="btn btn-primary"
onclick="Repsendbtn_Click"/></div>
</div></div>
</ItemTemplate>
</asp:FormView>
protected void Repsendbtn_Click(object sender, EventArgs e)
{
if (Session["UsrNme"] != null)
{
using (SqlConnection cn = new SqlConnection(sc))
{
string SendMsgSQL = #"INSERT mails [CVs] ([Message], [Mestitle], [AdsID], [Date], [Receiver], [sender])
VALUES (#Message, #Mestitle, #AdsID , #Date, #Receiver, #sender)";
using (SqlCommand SendMsgcmd = new SqlCommand(SendMsgSQL, cn))
{
cn.Open();
var user = Session["UsrNme"];
SendMsgcmd.Parameters.AddWithValue("#sender", user);
SendMsgcmd.Parameters.AddWithValue("#Message", TextBox1.Text);
SendMsgcmd.Parameters.AddWithValue("#Mestitle", adstitmsglbl.Text);
SendMsgcmd.Parameters.AddWithValue("#AdsID", adsnummsglbl.Text);
SendMsgcmd.Parameters.AddWithValue("#Date", DateTime.Now);
SendMsgcmd.Parameters.AddWithValue("#Receiver", Reciverlblnme.Text);
SendMsgcmd.ExecuteNonQuery();
Response.Redirect("User panel.aspx");
}
}
}
else
{
// Consider throwing an error (if these fields are required)
}
}

ListView string from aspx to codebehind

Hello I have following aspx code and when the button inside ItemTemplate is cliked i need to pass the Item.BookID into code behind and I am unsure how to do it as there can be multiple items in the view. Thank you help would be much appreciated.
<asp:ListView runat="server" ID="UserDetailBooks" DefaultMode="ReadOnly" ItemType="WebApplication1.Models.Borrowed" SelectMethod="GetBorrow" DeleteMethod="ReturnBook">
<EmptyDataTemplate>
<h3>No borrowed books!</h3>
</EmptyDataTemplate>
<LayoutTemplate>
<div style="margin-left: auto; margin-right: auto; width: 50%;">
<h4>Books in possesion:</h4>
<table style="border-spacing: 2px;">
<tr id="groupPlaceholder" runat="server">
</tr>
</table>
</div>
</LayoutTemplate>
<GroupTemplate>
<tr>
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td><asp:Button runat="server" Text="Return" OnClick="ReturnBook" />
<%#:Item.BookTitle %>
</td>
</ItemTemplate>
</asp:ListView>
You could pass the id in the button attributes?
<asp:Button runat="server" Text="Return" BookID="<%#:Item.BookID %>" OnClick="ReturnBook" />
<%#:Item.BookTitle %>
void ReturnBook(object sender, EventArgs e) {
Button b = sender;
string BookId = b.Attributes["BookID"];
}

How to bind TextBox text to RadListBox

I have two RadListBoxes, i.e radListBoxSource and RadListBoxDestination.
Here I am binding radListBoxSource items with DataSource and transfer to RadListBoxDestination.
Now I want to add some text from TextBox to RadListBoxDestination which data is not in radListBoxSource.
For this I added TextBox and Button.
Please tell me how to bind TextBox data and radListBoxSource data to RadListBoxDestination.
.aspx:
<telerik:RadListBox runat="server" ID="radListBoxSource" Height="350px" Width="250px"
EnableDragAndDrop="true" TransferMode="Move" SelectionMode="Multiple" AllowTransfer="true"
TransferToID="RadListBoxDestination" AutoPostBackOnTransfer="true" DataSortField="CandidateColumn"
DataKeyField="ColumnID" AllowReorder="true">
<EmptyMessageTemplate>
No columns exist
</EmptyMessageTemplate>
<HeaderTemplate>
<table width="100%">
<tr>
<td style="font-weight: bold; text-align: left;">
Candidate Columns
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<div style="width: 100%;">
<%# Eval("CandidateColumn") %>
</div>
</ItemTemplate>
</telerik:RadListBox>
<telerik:RadListBox ID="RadListBoxDestination" Height="350px" Width="250px" runat="server"
SelectionMode="Multiple" EnableDragAndDrop="true" AllowReorder="true">
<EmptyMessageTemplate>
No columns exist
</EmptyMessageTemplate>
<HeaderTemplate>
<table width="100%">
<tr>
<td style="font-weight: bold; text-align: left;">
Candidate Columns
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<div style="width: 100%;">
<%# Eval("CandidateColumn") %>
</div>
</ItemTemplate>
</telerik:RadListBox>
<table>
<tr>
<td>
<asp:Label ID="lblText" runat="server" Text="Enter Custom Field Name" Font-Bold="true"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtFieldName" runat="server" Width="175px"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnAdd" CssClass="form_btn_txt" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
</table>
.cs:
protected void btnAdd_Click(object sender, EventArgs e)
{
RadListBoxItem test1 = new RadListBoxItem("item 1");
test1.Text = txtFieldName.Text.Trim();
RadListBoxDestination.Items.Insert(test1.Clone());
}
And I am binding radListBoxSource as follows:
private void FillComboData()
{
BizCandidate bizCandidates = new BizCandidate();
DataSet dsCandidates = new DataSet();
try
{
dsCandidates = bizCandidates.GetCandidateColumns();
if (dsCandidates != null && dsCandidates.Tables.Count > 0)
{
if (dsCandidates.Tables[0].Rows.Count > 0)
{
radListBoxSource.DataSource = dsCandidates.Tables[0];
radListBoxSource.DataValueField = "ColumnID";
radListBoxSource.DataTextField = "CandidateColumn";
radListBoxSource.DataBind();
}
}
}
catch { }
}
Remove the itemTemplate from the RadListBoxDestination, as you are not using a data source for this list box:
<telerik:RadListBox ID="RadListBoxDestination" Height="350px" Width="250px" runat="server"
SelectionMode="Multiple" EnableDragAndDrop="true" AllowReorder="true">
<EmptyMessageTemplate>
No columns exist
</EmptyMessageTemplate>
<HeaderTemplate>
<table width="100%">
<tr>
<td style="font-weight: bold; text-align: left;">
Candidate Columns
</td>
</tr>
</table>
</HeaderTemplate>
</telerik:RadListBox>

Retrieve Data from Repeater item

I have the following Repeater:
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound">
<ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Am I trying to loop through each item, find one where the radio button is checked. If a radio button is checked i want to then check its value (Accept or Reject) and retrieve the data (Eval Date,Hours etc) and send it to another method to add to the database.
Can you please help me out, code so far:
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in RptLeaveRequests.Items)
{
}
}
You use <asp:RadioButtonList> , but in your code behind , you cast as RadioButton .
Try likes this ,
foreach (RepeaterItem item in RptLeaveRequests.Items)
{
var rdbList = item.FindControl("rbtVerified") as RadioButtonList;
switch(rdbList.SelectedValue)
{
case "1":
//Accept
break;
case "2":
//Reject
break;
}
}

Categories