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
I created some textbox and I want to get their value dynamically.
Briefly, ı explain my page:
I have dropDown list has number 1 to 15.When the user select number and I created textbox as selected number.
for example; user select 3 and I create 3 text box and user write something in textbox.
Here is My Code:
aspx Side:
<asp:DropDownList ID="ddlUserSelected" AutoPostBack="true" OnSelectedIndexChanged="ddlUserSelected_SelectedIndexChanged" runat="server">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"/>
Code Behind:
protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
TextBox txtDate = new TextBox();
Label lbl = new Label();
lbl.Text = "<br/>";
txtDate .Width = 70;
txtDate .CssClass = "tbl";
txtDate .ID = "txtDate" + a;
PlaceHolder1.Controls.Add(txtDate);
PlaceHolder1.Controls.Add(lbl);
}
}
And Also I have Save button.
protected void btnSave_Click(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
//I want to get each textbox value
}
}
Note: for loop doesn't matter (can be removed) My main aim is get the text box value.
How to get textbox(es) value in btnSave_Click method?
Below code will help you
protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
TextBox txtDate = new TextBox();
Label lbl = new Label();
lbl.Text = "<br/>";
txtDate.Width = 70;
txtDate.CssClass = "tbl";
txtDate.ID = "txtDate" + a;
PlaceHolder1.Controls.Add(txtDate);
PlaceHolder1.Controls.Add(lbl);
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
if(Request.Form.Get("txtDate" + a.ToString()) != null)
{
var str = Request.Form.Get("txtDate" + a.ToString());
}
}
}
If you are used master page then use below code
if (Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString()) != null)
{
var str = Request.Form.Get("ctl00$ContentPlaceHolder1$txtDate" + a.ToString());
}
protected void btnSave_Click(object sender, EventArgs e)
{
for (int a = 1; a <= int.Parse(ddlUserSelected.SelectedItem.Text); a++)
{
string value = Request.Form["txtDate" + a];
}
}
Problem
If you dynamically add controls to a page, you need to reloaded them on Page Init or Page Load.
Otherwise, you won't be able to find them when you post back.
ASPX
<asp:DropDownList ID="ddlUserSelected" AutoPostBack="true"
OnSelectedIndexChanged="ddlUserSelected_SelectedIndexChanged"
runat="server">
<asp:ListItem Text="Select one" />
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<asp:ListItem Text="4" />
<asp:ListItem Text="5" />
</asp:DropDownList>
<br/>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"/>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
Code Behind
private int Total
{
get
{
int total;
if (Int32.TryParse(ddlUserSelected.SelectedItem.Text, out total))
return total;
return 0;
}
}
protected void Page_Load(object sender, EventArgs e)
{
CreateTextBoxes(Total);
}
protected void ddlUserSelected_SelectedIndexChanged(object sender, EventArgs e)
{
CreateTextBoxes(Total);
}
protected void btnSave_Click(object sender, EventArgs e)
{
int total = Total;
for (int a = 1; a <= total; a++)
{
var textbox = PlaceHolder1.FindControl("txtDate" + a) as TextBox;
}
}
private void CreateTextBoxes(int total)
{
for (int a = 1; a <= total; a++)
{
// Make sure we do not add same ID again
if (PlaceHolder1.FindControl("txtDate" + a) == null)
{
TextBox txtDate = new TextBox();
Label lbl = new Label();
lbl.Text = "<br/>";
txtDate.Width = 70;
txtDate.CssClass = "tbl";
txtDate.ID = "txtDate" + a;
PlaceHolder1.Controls.Add(txtDate);
PlaceHolder1.Controls.Add(lbl);
}
}
}
I'm trying to use an ajax panel to add keep multiple images added to table cells dynamically. Thing is when I add the second image, the first one dissapears.
Its really just a silly example to try and get ajax controls working for another project.
I'm putting an image of Bill Gates in row 3, column 3 and an image of Steve Jobs in row 1, column 5. I have a button to place each image.
I can't seem to get both to display at the same time.
I have written a function to generate the cell id (GenerateTableCellID), as I've been told I would need to to this. Also there is a function to extract the cell and row in a tuple (GetColumnAndRow).
I'm not sure how to use a Session object to save the data. I thought using AJAX would be the answer, though I think I'm missing a major aspect of it.
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div id="tablePlaceHolder" runat="server"></div>
<asp:Button ID="tblButton2" runat="server" Text="Add Steve Jobs" OnClick="tblButton_Click_Jobs" />
<asp:Button ID="tblButton" runat="server" Text="Add Bill Gates" OnClick="tblButton_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
int tableSize = 5;
var t = new HtmlTable();
t.ID = "myTable";
var placeHolderURL = "http://wiki.tripwireinteractive.com/images/4/47/Placeholder.png";
for (int r = 0; r < tableSize; r++)
{
var tableRow = new HtmlTableRow();
tableRow.ID = "row_" + r.ToString();
for (int c = 0; c < tableSize; c++)
{
var tableCell = new HtmlTableCell();
var id = GenerateTableCellID(r, c);
tableCell.ID = id;
tableCell.Height = "20";
tableCell.Width = "20";
tableCell.InnerHtml = string.Format("<img src='{0}' width='20' height='20' />", placeHolderURL);
tableRow.Controls.Add(tableCell);
}
t.Controls.Add(tableRow);
}
tablePlaceHolder.Controls.Add(t);
}
protected void tblButton_Click(object sender, EventArgs e)
{
int c =2;
int r = 2;
var id = GenerateTableCellID(c, r);
var image = GenerateImage("http://www.mnn.com/sites/default/files/billgates.jpg");
var cell = (HtmlTableCell)UpdatePanel2.FindControl(id);
cell.InnerHtml = "";
cell.Controls.Add(image);
}
protected void tblButton_Click_Jobs(object sender, EventArgs e)
{
int c = 4;
int r = 0;
var id = GenerateTableCellID(c, r);
var image = GenerateImage("http://images.boomsbeat.com/data/images/full/209/jobs-jpg.jpg");
var cell = (HtmlTableCell)UpdatePanel2.FindControl(id);
cell.InnerHtml = "";
cell.Controls.Add(image);
}
protected Image GenerateImage(string url)
{
var image = new Image();
image.ImageUrl = url;
image.Width = 20;
image.Height = 20;
return image;
}
protected string GenerateTableCellID(int c, int r)
{
return "column_" + c.ToString() + "_row_" + r.ToString();
}
protected Tuple<int, int> GetColumnAndRow(string tableCellID)
{
string[] splitString = tableCellID.Split('_');
int column, row;
if (Int32.TryParse(splitString[1], out column) && Int32.TryParse(splitString[3], out row))
{
return new Tuple<int, int>(column, row);
}
else
{
return null;
}
}
It is because at every update you clear the html present before by cell.InnerHtml = ""; remove this and try
protected void tblButton_Click_Jobs(object sender, EventArgs e)
{
int c = 4;
int r = 0;
var id = GenerateTableCellID(c, r);
var image = GenerateImage("http://images.boomsbeat.com/data/images/full/209/jobs-jpg.jpg");
var cell = (HtmlTableCell)UpdatePanel2.FindControl(id);
//cell.InnerHtml = "";
cell.Controls.Add(image);
}
As mentioned on the Page LifyCycle your table is created everytime when you do reload page (does not matter is this postback or not). Also you could read this post. In other words, it is not proper way store your data in the dynamic generated controls, because you lose your data on page load.
But if it is necessary for you could use AJAX methods ($.get and $.post, not UpdatePanel) to get data from the backend and add this to generated control on the client side
I have list of hyperlinks which i want to use to create a 2x* table (* is number of hyperlinks)
Here is my code...
for (int rows = 0; rows < hlist.Count; rows++) //Create rows for the number of hyperlinks, so i will always have a spare row.
{
TableRow row = new TableRow(); // Create the new rows
table.Rows.Add(row); //Add rows to the table
for (int cells = 0; cells < 2; cells++)
{
TableCell cell = new TableCell();
for(int h = 0; h < hlist.Count; h++)
cell.Controls.Add(hlist[h]);
row.Cells.Add(cell);
}
}
All this does is list all my hyperlinks in a single column table, with a new row for each hyperlink!
Any help would be appreciated!!
Thanks
Assuming that you want to create a table that shows two hyperlinks per row, you could try the following code:
for (int i = 0; i < hlist.Count; i += 2)
{
TableRow row = new TableRow(); // Create the new rows
table.Rows.Add(row);
for (int j = i; j < Math.Min(i + 2, hlist.Count); j++)
{
TableCell cell = new TableCell();
cell.Controls.Add(hlist[j]);
row.Controls.Add(cell);
}
}
However, using dynamically added controls in ASP.NET is complex if you want them to react on events. So I'd propose to check whether you could change your approach so that you can use a Repeater instead. In order to do so, you'd first have to change your data model, e.g. to a list of Pair objects that contain two URLs, e.g.:
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
IEnumerable<Uri> uris = GetUris();
List<Tuple<Uri, Uri>> pairs = new List<Tuple<Uri, Uri>>();
for (int i = 0; i < uris.Count; i += 2)
{
var uri1 = uris[i];
var uri2 = i + 1 < uris.Count ? uris[i + 1] : null;
pairs.Add(new Tuple<Uri, Uri>(uri1, uri2));
}
rpt.DataSource = pairs;
rpt.DataBind();
}
}
If your URLs are not compatible with a Uri (maybe they contain a leading ~), you can also use strings instead of Uri.
The markup for your the Repeater would look similar to this:
<asp:Repeater ID="rpt" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:HyperLink runat="server" Text="Link 1"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Item1") %>' />
</td>
<td>
<asp:HyperLink runat="server" Text="Link 1"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Item2") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I have a user control (.ascx) added to my application:
<uc1:pomedsrow runat="server" id="POMedsRow" />
And here is html and logic
<asp:Panel ID="Panel1" runat="server">
How many PO Meds do you wish to order?
<asp:TextBox ID="txtReqPONum" runat="server" />
<asp:LinkButton ID="lbnAddPOMeds" runat="server" Text="Go"
OnClick="lbnAddPOMeds_Click"/>
</asp:Panel>
<asp:Panel ID="pnlPOMeds" Visible="false" runat="server">
<table border="1">
<tr>
<td><p>PO Meds</p></td>
<td><p>Min/Max</p></td>
<td><p>Amount to Order</p></td>
</tr>
<uc1:pomedsrow runat="server" id="POMedsRow" />
</table>
<br />
</asp:Panel>
protected void lbnAddPOMeds_Click(object sender, EventArgs e)
{
int ReqPO = Convert.ToInt32(txtReqPONum.Text);
int n = ReqPO;
for (int i = 0; i < n; i++)
{
Control pomedsrow = new Control();
//Assigning the textbox ID name
pomedsrow.ID = "txtPOAmount" + "" + ViewState["num"] + i;
this.Form.Controls.Add(pomedsrow);
}
}
But when I click the link button nothing happens. Am I not calling the custom control correctly?
You are not adding your control properly. Try this:
protected void lbnAddPOMeds_Click(object sender, EventArgs e)
{
TextBox txtReqPONum = (TextBox) Panel1.FindControl("txtReqPONum");
int ReqPO = 0;
if (txtReqPONum != null && int.TryParse(txtReqPONum.Text, out ReqPO) )
{
int n = ReqPO;
for (int i = 0; i < n; i++)
{
UserControl myControl = (UserControl)Page.LoadControl("~/pomedsrow.ascx");//(UserControl)Page.LoadControl("Your control path/pomedsrow.ascx");
//Assigning the textbox ID name
myControl.ID = "txtPOAmount" + "" + ViewState["num"] + i;
Panel1.Controls.Add(myControl);
}
}
}
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.