How to populate html block several times asp.net - c#

I would like to make a kanban for my application. I want to populate it with a mysql table. To do so I need to repeat the following lines of code several times.
<li class="drag-column drag-column-needs-review" id="list2" runat="server">
<span class="drag-column-header" runat="server" id="listspan">
<asp:Label ID="Label3" runat="server" Text="Label">Needs Review</asp:Label>
</span>
<div class="drag-options" id="options3"></div>
<ul class="drag-inner-list" runat="server" id="list1">
</ul>
</li>
How can I do so? I am unsure what to do in aspx.cs Here is what I have tried so far.
protected void Page_Load(object sender, EventArgs e)
{
PopulateLists();
}
public void PopulateLists()
{
HtmlGenericControl li;
for (int j = 0; j < 5; j++)
{
li = new HtmlGenericControl("li");
li.Attributes.Add("class", "drag-column drag-column-needs-review");
li.InnerText = "Item" + j;
list2.Controls.Add(li);
li.Style.Add("background-color", "green");
for (int i = 0; i < 3; i++)
{
li = new HtmlGenericControl("li");
li.Attributes.Add("class", "drag-item");
li.InnerText = "Item" + i;
if (i % 2 == 0)
{
li.Style.Add("background-color", "#ffff00");
}
else
{
li.Style.Add("background-color", "red");
}
list1.Controls.Add(li);
list1.Attributes.Add("onclick", "return ShowModalPopup()");
}
}
}
The code above gives my a list with rows but only for the first list the other lists are not being populated.
the red arrows in images is what I want to do. Any help would be very appreciated thank you.

Related

Need help with binding Telerik's RADStrip dynamically

Followed this example on telerik's website and implemented as follows:-
aspx page:-
<div id="div1" runat="server">
<telerik:RadTabStrip ID="RadTabStrip1" runat="server" MultiPageID="RadMultiPage1">
</telerik:RadTabStrip>
<telerik:RadMultiPage ID="RadMultiPage1" runat="server" Width="100%">
<telerik:RadPageView ID="pv1" runat="server" Selected="true">
</telerik:RadPageView>
</telerik:RadMultiPage>
</div>
code behind:-
This method in the line dataset ds = objbllQuesType.GetQuesType(); returns a DataSet with a table containing QuesTypeID and QuesType's Name
Now what is happening is, only 4 Tabs are being generated on the RadStrip which I know is coz of the "for loop (int i = 0; i < 4; i++)"
I want the no. of tabs to be equal to the no. of QuesType s I have in my Table which is 6.
Also , ALL the tabs' Text is "QuesType1" when it should be as follows:-
In the RadStrip, Tab1's Text should be "QuesType1", Tab2's Text should be "QuesType2" , Tab3's Text should be "QuesType3" and so on.
How should I modify the code above to achieve what I want ?
[EDIT]
Has anyone worked with Telerik's RADStrip? Need help plz.
What should I change in the code below so it fetches the QuesTypes and binds QuesTypes to the tabs ? Currently its only fetching 1st QuesType and binding that one to ALL the tab's text attribute.
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
tab.Text = ds.Tables[0].Rows[0]["QuesType"].ToString();
}
Try this code for set text to tabs:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet ds = GetDataSet();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CreateRootTab(i, ds);
}
RadTabStrip1.SelectedIndex = 0;
}
}
private void CreateRootTab(int index, DataSet ds)
{
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
var tab = new RadTab();
tab.Text = (string) ds.Tables[0].Rows[index].ItemArray[i];
RadTabStrip1.Tabs.Add(tab);
}
}
private DataSet GetDataSet()
{
bllQuesType objbllQuesQType = new bllQuesType();
var ds = new DataSet();
return objbllQuesType.GetQuesType();
}
I didn't work with Rad, but I think it can be helpfull in your case.
Best regards, Dima.

RadTreeView Visual Clutter

I'm very new to Telerik and its functionality. For my user management, I decided to show an admin a TreeView to select whether a user can access some property or not. But When I hover mouse cursor on the checkboxes, the title of each textbox ruined! You see in the image below:
A: TreeView in normal shape
B: TreeView when hover on title
C: TreeView when hover on checkboxes
ASPX code:
<asp:Panel ID="Panel1" runat="server" GroupingText="MenuAccess">
<div style="overflow: auto; width: 230px; height: 200px; display: block; borderstyle: Solid;borderwidth: 1px">
<telerik:RadTreeView runat="server" ID="RadTreeView1" CheckChildNodes="true" CheckBoxes="true" Font-Size="15px"
Visible="true" SelectedNodeStyle-BorderStyle="Dashed" ForeColor="Black" SelectedNodeStyle-BorderWidth="1px"
SelectedNodeStyle-Font-Bold="true" Font-Names="B Nazanin" NodeStyle-Font-Size="23px" NodeStyle-HorizontalPadding="2px">
</telerik:RadTreeView>
</div>
</asp:Panel>
Code behind that creates the items of TreeView:
public void FillTreeViewForMenuDynamic(RadTreeNode n, int id = 0)
{
var list = db.AccessRightCatalogs.Where(x => x.ParentMenuId == id).ToList();
foreach (var item in list)
{
RadTreeNode p = new RadTreeNode();
p.Text = item.Title;
p.Value = item.Id.ToString();
n.Nodes.Add(p);
if (userAccessList.Contains(item.Id))
p.Checked = true;
FillTreeViewForMenuDynamic(p, item.Id);
if (item.Id == 5)
{
p.Checked = true;
p.Enabled = false;
p.ForeColor = Color.Black;
}
}
if (list.Count > 0)
{
if (isparent(list[0].Title))
{
if (n.Text == "")
{
int count = n.Nodes.Count;
List<RadTreeNode> t = new List<RadTreeNode>();
for (int i = 0; i < count; i++)
{
RadTreeNode temp = n.Nodes[i];
t.Add(temp);
}
for (int i = 0; i < t.Count; i++)
{
RadTreeView1.Nodes.Add(t[i]);
}
}
else
RadTreeView1.Nodes.Add(n);
}
}
}
P.S. As I'm new to this please take it easy.
P.S. Sorry for bad English or bad explanation, if you need more clarification just ask.

Retrieving the value of dynamically generated text boxes in code behind on a click event of a button?

In the code below I am genrating some textboxes dynamically. And I am trying to get there values on the click event of a asp button. I have tried many of the ways but none seems to be working. Also I am trying to avoid using ajax to send the values. Any help in this matter will be truly appreciated.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="product_entry.aspx.cs"Inherits="product_entry" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="pos.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function() {
var maxrow = 1;
$('#TextBox1').val(maxrow);
//var scrolled = 0;
$("td.sno").each(function(index, element) {
$(element).text(index + 1);
});
$(document).on("click", "button.remove", function() {
$(this).parent().parent().remove();
adding();
});
$(document).on("click", ".add, .remove,.tabrow:last", function() {
numbering();
});
$(document).on("focus", ".tabrow:last", function() {
// scrolled = scrolled + 200;
//$("#table").animate({
// scrollTop: scrolled,20:"linear"
//});
adding();
});
function numbering() {
$("td.sno").each(function(index, element) {
$(element).text(index + 1);
maxrow = index + 1;
});
}
function iding() {
$("tr").each(function(trIdx, element) {
var trIndex = 'textbox' + (trIdx);
// alert('hello');
$(this).find("td > input").attr('id', function(idx) {
return trIndex + (idx + 1)
});
});
}
function adding() {
$("tr.tabrow:first").clone(true).appendTo("#Table1");
numbering();
iding();
$(".tabrow:last").children("td").children("input").each(function(index, element) {
$(element).val("");
});
$('#TextBox1').val(maxrow);
}
$(document).on("click", ".check", function() {
setdata();
});
function setdata() {
var rowValues
$("tr").each(function(index, value) {
rowValues = new Array(); $(this).find('input').each(function() {
if ($(this).val() != '') {
alert('hello');
rowValues.push($(this).val());
}
})
alert("row" + index + " : " + rowValues);
//do something with rowValues
}
)
$.ajax({
type: "POST",
url: "pdtentry.aspx/Done",
contentType: "application/json; charset=utf-8",
data: "{ info: rowValues}",
dataType: "json",
success: function(result) {
alert('Yay! It worked!');
},
error: function(result) {
alert('Oh no :(');
}
});
return false;
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="mhead">
<asp:Label ID="Label1" runat="server" Text="Enter product into database"></asp:Label>
</div>
<div>
<asp:TextBox ID="TextBox1" runat="server" Style="display: none;"></asp:TextBox>
</div>
<asp:Panel ID="Pnl" runat="server">
<div id="table">
<asp:Table ID="Table1" runat="server">
<asp:TableHeaderRow ID="head">
<asp:TableHeaderCell ID="remove">-</asp:TableHeaderCell>
<asp:TableHeaderCell ID="sno">S.No</asp:TableHeaderCell>
<asp:TableHeaderCell ID="pdt_id">Product ID</asp:TableHeaderCell>
<asp:TableHeaderCell ID="pdt_descp">Product Description</asp:TableHeaderCell>
<asp:TableHeaderCell ID="plu">PLU</asp:TableHeaderCell>
<asp:TableHeaderCell ID="pdt_type">Product Type</asp:TableHeaderCell>
<asp:TableHeaderCell ID="cost">Cost</asp:TableHeaderCell>
<asp:TableHeaderCell ID="discount">Discount</asp:TableHeaderCell>
<asp:TableHeaderCell ID="qty">Quantity</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
</asp:Panel>
<div>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" OnClick="Button1_Click"
UseSubmitBehavior="False" /></div>
<input id="Button2" type="button" value="button" class="check" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
And code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Services;
public partial class product_entry : System.Web.UI.Page
{
int count;
static String[] a;
protected void Page_Load(object sender, EventArgs e)
{
int i,j;
for (i = 0; i < 1; i++)
{
TableRow tr = new TableRow();
tr.Attributes.Add("class", "tabrow");
for (j = 0; j <= 8; j++)
{
TableCell tc = new TableCell();
if (j == 0)
{
tc.Controls.Add(new LiteralControl("<Button class=remove type=button>-</button>"));
}
if (j == 1)
{
tc.Attributes.Add("class", "sno");
}
if (j == 2 || j == 3 || j == 4 || j == 5 || j == 6 || j == 7 || j == 8)
{
TextBox tb = new TextBox();
tb.Style["width"] = "98%";
tc.Controls.Add(tb);
}
tr.Controls.Add(tc);
}
Table1.Controls.Add(tr);
}
}
[WebMethod]
public static string Done(string[] info)
{
a = info;
Label lb2=new Label();
lb2.Text=""+a.Length;
string str = string.Empty;
return str;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = TextBox1.Text;
count = Convert.ToInt32(TextBox1.Text);
for (int i = 1; i <= count; i++)
{
for (int j = 1; j <= 7; j++)
{
Response.Write("textbox" + i + j);
string aa = ((TextBox)Pnl.FindControl("textbox" + i + j)).Text;
Response.Write(((TextBox)Pnl.FindControl("textbox11")).Text);
}
}
/* try
{
int count = Convert.ToInt32(TextBox1.Text);
for (int i = 1; i <= count; i++)
{
for (int j = 1; j <= 7; j++)
{
Label l = new Label();
string str = "textbox" + i + j;
TextBox tb=((TextBox)this.FindControl(str)).Text;
l.Text = tb.Text;
}
}
}
catch { }
Panel[] pnls = new Panel[1] { Pnl };
//ArrayList answers = new ArrayList();
//Alternatively....
ArrayList arrayList = new ArrayList();
//string[] arr=new string[7];
//arrayList.Add(arr);
//for (int i = 0; i < 7 ; i++)
//{
// ArrayList list2 = new ArrayList();
//arrayList.Add(list2);
//}
//System.Collections.Hashtable ht = new System.Collections.Hashtable();
foreach (Panel Pnl in pnls)
{
foreach (Control childControl in Pnl.Controls)
{
if (childControl is TextBox)
{
TextBox box = (TextBox)childControl;
//if(box.Text!=null)
//Retrieve and store value from childControl.Text
arrayList.Add(box.Text);
foreach (int i in arrayList)
{
Label l3 = new Label();
l3.Text = (String)arrayList[i];
//Alternatively...
// ht.Add( box.ID, box.Text );
}
}
}
} */
}
}
the one way is to find item is Request.Forms collection
Request.From["controlName"]
This will contain the required value. Note that it work on Name rather than Id so you should provide the Name as well.

strange asp.net dropdownlist selecte value not set

I use the following code to populate my drop down list:
if (ddl_year.Items.Count == 0)
{
int current_year = System.DateTime.Now.Year;
for (int i = 1; i < 11; i++)
{
ListItem li = new ListItem((current_year - i).ToString(),
(current_year - i).ToString());
ddl_year.Items.Add(li);
}
ddl_year.SelectedIndex = 0;
}
then later, I use i set the selected value as follow:
ddl_year.SelectedValue="2010";
The selected value remains 2012; Can someone tell me what i'm doing wrong?
The strange thing is that i do exactly the same thing for an other drop down list which get a selected value of 2009 and that value is shown correctly.
Any comment will be highly appreciated.
Thanks in advance
More details about the actual program:
if (!Page.IsPostback)
{
FillYears(ddl_year_B); // The content of this method is what i desribed ealier in my post.
FillYears(ddl_year_A);
if (Session[CurrentCompareYearWithSessionKey] == null)
Session[CurrentCompareYearWithSessionKey] = Monitor_year - 1;
ddl_year_B.SelectedValue = Session[CurrentCompareYearWithSessionKey].ToString();//Works as expected
if (Session[CurrentSelectedYearSessionKey] == null)
Session[CurrentSelectedYearSessionKey] = Monitor_year;
ddl_year_A.SelectedValue = Session[CurrentSelectedYearSessionKey].ToString();// Does not work as expected
}
And below the aspx markup:
<td > Year:</td>
<td >
<asp:DropDownList ID="ddl_year_A" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_year_A_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td >Compared year:</td>
<td >
<asp:DropDownList Width="80px" ID="ddl_year_B" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddl_year_B_SelectedIndexChanged">
</asp:DropDownList>
</td>
In my case your code is working fine as expected. Pl. tell what is the initial value of Monitor_year ?
I reconstructed your code as follows and it is working perfactly:
if (!Page.IsPostBack)
{
int Monitor_year = 2010;
FillYears(ddl_year_B); // The content of this method is what i desribed ealier in my post.
FillYears(ddl_year_A);
if (Session["CurrentCompareYearWithSessionKey"] == null)
Session["CurrentCompareYearWithSessionKey"] = Monitor_year - 1;
ddl_year_B.SelectedValue = Session["CurrentCompareYearWithSessionKey"].ToString();//Works as expected
if (Session["CurrentSelectedYearSessionKey"] == null)
Session["CurrentSelectedYearSessionKey"] = Monitor_year;
ddl_year_A.SelectedValue = Session["CurrentSelectedYearSessionKey"].ToString();// Does not work as expected
}
//-----------
protected void FillYears(DropDownList ddl_year)
{
if (ddl_year.Items.Count == 0)
{
int current_year = System.DateTime.Now.Year;
for (int i = 1; i < 11; i++)
{
ListItem li = new ListItem((current_year - i).ToString(), (current_year - i).ToString());
ddl_year.Items.Add(li);
}
ddl_year.SelectedIndex = 0;
}
}
//--------------------- aspx page
<asp:DropDownList ID="ddl_year_B" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_year_A" runat="server"></asp:DropDownList>
The value of ddl_year_B and ddl_year_A is 2009 and 2010 respectively
Try this code , it totally work for me .
int yr = DateTime.Now.Year;
for (int y = 0; y < 11; y++)
{
ddlYear.Items.Add(new ListItem((yr - y).ToString()));
}
ddlYear.SelectedValue = "2010";
SelectedValue property of drop down list only returns currently selected value. Which means you can't set any value for this property.(get property only works).try this
ListItem selectedListItem = ddl.Items.FindByValue("2010");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
};

Changing selection for a DropdownList triggers OnSelectedIndexChanged event of a CheckBoxList

Basically I want to implement a filtering option for a Grid view and I have a dropdown list containing the column names and a checkboxlist containing the available filtering values for that column. Every time I select a different column, I have to load the filtering values for that column into the checkbox list.
The problem I have is that when I change the column in the dropdown list, the event for the checklist is fired and the application crashes.
My controls are defined like this:
<div class="LeftAligned">
<asp:Label ID="FilterLabel" runat="server" Text="Filter by:" />
<asp:DropDownList runat="server" ID="FilterReviewsDropDownList" AutoPostBack="true" OnSelectedIndexChanged="FilterReviewsDropDownList_SelectedIndexChanged" />
<asp:ImageButton ID="FilterReviewsButton" runat="server" ImageUrl="~/images/filter.png" AlternateText="VALUE" CssClass="filter_button" OnClick="FilterReviewsButton_Click" />
<div onmouseout="javascript:bMouseOver=false;" onmouseover="javascript:bMouseOver=true;" class="filter_div">
<asp:CheckBoxList AutoPostBack="true" ID="FilterReviewsCheckBoxList" ClientIDMode="Static" runat="server" CssClass="filter_checklist collapsed"
OnSelectedIndexChanged="FilterReviewsCheckBoxList_Selected">
</asp:CheckBoxList>
</div>
<%--asp:Button runat="server" ID="ApplyFilterButton" Text="Apply Filter" OnClick="ApplyFilterButton_Click"/>
<asp:Button runat="server" ID="ClearFilterButton" Text="Clear Filter" OnClick="ClearFilterButton_Click"/--%>
</div>
In the CodeBehind file I have the following code:
protected void FilterReviewsButton_Click(object sender, EventArgs e)
{
FilterReviewsCheckBoxList.CssClass = "filter_checklist";
}
protected void FilterReviewsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
LoadFilterCheckboxes(FilterReviewsDropDownList.SelectedIndex);
}
private void LoadFilterCheckboxes(int iColumn)
{
SortedSet<string> oItems = new SortedSet<string>();
for (int i = 0; i < ReviewsGridView.Rows.Count; i++)
{
IEnumerable<Label> oLabels = ReviewsGridView.Rows[i].Cells[iColumn].Controls.OfType<Label>();
string sValue = "";
if (oLabels != null && oLabels.Count() > 0)
{
sValue = oLabels.First().Text;
}
else
{
sValue = ReviewsGridView.Rows[i].Cells[iColumn].Text;
}
if (!oItems.Contains(sValue))
oItems.Add(sValue);
}
FilterReviewsCheckBoxList.Items.Clear();
FilterReviewsCheckBoxList.Items.Add("All");
FilterReviewsCheckBoxList.Items[0].Selected = true;
foreach (string sItem in oItems)
{
FilterReviewsCheckBoxList.Items.Add(sItem);
FilterReviewsCheckBoxList.Items[FilterReviewsCheckBoxList.Items.Count - 1].Selected = true;
}
}
protected void FilterReviewsCheckBoxList_Selected(object sender, EventArgs e)
{
string sResult = Request.Form["__EVENTTARGET"];
if (string.IsNullOrEmpty(sResult))
{
FilterReviewsCheckBoxList.Items[0].Selected = true; //weird bug fix...
return;
}
string[] sCheckedBox = sResult.Split('$');
//get the index of the item that was checked/unchecked
int i = int.Parse(sCheckedBox[sCheckedBox.Length - 1].Split('_')[1]);
if (i == 0)
{
if (FilterReviewsCheckBoxList.Items[i].Selected == true)
{
for (int j = 1; j < FilterReviewsCheckBoxList.Items.Count; j++)
FilterReviewsCheckBoxList.Items[j].Selected = true;
}
else
{
for (int j = 1; j < FilterReviewsCheckBoxList.Items.Count; j++)
FilterReviewsCheckBoxList.Items[j].Selected = false;
}
}
else
{
if (FilterReviewsCheckBoxList.Items[i].Selected == false)
{
FilterReviewsCheckBoxList.Items[0].Selected = false;
}
else
{
//if (oFirstTable != null)
//{
// oTable = oFirstTable;
// oView = oTable.DefaultView;
//}
bool bAllChecked = true;
for (int j = 1; j < FilterReviewsCheckBoxList.Items.Count; j++)
{
if (FilterReviewsCheckBoxList.Items[j].Selected == false)
{
bAllChecked = false;
break;
}
}
if (bAllChecked)
FilterReviewsCheckBoxList.Items[0].Selected = true;
}
}
}
Any idea of why FilterReviewsCheckBoxList_Selected is called (with the dropdownlist as the sender argument) when changing the dropdown list?
For anyone who may encounter the same problem, the fix is to avoid static binding of the grid view to entity data source. Instead, bind the grid dynamically in the page load event.

Resources