how to create dynamic textbox,dropdownlist in asp.net - asp.net

I want to create dynamic 2 textbox and 2 dropdownlist below each textbox and dropdownlist in button click. This is the code i am using which creates dynamic textbox and dropdownlist but its creating control in horizontal way.I want creation of dynamic controls in a vertical manner which will be coming under textbox and dropdown.
<div id="dvContainer" runat="server">
<table align="center" cellpadding="0" cellspacing="0" width="90%">
<tr>
<td>
<table width="100%">
<tr>
<td align="center">Date</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="150px"> </asp:TextBox>
</td>
<td align="center">Time</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="150px"></asp:TextBox>
</td>
<td align="center">Godown</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Width="150px">
</asp:DropDownList>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><table width="100%">
<tr>
<td align="center">Item</td>
<td align="center">QTY</td>
<td align="center">Unit</td>
<td align="center">Available</td>
<td align="center"></td>
</tr>
<tr>
<td align="center">
<asp:DropDownList ID="DropDownList2" runat="server" Width="150px">
</asp:DropDownList>
</td>
<td align="center">
<asp:TextBox ID="TextBox3" runat="server" Width="150px"></asp:TextBox>
</td>
<td align="center">
<asp:DropDownList ID="DropDownList3" runat="server" Width="150px">
</asp:DropDownList>
</td>
<td align="center">
<asp:TextBox ID="TextBox4" runat="server" Width="150px"></asp:TextBox>
</td>
<td align="center">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
</tr>
</table></td>
</tr>
</table>
</div>
This is the code i am using in cs page
public partial class StockEntry : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Ddl_Item;
protected System.Web.UI.WebControls.TextBox Txt_Quantity;
protected System.Web.UI.WebControls.DropDownList Ddl_Unit;
protected System.Web.UI.WebControls.TextBox Txt_Available;
protected void Page_Load(object sender, EventArgs e)
{
}
int countTimes = 0;
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["countTimes"] == null)
{
countTimes = 1;
}
else
{
countTimes = Convert.ToInt32(ViewState["countTimes"]);
}
for (int i = 0; i < countTimes; i++)
{
Ddl_Item = new DropDownList();
Ddl_Item.ID = "Ddl_Item" + i;
Ddl_Item.Width = 180 + i;
Txt_Quantity = new TextBox();
Txt_Quantity.ID = "Txt_Quantity" + i;
Txt_Quantity.Width = 180 + i;
Ddl_Unit = new DropDownList();
Ddl_Unit.ID = "Ddl_Unit" + i;
Ddl_Unit.Width = 180 + i;
Txt_Available = new TextBox();
Txt_Available.ID = "Txt_Available" + i;
Txt_Available.Width = 180 + i;
dvContainer.Controls.Add(Ddl_Item);
dvContainer.Controls.Add(Txt_Quantity);
dvContainer.Controls.Add(Ddl_Unit);
dvContainer.Controls.Add(Txt_Available);
}
countTimes = countTimes + 1;
ViewState.Add("countTimes", countTimes);
}
}

You are adding your controls in the div, not in the table and next to each other. To add some html structure around controls, you can create literal object with your html in its text, and add them before/after your controls (for example you can add <br/> )

I think what you want is HtmlGenericControl http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx
This will allow you to add the table that you require to set your layout, you could of course wrap each control in a div and control your layout using CSS.

Related

asp.net- Highlight cell in listview

I have this piece of code which works fine and highlights the label. However, I want it to highlight the entire cell not just the label.
Any help would be appreciated!
protected void HighLight_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label TotalTimeLabel = (Label)e.Item.FindControl("TotalTime");
if (TotalTimeLabel != null)
{
Decimal Total;
if (Decimal.TryParse(TotalTimeLabel.Text, out Total) == true)
{
if (Total > (Decimal)1.5)
{
TotalTimeLabel.BackColor = System.Drawing.Color.Red;
TotalTimeLabel.ForeColor = System.Drawing.Color.Black;
}
}
}
}
}
The code for the table is below
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="HighLight_ItemDataBound" >
<LayoutTemplate>
<table cellpadding="1" class="TableCSS" runat="server" id="tblProducts">
<tr runat="server" style="background-color:lightgrey">
<th runat="server">enNotificationNoNI</th>
<th runat="server">TotalTime</th>
<th runat="server">TPTIMEIN</th>
<th runat="server">Status</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" class="TableData">
<td>
<asp:Label ID="enNotificationNoNI" runat="Server" Text='<%#Eval("enNotificationNoNI") %>' />
</td>
<td>
<asp:Label ID="TotalTime" runat="Server" Text='<%#Eval("TotalTime") %>' />
</td>
<td>
<asp:Label ID="TPTIMEIN" runat="Server" Text='<%#Eval("TPTIMEIN") %>' />
</td>
<td>
<asp:Label ID="Status" runat="Server" Text='<%#Eval("Status") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Try this code
For entire row
Control ctrl = TotalTimeLabel.Parent.Parent;
HtmlTableRow tblrw = (HtmlTableRow)ctrl;
tblrw.BgColor = System.Drawing.Color.Red.Name;
For one cell
Control ctrl = TotalTimeLabel.Parent;
HtmlTableCell tblcl = (HtmlTableCell)ctrl;
tblcl.BgColor = System.Drawing.Color.Red.Name;
let me know whether this works :)

How to get values from templatefields in Gridview

I have a gridview with template fields.
I want when I press the imagebutton to get the values of the Labels inside the ItemTemplate.
<asp:GridView ID="GridView1" ShowHeader="False"
GridLines="None" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table style="border: 0px solid">
<tr>
<td style="width: 220px">
<asp:Image ID="imgEmployee" BorderStyle="Solid" Height="182px" Width="235px"
ImageUrl='<%# Eval("Photo")%>'
runat="server" />
</td>
<td style="width: 600px">
<table>
<tr>
<td>
<b>ID:</b>
</td>
<td>
<asp:Label ID="lblId"
runat="server"
Text='<%#Eval("id")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Item:</b>
</td>
<td>
<asp:Label ID="lblItem"
runat="server"
Text='<%#Eval("Item")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Price:</b>
</td>
<td>
<asp:Label ID="lblPrice"
runat="server"
Text='<%#Eval("Price")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Notes:</b>
</td>
<td>
<asp:Label ID="lblNotes"
runat="server"
Text='<%#Eval("Notes")%>'>
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text ="1" ></asp:TextBox>
</td>
<td>
<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/buy.png" CommandName="AddToBasket" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridView1_RowCommand I have the following code
If e.CommandName = "AddToBasket" Then
Here i want to take the values of the labels
You need to attach OnRowCommand event method to the GridView first:
<asp:GridView ID="GridView1" ShowHeader="False"
GridLines="None" AutoGenerateColumns="False"
OnRowCommand ="GridView1_RowCommand"
runat="server">
You can get the GridViewRow from commandsource's NamingContainer and use FindControl method to find the Labels like below:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToBasket")
{
int id = 0;
string item = string.Empty; ;
decimal price = 0.00m;
string notes = string.Empty;
var gvRow = ((ImageButton)e.CommandSource).NamingContainer as GridViewRow;
var lblId = gvRow.FindControl("lblId") as Label;
var lblItem = gvRow.FindControl("lblId") as Label;
var lblPrice = gvRow.FindControl("lblId") as Label;
var lblNotes = gvRow.FindControl("lblId") as Label;
if (lblId != null && lblItem != null && lblPrice != null && lblNotes != null)
{
int.TryParse(lblId.Text, out id);
item = lblItem.Text;
decimal.TryParse(lblPrice.Text, out price);
notes = lblNotes.Text;
}
}
}

Add a new row in a gridview

next code shows a table filled (a gridview) with 5 columns and several rows. It's running well
while (drCLientes.Read())
{
GridView gv = new GridView();
gv.ID = "myGridID";
cong.Open();
da = new OleDbDataAdapter(sql, cong);
ds = new DataSet("Clientes");
da.Fill(ds, "Clientes");
cong.Close();
gv.DataSource = ds.Tables["Clientes"];
gv.DataBind();
}
After this code, the result is this:
Table that I have
Now I need to add a new row, in the first position. The first column empty. The second and third one columns must be combined. The same with the 4th and 5th ones. Here is an image with the table that I need:
Table that I need
Regards
You need to use Repeater instead of GridView.
<asp:Repeater ID="repList" runat="server" OnItemCommand="repList_ItemCommand">
<HeaderTemplate>
<table cellpadding="3" cellspacing="0" border="1" style="border-collapse: collapse;"
width="100%">
<thead style="text-align: center;" class="Gridheader">
<tr>
<td rowspan="2">Sequence</td>
<td colspan="4">Student</td>
<td colspan="3">Lesson</td>
<td rowspan="2">Point</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>Age</td>
<td>Class</td>
<td>Name</td>
<td>Teacher</td>
<td>Time</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:Label ID="lblSequence" runat="server" >
</asp:Label>
</td>
<td>
<asp:Label ID="lblStudentName" runat="server" >
</td>
<td>
<asp:Label ID="lblStudentSurname" runat="server" >
</td>
<td>
<asp:Label ID="lblStudentAge" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonClass" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonName" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonTeacher" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonTime" runat="server" >
</td>
<td>
<asp:Label ID="lblLessonPoint" runat="server" >
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
and Code Behind;
public void rptr_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Data.Common.DbDataRecord objRow = (System.Data.Common.DbDataRecord)e.Item.DataItem;
Label lblAnswer = e.Item.FindControl("lblSequence") as Label;
lblAnswer.Text = objRow["Sequence"].ToString();
Label lblStudentName = e.Item.FindControl("lblStudentName") as Label;
lblStudentName.Text = objRow["StudentName"].ToString();
...
..
.
}
}

How to highlight the row in listview according to condition?

I want to change background color of the row in particular condition.My code is
<asp:ListView ID="lst_SentItems" runat="server" ItemPlaceholderID="tr" OnItemDataBound="lst_SentItems_ItemDataBound">
<LayoutTemplate>
<table cellspacing="0">
<tr class="hdrRowColor1">
<td align="left" width="180px">
EmpName
</td>
<td align="left" width="180px">
Salary
</td>
<td align="left" width="180px">
Address
</td>
<td align="left" width="180px">
Department
</td>
</tr>
<tr id="tr" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr class='<%# Convert.ToBoolean(Container.DataItemIndex % 2) ? "EvenRowColor" : "OddRowColor" %>'>
<td align="left">
<asp:label id="lbl3" runat="server" text='<%# Eval("EmpName")%>' />
</td>
<td align="left">
' />
</td>
<td align="left">
<asp:label id="lbl1" runat="server" text='<%# Eval("Address")%>' />
</td>
<td align="left">
<asp:label id="lbl" runat="server" text='<%# Eval("Department")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Now i want to give color to particular department like if person is from Account dept then the row's background color should be red, if person belongs to IT dept then Back ground color should be green.
I have tried it some code in ItemdataBound but that code changes only the back ground of labels.i want to change the row back ground.That code is
protected void lst_SentItems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Label lblCount = (Label)e.Item.FindControl("lbl");
if (lblCount != null)
{
if (lblCount.Text == IT2")
{
lblCount.BackColor = System.Drawing.Color.Red;
}
}
}
}
Vision check this :It is just an idea and edit it according to your requirement.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataitem = (ListViewDataItem)e.Item;
int policyid = (int)DataBinder.Eval(dataitem.DataItem, "EmpID");
if (policyid == 3)
{
HtmlTableRow cell = (HtmlTableRow)e.Item.FindControl("MainTableRow");
cell.Style.Add("background-color", "Red");
}
}
}

Problem with ajax and RadTreeView

Hi all :
I have a user control which contain RadTreeView from Telerik inside ModalPopupExtender , the previous control loaded at runtime :
DashBoardDesignerCell tempCell = LoadControl("~/UserControls/DashBoardDesignerControls/DashboardDesignerCell.ascx") as DashBoardDesignerCell;
When SelectedNodeChanged occurs the ModalPopupExtender disappears.
This is the full code for user contrl:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="DashBoardDesignerCell.ascx.cs"
Inherits="RightBI.UserControls.DashBoardDesignerControls.DashBoardDesignerCell" %>
<%# Register Src="~/UserControls/PresentationControls/SelectPresentationControl.ascx" TagPrefix="rightbicontrols" TagName="selectpresentationcontrol" %>
<asp:UpdatePanel ID="dashboardDesignerCellUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table cellpadding="3" cellspacing="0" style='width: 100%; border-style: solid; border-width: thin;
border-color: Silver;'>
<thead>
<tr style='background: blue'>
<td>
<asp:ImageButton SkinID="selectControlButton" runat="server" ID="addControlButton"
OnClick="okButton_Click" />
<asp:ImageButton SkinID="deleteControlButton" runat="server" ID="removeControlButton"
OnClick="removeControlButton_Click" />
<asp:LinkButton runat="server" ID="LinkButton1" Style='display: none;'>
</asp:LinkButton>
<ajax:ModalPopupExtender runat="server" TargetControlID="addControlButton" ID="selectPresentationControlModalPopupExtender"
PopupControlID="popupSelectPresentationControl" CancelControlID="cancelButton"
BackgroundCssClass="popup_black_BG">
</ajax:ModalPopupExtender>
<asp:Panel runat="server" ID="popupSelectPresentationControl" Style='display: none;'>
<asp:UpdatePanel runat="server" ID="popupSelectUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<div class="popup_container">
<table cellpadding="0" cellspacing="0" class="popup_table">
<tr>
<td class="popup_header_left">
</td>
<td class="popup_header_middle">
</td>
<td class="popup_header_right">
</td>
</tr>
<tr>
<td class="popup_main_left">
</td>
<td class="popup_main">
<div class="popup_content">
<table cellpadding="0" cellspacing="0" style='width: 100%'>
<tr>
<td>
<span>Caption (*):</span>
</td>
<td>
<asp:TextBox runat="server" ID="captionTextBox">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="captionTextBox"
ValidationGroup="SelectPresentationControl" ErrorMessage="Caption is required."
Text="*">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<rightbicontrols:selectpresentationcontrol id="selectControl" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<telerik:RadButton ID="okButton" Text="Save" SkinID="okButton" runat="server" CommandName="Save"
ValidationGroup="SelectPresentationControl" OnClick="okButton_Click">
</telerik:RadButton>
<telerik:RadButton ID="cancelButton" Text="Cancel" SkinID="cancelButton" runat="server"
CommandName="Cancel">
</telerik:RadButton>
</td>
</tr>
</table>
</div>
</td>
<td class="popup_main_right">
</td>
</tr>
<tr>
<td class="popup_footer_left">
</td>
<td class="popup_footer_middle">
</td>
<td class="popup_footer_right">
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</td>
</tr>
</thead>
<tbody>
<tr>
<td style='min-height: 150px;'>
<asp:UpdatePanel runat="server" ID="controlUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel runat="server" ID="controlPanel">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</tbody>
</table>
</ContentTemplate>
</asp:UpdatePanel>
public Guid DashboardColumnID
{
get
{
if (ViewState["DashboardColumnID"] == null)
{
return Guid.Empty;
}
return new Guid(ViewState["DashboardColumnID"].ToString());
}
set
{
ViewState["DashboardColumnID"] = value;
}
}
protected Guid DashboardZoneID
{
get
{
if (ViewState["DashboardZoneID"] == null)
{
return Guid.Empty;
}
return new Guid(ViewState["DashboardZoneID"].ToString());
}
set
{
ViewState["DashboardZoneID"] = value;
}
}
protected Guid PresentationControlID
{
get
{
if (ViewState["PresentationControlID"] == null)
{
return Guid.Empty;
}
return new Guid(ViewState["PresentationControlID"].ToString());
}
set
{
ViewState["PresentationControlID"] = value;
}
}
protected void LoadDashboardZone()
{
DashboardColumn dashboardColumn = DashboardColumn.GetDashboardColumn(DashboardColumnID);
if (dashboardColumn.DashboardZones.Count == 1)
{
DashboardZoneID = dashboardColumn.DashboardZones.FirstOrDefault().Id;
}
if (DashboardZoneID == Guid.Empty)
{
removeControlButton.Visible = false;
addControlButton.Visible = true;
}
else
{
removeControlButton.Visible = true;
addControlButton.Visible = false;
}
controlPanel.Controls.Clear();
if (DashboardZoneID != Guid.Empty)
{
DashboardDesignerZone zone = LoadControl("~/UserControls/DashBoardDesignerControls/DashboardZone.ascx") as DashboardDesignerZone;
zone.DashboardZoneID = DashboardZoneID;
controlPanel.Controls.Add(zone);
}
controlUpdatePanel.Update();
}
protected void Page_Load(Object sender, EventArgs e)
{
//
String code = "function openWindow() {var oWnd = $find('" + selectPresentationWindow .ClientID+ "');oWnd.show(); }";
ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "OpenRadWindow", code , true);
LoadDashboardZone();
}
protected void okButton_Click(object sender, EventArgs e)
{
AddPresentationControl(captionTextBox.Text, selectControl.PresentationControlId);
//dashboardDesignerCellUpdatePanel.Update();
}
protected void removeControlButton_Click(Object sender, EventArgs e)
{
if (DashboardZone.RemoveDashboardZone(this.DashboardZoneID))
{
PresentationControlID = Guid.Empty;
DashboardZoneID = Guid.Empty;
LoadDashboardZone();
//dashboardDesignerCellUpdatePanel.Update();
}
}
public void AddPresentationControl(String caption, Guid presentationControlID)
{
DashboardZone tempDashboardZone = DashboardZone.AddDashboardZone(caption, DashboardColumnID, presentationControlID);
if (tempDashboardZone != null)
{
PresentationControlID = presentationControlID;
DashboardZoneID = tempDashboardZone.Id;
LoadDashboardZone();
}
}
Is There any idea???
Do you by any means do postback or ajax request when a treeview node is selected? This might be potential cause the modal popup extender is closed. Why you do not use the Telerik Ajax window instead, to compare the results? Also I assume that you load the user control dynamically on init or load of the page and clear the Controls collection of the container before adding the user control inside it.

Resources