I've got the following code:
HtmlGenericControl li = new HtmlGenericControl("li");
ULRouting.Controls.Add(li);
HtmlGenericControl anchor = new HtmlGenericControl("a");
li.Attributes.Add("myCustomIDAtribute", "11");
anchor.InnerText = "TabX";
li.Controls.Add(anchor);
I'm trying to find out how you add a dropdown box to the li control when you dynamically create the li please ie:
<asp:DropDownList ID="cmbRoutingStage_1" runat="server" DataSourceID="sqlDBStages" DataTextField="stages" DataValueField="StageRefID"></asp:DropDownList>
Try this:-
DropDownList ddl = new DropDownList();
ddl.ID = "cmbRoutingStage_1";
ddl.DataSourceID = "sqlDBStages";
ddl.DataTextField = "stages";
ddl.DataValueField = "StageRefID";
li.Controls.Add(ddl);
As a side note you can use HtmlAnchor class for generating the anchor tag:-
HtmlAnchor anchor = new HtmlAnchor();
Related
I have this line in my .aspx code:
<div id="view" class="g2" runat="server">
</div>
but when I want to find this element in C#(aspx.cs) and send data for it, C# cant find it this way:
view.Controls.Add(item);
*item is a html element that I made through this code:
for(int i=0; i<foods.Count; i++)
{
HtmlGenericControl item = new HtmlGenericControl("div");
item.Attributes["class"] = "items";
item.Style["border_bottom"] = "3px solid aqua";
HtmlImage img = new HtmlImage();
img.Src = foods[i].picGet();
HtmlGenericControl mask = new HtmlGenericControl("div");
item.Attributes["class"] = "mask";
HtmlGenericControl h2 = new HtmlGenericControl("h2");
item.InnerHtml = foods[i].fnameGet();
HtmlGenericControl price = new HtmlGenericControl("span");
item.Attributes["class"] = "price";
item.InnerHtml = foods[i].priceGet().ToString() + "ریال";
HtmlGenericControl desc = new HtmlGenericControl("p");
item.InnerHtml = foods[i].describeGet();
mask.Controls.Add(h2);
mask.Controls.Add(price);
mask.Controls.Add(desc);
item.Controls.Add(img);
item.Controls.Add(mask);
view.Control.Add(item);
}
I have written the namespace System.Web.UI.HtmlControls
I saw a course that did the same thing and was successful
the error is that even my visual studio cant find the view that i used in the last line of the for loop.
You can use Control.FindControl to search control with the specified id parameter as follow:
Control view = FindControl("view");
//Then add your item to div
view.Controls.Add(item);
I've created a table row dynamically using one function as shown here.
Table The_Table = Invoice_Table;
TableRow new_Item_Row = new TableRow();
The_Table.Rows.Add(new_Item_Row);
TableCell new_type = new TableCell();
TableCell new_item = new TableCell();
TableCell new_amount = new TableCell();
DropDownList type_List = new DropDownList();
type_List.ID = "type_List";
ListItem Cash = new ListItem();
Cash.Value="Cash";
Cash.Text="Cash/Cheque";
ListItem IVoi = new ListItem();
IVoi.Value="IVoi";
IVoi.Text="Invoice";
type_List.Items.Add(Cash);
type_List.Items.Add(IVoi);
TextBox item_Text = new TextBox();
item_Text.ID = "item_Text";
TextBox amount_Text = new TextBox();
amount_Text.ID = "amount_Text";
new_type.Controls.Add(type_List);
new_item.Controls.Add(item_Text);
new_amount.Controls.Add(amount_Text);
new_Item_Row.Cells.Add(new_type);
new_Item_Row.Cells.Add(new_item);
new_Item_Row.Cells.Add(new_amount);
I then try to access this control later using the following in a different function
DropDownList type_L = Invoice_Table.FindControl("type_List") as DropDownList;
TextBox amount_p = Invoice_Table.FindControl("amount_Text") as TextBox;
TextBox Item_T = Invoice_Table.FindControl("item_Text") as TextBox;
but it is returning
"Object reference not set to an instance of an object."
I assume it is because it can't find the control? But I'm not sure how to fix it.
Sorry for the messiness of adding new cells... I'm new to this and I am not aware of any better way to do this.
The problem is that FindControl is not recursive. You have to call it on the TableCell instance and not the Table itself.
TextBox amount_p = new_amount.FindControl("amount_Text") as TextBox;
I'm using a javascript menu from dynamicdrive
I'd tried to get the menu items from a database.
my aspx file contain the menu and it works fine when it's a static menu.
my table or my query actually produces:
IdPage int, PageTitle varchar(20), PageFileUrl varchar(30), ParentIdPage int
My methods to get data
DataRow[] dataRowParent = _dataTable.Select("[ParentIdPage]=" + 0);
foreach (DataRow dr in dataRowParent)
{
HtmlGenericControl li = new HtmlGenericControl("li");
// add <a>
HtmlGenericControl hlink = new HtmlGenericControl("a");
if (dr["PageFileUrl"].ToString() == "") // this item has a submenu.
{
li.Attributes.Add("rel", "ddsubmenu" + dr["IdPage"].ToString());
hlink.Attributes.Add("href", "#");// link should be # when no direct link
hlink.InnerText = dr["PageTitle"].ToString();
li.Controls.Add(hlink);
ulTopMenu.Controls.Add(li);
AddNewUl((int)dr["IdPage"]);
AddSubmenuItems(_dataTable, (int)dr["IdPage"]);
}
else // Direct link ,no submenu
{
hlink.Attributes.Add("href", dr["PageFileUrl"].ToString());
hlink.InnerText = dr["PageTitle"].ToString();
li.Controls.Add(hlink);
ulTopMenu.Controls.Add(li);
}
}
}
private void AddSubmenuItems(DataTable dataTable, int menuId)
{
// create related sub menu
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "ParentIdPage=" + menuId;
foreach (DataRowView subMenuItem in dataView)
{
// find related <ul>
HtmlControl ulControl = (HtmlControl)FindControl("ddsubmenu" + menuId);
// Add new <li><a href="PageFileUrl.aspx" >page title</a> </li>
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlGenericControl hlink = new HtmlGenericControl("a");
hlink.Attributes.Add("href", subMenuItem["PageFileUrl"].ToString());
hlink.InnerText = subMenuItem["PageTitle"].ToString();
li.Controls.Add(hlink);
li.InnerText = subMenuItem["PageTitle"].ToString();
li.Attributes.Add("href", subMenuItem["PageFileUrl"].ToString());
ulControl.Controls.Add(li);
}
}
private void AddNewUl(int menuId)
{
// Add new <ul id="ddsubmenu00" class= "ddsubmenustyle">
HtmlGenericControl newUl = new HtmlGenericControl("ul");
// Set the attributes of the new ul.
newUl.ID = "ddsubmenu" + menuId;
newUl.Attributes.Add("class", "ddsubmenustyle");
placeHolder1.Controls.Add(newUl);
}
My problem is that submenu doesn't appear!, what is wrong?
any help appreciated.
Thanks for all, finally I figured it out:
Replace this line
li.Attributes.Add("rel",""+ "ddsubmenu" + dr["IdPage"].ToString()+"");
with this one:
hlink.Attributes.Add("rel",""+ "ddsubmenu" + dr["IdPage"].ToString()+"");
Now it works properly :-)
I am creating an application where I require to add dynamic checkbox list. Please anyone tell me how to add dynamic checkbox list using C#.
Put a placeHolder on your form with the ID placeHolder and add the following code to your Page_Load():
CheckBoxList cbList = new CheckBoxList();
for (int i = 0; i < 10; i++)
cbList.Items.Add(new ListItem("Checkbox " + i.ToString(), i.ToString()));
placeHolder.Controls.Add(cbList);
This will add 10 CheckBox objects within your CheckBoxList(cbList).
Use the following code to examine each CheckBox object within the CheckBoxList
foreach(ListItem li in cbList.Items)
{
var value = li.Value;
var text = li.Text;
bool isChecked = li.Selected;
}
The placeholder is used to add the CheckBoxList to the form at runtime, using a placeholder will give you more control over the web page where the CheckBoxList and its items will appear.
Here is an example
CheckBoxList chkList = new CheckBoxList();
CheckBox chk = new CheckBox();
chkList.ID = "ChkUser";
chkList.AutoPostBack = true;
chkList.RepeatColumns = 6;
chkList.DataSource = us.GetUserDS();
chkList.DataTextField = "User_Name";
chkList.DataValueField = "User_Id";
chkList.DataBind();
Panel pUser = new Panel();
if (pUserGrp != "")
{
pUser.GroupingText = pUserGrp ;
chk.Text = pUserGrp;
}
else
{
pUser.GroupingText = "Non Assigned Group";
chk.Text = "Non Assigned group";
}
pUser.Controls.Add(chk);
pUser.Controls.Add(chkList);
this.Form.Controls.Add(pUser);
At code behind you can create new ASP.NET Controls and you can add these controls to your page. All you need to do is to create new CheckBoxList Object and add ListItems to it. Finally, you need to add your CheckBoxList to your Page.
// Create CheckBoxList
CheckBoxList list= new CheckBoxList();
// Set attributes for CheckBoxList
list.ID = "CheckBoxList1";
list.AutoPostBack = true;
// Create ListItem
ListItem listItem = new ListItem();
// Set attributes for ListItem
listItem .ID = "ListItem1";
// Add ListItem to CheckBoxList
list.Items.Add(listItem );
// Add your new control to page
this.Form.Controls.Add(list);
I have a HtmlGenericController and to this I want to add RadioButtons. My radiobuttons come from a RadioButtonList an hence the objects are Listitems. How do I get my genericcontroller to show the radiobuttons?
This is my code
private HtmlGenericControl generateCells(String domainName)
{
HtmlGenericControl container = new HtmlGenericControl("div");
HtmlGenericControl dName = new HtmlGenericControl("span");
dName.InnerHtml = domainName;
RadioButtonList radioList = new RadioButtonList();
radioList.ID = "radio_" + domainName;
radioList.RepeatDirection = RepeatDirection.Horizontal;
ListItem sunriseA = new ListItem();
sunriseA.Value = Price_Types.SUNRISE_ONE.ToString();
//sunriseA.Text = Price_Types.SUNRISE_ONE.ToString();
sunriseA.Text = "";
radioList.Items.Add(sunriseA);
ListItem sunriseB = new ListItem();
sunriseB.Value = Price_Types.SUNRISE_TWO.ToString();
//sunriseB.Text = Price_Types.SUNRISE_TWO.ToString();
sunriseB.Text = "";
radioList.Items.Add(sunriseB);
ListItem landrush = new ListItem();
landrush.Value = Price_Types.LANDRUSH.ToString();
//landrush.Text = Price_Types.LANDRUSH.ToString();
landrush.Text = "";
radioList.Items.Add(landrush);
ListItem general = new ListItem();
general.Value = Price_Types.GENERAL.ToString();
//general.Text = Price_Types.GENERAL.ToString();
general.Text = "";
radioList.Items.Add(general);
container.Controls.Add(dName);
foreach (ListItem item in radioList.Items)
{
HtmlGenericControl span = new HtmlGenericControl("span");
span.InnerHtml = item;//what to put here??
container.Controls.Add(span);
}
return container;
}
var radioButton = new HtmlGenericControl("input");
radioButton.Attributes["type"] = "radio";
radioButton.Attributes["name"] = "groupName";
radioButton.Attributes["value"] = "buttonValue";
That will only render the round radiobutton itself, though. To add a label, you'll have to render for example a span besides it. Or, IIRC, render a label field with the for attribute set to the ID of the radiobutton, so clicking the label will automatically click the button too.