I'm dynamically creating buttons with values from a database, and I have a modalpopup using ajax. I know that when using the popup I have to set a targetid, that's fine when its a predefined button. But how do you get the modalpopup to recognize a target when it dynamically created without having to use stringbuilder and rebuild a modalpopup on an eventhandler, and if I'm left to using stringbuilder then how do I call that when its created?
This is the code for the buttons
button.ID = Convert.ToString(myID);
button.Text = myStr;
button.PostBackUrl = "~/WebForm2.aspx?SubjectID=" + myID;
button.CssClass = "DynamicButtonOverlay";
This is the modalpopup
<asp:Panel ID="MyPanel" runat="server" CssClass="myModalPopup">
<table style="width:100%;">
<tr>
<td>Select News</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddNewsToDelete" runat="server" CssClass="form-control"></asp:DropDownList>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<asp:Button ID="btnDeleteNews" runat="server" Text="Radera" CssClass="btn btn-danger pull-left" OnClick="btnDeleteNews_Click" CausesValidation="False" />
<asp:Button ID="btnClose" runat="server" Text="Close" class="btn btn-success pull-right" />
</td>
</tr>
</table>
</asp:Panel>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="MyPanel" BackgroundCssClass="myModalBackground" TargetControlID="Submit1" OkControlID="btnClose"></asp:ModalPopupExtender>
This is the code generating the buttons..
test.GetSubjects();
int subjectid = 0;
// Current row count.
int rowCtr;// = 0;
// Total number of cells per row (columns).
int cellCtr;
// Current cell counter.
int cellCnt;
//count number of rows in dataset
int rN = test.dsSubjects.Tables[0].Rows.Count;
cellCnt = 4;
for (rowCtr = 1; rowCtr <= rN; rowCtr++)
{
// Create a new row and add it to the table.
TableRow tRow = new TableRow();
Table1.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= 4; cellCtr++)
{
//
Button button = new Button();
//
HyperLink link = new HyperLink();
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
/* If the rowcounter is equal to the record numbers
* then it has to break because if not it will throw an error
* saying that there is no row at ending position */
if (rowCtr == rN)
break;
string myStr = test.dsSubjects.Tables[0].Rows[rowCtr - 1]["SubjectName"].ToString();
int myID = Convert.ToInt32(test.dsSubjects.Tables[0].Rows[rowCtr - 1]["SubjectID"].ToString());
button.ID = Convert.ToString(myID);
button.Text = myStr;
button.PostBackUrl = "~/WebForm2.aspx?SubjectID=" + myID;
button.CssClass = "DynamicButtonOverlay";
tCell.Controls.Add(button);
tCell.CssClass = "DynamicButtonOverlay";
tRow.Cells.Add(tCell);
rowCtr++;
/* If the cellcount is 3 then it needs to break, if not then
* you'll miss every 4rth record, don't know why. But this works */
if (cellCtr == 4)
{
rowCtr = rowCtr - 1;
break;
}
}
}
Related
I have written below lines of code
public void UpdatePageLables(int aPageCount)
{
PageCount = (int)Math.Ceiling((decimal)aPageCount / PageSize);
int recordCount = PageCount;
if (PageSizeChanged != null)
{
HiddenField hd = new HiddenField();
int current;
current = PageIndex;
int pre;
int Next;
double dblPageCount = (double)((decimal)recordCount / decimal.Parse(lstPageSize.SelectedValue));
int pageCount = PageCount;
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
// pages.Add(new ListItem("First", "1", PageIndex > 1));
current = PageIndex;
pre = --PageIndex;
PageIndex = current;
// pages.Add(new ListItem("Previous", pre.ToString(), PageIndex > 1));
for (int i = 1; i <= aPageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != PageIndex));
}
int currentPage = PageIndex;
Next = ++PageIndex;
PageIndex = currentPage;
//pages.Add(new ListItem("Next", Next.ToString(), PageIndex < pageCount));
// pages.Add(new ListItem("Last", pageCount.ToString(), PageIndex < pageCount));
hd.Value = (pre.ToString());
}
rptPager.DataSource = pages;
rptPager.DataBind();
}
}
And in ascx file
<div class="pagination">
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" OnClick="imgPre_Click"
data-rel="tooltip" data-original-title="previous page.">«</asp:LinkButton>
<asp:Repeater ID="rptPager" OnItemDataBound="rptPager_ItemDataBound" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" OnClick="imgnext_Click"
data-rel="tooltip" data-original-title="next page."> »
</asp:LinkButton>
<div class="page-size"><span>Page Size: </span>
<asp:DropDownList runat="server" ID="lstPageSize" AutoPostBack="true" style="float:right"
OnSelectedIndexChanged="lstPageSize_SelectedIndexChanged">
<asp:ListItem Text="5" Value="5"></asp:ListItem>
<asp:ListItem Text="10" Value="10"></asp:ListItem>
<asp:ListItem Text="15" Value="15" Selected="True"></asp:ListItem>
<asp:ListItem Text="25" Value="25" ></asp:ListItem>
<asp:ListItem Text="50" Value="50"></asp:ListItem>
<asp:ListItem Text="75" Value="75"></asp:ListItem>
<asp:ListItem Text="100" Value="100"></asp:ListItem>
<asp:ListItem Text="150" Value="150"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
Now I want to display only first seven page number links when there will be hundred page number links and rest of page number links should not be displayed.
When user will click on ... button then page numbers from 8 to 14 should get displayed and 1 to 7 should be hidden and so on.
Please help me !!!
You should only fill your list of pages with the pages you want. So if you want 7 pages (3 left to selected index and 3 right to selected index) you can do:
for (int i = PageIndex - 3; i <= PageIndex + 3; i++)
{
if (i > 0 && i <= aPageCount) {
{
//i is still inside total range of pages, so page can be added
pages.Add(new ListItem(i.ToString(), i.ToString(), i != PageIndex));
}
}
If you want other list of pages (like 7 next after PageIndex) you need to change the for loop.
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);
}
}
}
I am trying to load models and set every model in a separate, <td> when I reach 5 <td> I want to automatically start a new <tr> but it isn't really working with an if statement. Is it possible or should I go for an entire different approach?
Here is my code:
<tr>
#for (int i = 0; i < Model.Count; i++)
{
<td>
<img width="200px" src="#Url.Action("GetImage", "Beheer", new { productId = Model[i].product_id }) " alt="pasfoto"/><br />
<center><b>#Html.ActionLink(Model[i].naam, "PrProduct", "Categorie", new { id = Model[i].product_id }, null)</b></center>
<center>€ #Html.Label(Model[i].prijs)</center>
</td>
}
</tr>
Use the modulo operator %. Each time i%5 == 0, print a row around the td
#for (int i = 0; i < Model.Count; i++)
{
#if(i%5 == 0)
{
<tr>
}
<td>
<img width="200px" src="#Url.Action("GetImage", "Beheer", new { productId = Model[i].product_id }) " alt="pasfoto"/><br />
<center><b>#Html.ActionLink(Model[i].naam, "PrProduct", "Categorie", new { id = Model[i].product_id }, null)</b></center>
<center>€ #Html.Label(Model[i].prijs)</center>
</td>
#if(i%5 == 0)
{
</tr>
}
}
So you're going to have a table row when i is 0, 5, 10, 15, etc.
#for (int i = 0; i < Model.Count; i++)
{
#if(i%5 == 0)
{
<tr></tr>
}
<td>
<img width="200px" src="#Url.Action("GetImage", "Beheer", new { productId = Model[i].product_id }) " alt="pasfoto"/><br />
<center><b>#Html.ActionLink(Model[i].naam, "PrProduct", "Categorie", new { id = Model[i].product_id }, null)</b></center>
<center>€ #Html.Label(Model[i].prijs)</center>
</td>
}
The above code worked for me. Is it not much different from the answer Havelock gave, but it will not close the brackets in the above code if you do not close the html tags within the same if statement. This way may not look as good if you use borders because I think it will display one empty row? But, for me this worked fine:)
I have a DataList on ym web page, from which a user can choose a certain option within the DataList row.
I use the ItemCommand of DataList for this. Actually, I want to highlight the selected row when the user clicks on the item in the row.
<ItemTemplate>
<tr>
<td style="text-align:center"><asp:LinkButton ID="Item" Text='<%#Eval("Item")%>' CommandName="select" runat="server" /> <br /></td>
<td style="text-align:center"><asp:Label ID="lbQuery" Text='<%#Eval("Query")%>' runat="server" /><br /> </td>
</tr>
</ItemTemplate>
As shown above, the user can click on the LinkButton to choose an item. How do I highlight the corresponding row or only the cell?
Use following Method for Datalist to highlight selected row:
protected void DataList1_ItemDataBound(object sender,
DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
//Add eventhandlers for highlighting
//a DataListItem when the mouse hovers over it.
e.Item.Attributes.Add("onmouseover",
"this.oldClass = this.className;" +
" this.className = 'EntryLineHover'");
e.Item.Attributes.Add("onmouseout",
"this.className = this.oldClass;");
//Add eventhandler for simulating
//a click on the 'SelectButton'
e.Item.Attributes.Add("onclick",
this.Page.ClientScript.GetPostBackEventReference(
e.Item.Controls[1], string.Empty));
}
}
in your RowDataBound event add like this.
// Get the linklabel object and check for non nullity
LinkLabel lblItem = e.Item.FindControl("Item") as LinkLabel
if(lblitem !=null)
{
// add properties to it
lblItem.Attributes.Add("onclick", "this.style.background='#eeff00'");
}
on Item command
string[] str = e.CommandArgument.ToString().Split(';');
int index = Convert.ToInt32(str[2]); // ur item selected index
DataListItemCollection xx = DataList1.Items;
int count = 0;
foreach (DataListItem x in xx)
{
if (count == index)
{
(x.FindControl("Item") as LinkButton).BorderColor = System.Drawing.Color.Red;
(x.FindControl("Item") as LinkButton).BorderWidth = 1;
}
else
{
(x.FindControl("Item") as LinkButton).BorderColor = System.Drawing.Color.White;
(x.FindControl("Item") as LinkButton).BorderWidth = 0;
}
count = count + 1;
}