asp.net listview control - using dynamic columns on itemtemplate - asp.net

Greetings smart people of stackoverflow! I have this ListView with the following itemtemplate
<ItemTemplate>
<tr>
<td><%#Eval("abc_availablearea").ToString()%> </td>
<td><%#Eval("abc_classname").ToString() %> </td>
<td><%#Eval("abc_division").ToString() %> </td>
<td><%#Eval("abc_managername").ToString() %> </td>
</tr>
</ItemTemplate>
Now I am trying to use dynamic field names...so something like
<ItemTemplate>
<tr>
<td><%#Eval(fieldOne).ToString()%> </td>
<td><%#Eval(fieldTwo).ToString() %> </td>
<td><%#Eval(fieldThree).ToString() %> </td>
<td><%#Eval(fieldFour).ToString() %> </td>
</tr>
</ItemTemplate>
But it's not working for me. Anyone have an idea on how to do this? Thank you in advance for your help.

You'll need to create the ItemTemplate dynamically. Creating Web Server Control Templates Programmatically

I wrote a post on it: http://start-coding.blogspot.com/2013/06/dynamic-columns-in-listview.html.
On ItemDataBound event, do something like this:
private void dynamicPopulateRow(HtmlTableRow row, System.Data.DataRowView drv, int iGeneration)
{
if (row != null)
{
// http://www.pcreview.co.uk/forums/do-enumerate-all-columns-dataviewrow-t1244448.html
foreach (DataColumn dc in drv.Row.Table.Columns)
{
string sEmployeeID = drv["LoginID"].ToString();
if (dc.ColumnName.Equals("LoginID"))
{
// http://msdn.microsoft.com/en-US/library/e5daxzcy(v=vs.80).aspx
// Define a new HtmlTableCell control.
HtmlTableCell cell = new HtmlTableCell("td");
// Create the text for the cell.
cell.Controls.Add(new LiteralControl(Convert.ToString(drv[dc.ColumnName])));
cell.ColSpan = dc.ColumnName.Equals("LoginID") ? I_COLSPAN - iGeneration : 1;
// Add the cell to the HtmlTableRow Cells collection.
row.Cells.Add(cell);
}
else if (!(dc.ColumnName.Equals("GENERATION") ||
dc.ColumnName.Equals("hierarchy") ||
dc.ColumnName.Equals("rowNo") ||
dc.ColumnName.Equals("EmployeeID")))
{
// http://msdn.microsoft.com/en-US/library/e5daxzcy(v=vs.80).aspx
// Define a new HtmlTableCell control.
HtmlTableCell cell = new HtmlTableCell("td");
bool bIsNull = drv[dc.ColumnName] is System.DBNull;
Literal ltrl = new Literal();
ltrl.Text += "<input type=\"checkbox\" name=\"" + dc.ColumnName + "\"" +
(bIsNull ? "" : " value=" + drv[dc.ColumnName].ToString()) +
" id=\"" + sEmployeeID + "~" + dc.ColumnName.Replace(" ", "_") + "\"" +//will be retrieved later
" onclick=\"didModify(this)\" " +
(bIsNull ? " disabled" : "") +
(!bIsNull && ((int)drv[dc.ColumnName]) > 0 ? " checked>" : ">");
cell.Controls.Add(ltrl);
// Add the cell to the HtmlTableRow Cells collection.
row.Cells.Add(cell);
}
else
{
//other rows
}
}
}
}

Related

ListView ItemDataBound Issue

I have a ListView displaying Products. I'm trying to add a Category label before each list of products, where each category may have several products. I'm trying to achieve this without nesting ListViews. Right now the issue is that it is repeating the category name over every product name. This is not what I want. I only want it to list the category name over products when the category name changes. Both Product and Category are being returned from the same DB table.
Category 1
Product 1
Product 2
Category 2
Product 3
Product 4
<asp:ListView ID="classesList" ItemPlaceholderID="classesPlaceHolder"
OnItemDataBound="ClassesList_ItemDataBound" runat="server">
<LayoutTemplate>
<asp:Panel ID="classesPanel" CssClass="classesPanel" runat="server">
<asp:PlaceHolder runat="server" ID="classesPlaceHolder"></asp:PlaceHolder>
</asp:Panel>
</LayoutTemplate>
<ItemTemplate>
<strong><asp:Label ID="categoryNameLabel" runat="server"></asp:Label></strong>
<a href='#'><%# Eval("product_name") %></a><br />
</ItemTemplate>
</asp:ListView>
protected void ClassesList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
Label categoryNameLabel = e.Item.FindControl("categoryNameLabel") as Label;
if (!categoryNameLabel.Text.Contains(row["cat_name"].ToString()))
{
categoryNameLabel.Text = row["cat_name"].ToString() + "<br />";
}
}
}
Doing this in a loop would be trivial. But since the ItemDataBound event fires for every data item coming back, I'm trying to find a simple way of doing this.
UPDATE
I moved the data binding out of the Page Load method to a method called GetClasses(). This new method is called from the first page load. I gave up on the ItemDataBound method as I wanted to implement a loop and toggle the visibility of the categoryLabel. Unfortunately, no category is appearing now so maybe I'm missing something?
<ItemTemplate>
<strong><asp:Label ID="categoryNameLabel" Visible="false" runat="server"></asp:Label></strong>
<i class="fa fa-angle-double-right" aria-hidden="true"></i>
<a href='#'><%# Eval("product_name") %></a><br />
</ItemTemplate>
public void GetClasses()
{
DataSet ds = CatalogDALC.GetClassesByCategory(categoryID, DateTime.Now);
classesList.DataSource = ds;
classesList.DataBind();
// Set main category header for classes - loop here and set category name headers
int count = 0;
foreach (ListViewDataItem item in classesList.Items)
{
Label categoryNameLabel = item.FindControl("categoryNameLabel") as Label;
if (count == 0) // Set the first category name header regardless
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
else
{
if (ds.Tables[0].Rows[count-1]["cat_name"].ToString() != ds.Tables[0].Rows[count]["cat_name"].ToString())
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
else
{
categoryNameLabel.Visible = false;
continue;
}
}
count++;
}
}
My updated solution in the question was the right approach - the only bug was the continue statement - it was bypassing the counter. This is the working version for anyone in a similar situation:
<ItemTemplate>
<strong><asp:Label ID="categoryNameLabel" Visible="false" runat="server"></asp:Label></strong>
<i class="fa fa-angle-double-right" aria-hidden="true"></i>
<a href='#'><%# Eval("product_name") %></a><br />
</ItemTemplate>
public void GetClasses()
{
DataSet ds = CatalogDALC.GetClassesByCategory(categoryID, DateTime.Now);
classesList.DataSource = ds;
classesList.DataBind();
// Set main category header for classes - loop here and set category name headers
int count = 0;
foreach (ListViewDataItem item in classesList.Items)
{
Label categoryNameLabel = item.FindControl("categoryNameLabel") as Label;
if (count == 0) // Set the first category name header regardless
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
else
{
if (ds.Tables[0].Rows[count-1]["cat_name"].ToString() != ds.Tables[0].Rows[count]["cat_name"].ToString())
{
categoryNameLabel.Text = ds.Tables[0].Rows[count]["cat_name"].ToString() + "<br />";
categoryNameLabel.Visible = true;
}
}
count++;
}
}

Dynamically added controls remove when dropdown event fired in the datalist in user control

I am working on an ASP.NET VB.NET web form - a reservation web application, in which only one aspx page and rest are user-control page.
At run-time of aspx page, user controls load as per step and name define in db, like in below link.
ASP.NET Custom user control to add dynamically
In the first step, the first user-control is bind in page-init which is used to shows reservation availability detail in .NET datalist control like (see images).
All the details are bind to generate run-time control via data-list's item_databound event.
ImageOfRoom (asp.net Image Control) - on click popup will open with scroll functionality
Name (direct databound)
Amenities (icon(s)) - dynamically add from db.
No. of room as per room type (asp.net dropdown control) - dynamically add from db and on selected index changed, need another dropdown bind in same row and on change of adult dropdown price will vary.
Total price (direct databound)
Book now (button)
Now issue is whenever any event of datalist(click on romm-image or dropdown selected index changed) fired, the dynamic control remove like ammenities, dynamic dropdown of other row etc.
What I tried as :- ispostback, relevant event of page-life cycle, ajax-jquery, viewstate.
I checked this also, But no luck. :
Dynamically added controls in Asp.Net
I analyze that, the user-control is always rebound and then event fired, but no datalist rebind and thus no - databound event fire and finally dynamic control is removed. If you wish, I will share the code too (but its huge code).
So question is how to retain the dynamic controls and its value when dropdown selected index changed or image click event fired in datalist in usercontrol?
I am not used update-panel, does that work? If yes, then please give sample data.
Good to answer with sample data. Even please suggest that if possible via any other control like grid-view or else, then I ready to change it.
Updated
This is my code
Load User Control code
In aspx page, usercontrol define to load another user control as per current step. This “uc” user control tag in aspx page.
<div id="divPlaceholder" runat="server">
<uc:DynamicControlHost ID="ucDynamicControlHost" runat="server" />
</div>
In page_load as well as page_prerender( ispostback ) , the below code execute to load runtime user-control.
public Control SetUserControlPath(string path)
{
Control c = null;
if (this.dynamicAllContent.Controls.Count > 0)
{
//Check that the new control is not the same as the current control
if (!this.UserControlPath.Equals(path))
{
//Remove the old item because we can not simply replace them!
this.dynamicPHAllContent.Controls.Clear();
c = Page.LoadControl(path + ".ascx");
c.ID = path;
//Add New Item
this.dynamicAllContent.Controls.Add(c);
lock (_userControlLockObject)
{
//Store the new path
_strUserControl = path;
}
}
}
else
{
c = Page.LoadControl(path + ".ascx");
c.ID = path;
this.dynamicAllContent.Controls.Add(c);
_strUserControl = path;
}
return c;
}
Structure of datalist in usercontrol
<asp:UpdatePanel ID="EmployeesUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DataList ID="dlLodgingAvailableDetails" ShowHeader="true" OnSelectedIndexChanged="dlLodgingAvailableDetails_SelectedIndexChanged" runat="server" CellPadding="2" CellSpacing="2" BorderWidth="1px" BorderColor="Black"
OnItemDataBound="dlLodgingAvailableDetails_ItemDataBound" BorderStyle="Solid" GridLines="Horizontal" HorizontalAlign="Justify">
<HeaderStyle CssClass="submit_butt"></HeaderStyle>
<HeaderTemplate>
Lodging Item Type Details
<asp:Button ID="btnBookRoom" runat="server" Text="Book Rooms" CssClass="submit_butt" OnClick="btnBookRoom_Click" />
</HeaderTemplate>
<ItemTemplate>
<table cellpadding="2" cellspacing="0" border="1" style="width: 100%";>
<tr>
<td style="width:170px">
<asp:ImageButton ID="imgLodging" OnClick="imgLodging_Click" commandargument='<%# Eval("ItemTypeId") %>'
runat="server" ImageUrl='<%# Eval("Photo") %>' Width="150px" Height="120px" />
</td>
<td style="width:180px">
<b>Name</b><br />
<span><%# Eval("ItemTypeName") %></span><br />
<b>Occupancy</b> <span><%# Eval("Occupancy") %></span>
<br />
<asp:panel ID="placeholderAmmenities" runat="server" Visible="True" ></asp:panel>
</td>
<td style="width:100px">
<b>Room</b><br />
<asp:hiddenfield runat="server" ID="hdnItemTypeId" Value='<%# Eval("LodgingItemTypeId") %>' />
<asp:DropDownList ID="ddlAvailable" runat="server"
AppendDataBoundItems="True" SelectedValue='<%# Bind("LodgingReservationsAvailable") %>' >
<asp:ListItem Value="0" Text="0"/>
<asp:ListItem Value="1" Text="1"/>
<asp:ListItem Value="2" Text="2"/>
</asp:DropDownList>
</td>
<td>
</td>
<td style="width:100px">
<div id="dvadult" runat="server"></div>
<asp:placeholder runat="server" ID="PlaceHolderAdult" ViewStateMode="Enabled" EnableTheming="False" Visible="True" ></asp:placeholder>
</td>
<td style="width:50px">
<asp:Label runat="server" ID="lblnumbernight" ></asp:Label>
</td>
<td style="width:50px">
<asp:placeholder ID="placeholderPrice" runat="server" Visible="True"></asp:placeholder>
</td>
<td style="width:50px">
<b>Total</b><br />
<asp:Label runat="server" ID="lblTotalAmount" ></asp:Label>
</td>
<td style="width:100px">
<asp:Button ID="btnBookRoom" runat="server" Text="Book Rooms" CssClass="submit_butt" />
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorStyle BackColor="Lime" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
Datalist item data bound event code (its inside image binding , price related field add and also creating the dynamic control as per the condition)
protected void dlLodgingAvailableDetails_ItemDataBound(object sender, DataListItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Image img = e.Item.FindControl("imgLodging") as Image;
if (img != null)
{
string bytesval = ((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[3].ToString();
if (string.IsNullOrWhiteSpace(bytesval)) return;
byte[] bytes = (byte[])((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[3];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
img.ImageUrl = "data:image/png;base64," + base64String;
}
DropDownList ddlList = e.Item.FindControl("ddlAvailable") as DropDownList;
Label lbldipositamount = e.Item.FindControl("lblTotalAmount") as Label;
Label lblnumbernight = e.Item.FindControl("lblnumbernight") as Label;
var PlaceHolderAmmenities = e.Item.FindControl("placeholderAmmenities") as Panel;
ddlList.Attributes.Add("onchange", " openLodgingNumber1(this,'" + ddlList.SelectedValue + "');");
int? LodgingItemTypeId = Convert.ToInt32(((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[1]);
DataSet ds = new DataSet();
ds = LodgingData.SelectLodgingItemTypeAmenityDateSet(LodgingItemTypeId);
DataTable dt = new DataTable();
if (ds != null)
{
dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
Image image = new Image();
image.ID = "imgAmmenities" + j + DateTime.Now.ToString();
string bytesval = dt.Rows[j]["AmenityIcon"].ToString(); //((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[4].ToStrin();
//if (string.IsNullOrWhiteSpace(bytesval)) return;
if (bytesval != string.Empty)
{
byte[] bytes = (byte[])dt.Rows[j]["AmenityIcon"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
image.ImageUrl = "data:image/png;base64," + base64String;
image.Height = 20;
image.Width = 20;
image.EnableViewState = true;
PlaceHolderAmmenities.Controls.Add(image);
PlaceHolderAmmenities.Controls.Add(new LiteralControl(" "));
}
}
}
}
decimal PriceTotal = 0;
var PlaceHolderPrice = e.Item.FindControl("placeholderPrice") as PlaceHolder;
DataSet dsprice = new DataSet();
dsprice = LodgingData.SelectLodgingItemTypePrice(LodgingItemTypeId);
if (dsprice != null)
{
DataTable dtprice = new DataTable();
dtprice = dsprice.Tables[0];
if (dtprice.Rows.Count > 0)
{
DateTime fromdate = Convert.ToDateTime(txtFromDate.Text);
DateTime todate = Convert.ToDateTime(txtToDate.Text);
double daterange = ((todate - fromdate).TotalDays + 1);
lblnumbernight.Text = daterange.ToString();
//for (DateTime date = fromdate; date >= todate; date.AddDays(1))
for (int d = 0; d < Convert.ToInt32(daterange); d++ )
{
DateTime date = fromdate.AddDays(d);
//DataView dv = new DataView(dtprice);
DataTable dtprice1 = new DataTable();
DataRow[] rows = dtprice.Select("#" + date + "# >= PriceStartDate AND" + "#" + date + "# <= PriceEndDate");
if (rows.Length > 0)
{
dtprice1 = rows.CopyToDataTable();
}
if (dtprice1.Rows.Count > 0)
{
for (int j = 0; j < dtprice1.Rows.Count; j++)
{
Label lbl = new Label();
string dayofweek = dtprice1.Rows[j]["DayOfWeekId"].ToString();
if (dayofweek.Trim() == eDayOfWeek.All.ToString().Trim())
{
lbl.ID = "lbl" + j;
lbl.Text = dtprice1.Rows[j]["Price"].ToString();
PriceTotal += Convert.ToDecimal(dtprice1.Rows[j]["Price"]);
PlaceHolderPrice.Controls.Add(lbl);
PlaceHolderPrice.Controls.Add(new LiteralControl("<br />"));
}
else if (Convert.ToInt32(dayofweek) == Convert.ToInt32(date.DayOfWeek + 1))
{
lbl.ID = "lbl" + j;
lbl.Text = dtprice1.Rows[j]["Price"].ToString();
PriceTotal += Convert.ToDecimal(dtprice1.Rows[j]["Price"]);
PlaceHolderPrice.Controls.Add(lbl);
PlaceHolderPrice.Controls.Add(new LiteralControl("<br />"));
}
}
}
else
{
DataView dv1 = new DataView(dtprice);
dv1.RowFilter = "PriceStartDate IS NULL OR PriceEndDate IS NULL";
//dv1.RowFilter = "PriceStartDate == null and PriceEndDate == null";
DataTable dtprice2 = new DataTable();
dtprice2 = dv1.ToTable();
for (int j = 0; j < dtprice2.Rows.Count; j++)
{
Label lbl = new Label();
string dayofweek = dtprice2.Rows[j]["DayOfWeekId"].ToString();
if (dayofweek.Trim() == eDayOfWeek.All.ToString().Trim())
{
lbl.ID = "lbl" + j;
lbl.Text = dtprice2.Rows[j]["Price"].ToString();
PriceTotal += Convert.ToDecimal(dtprice2.Rows[j]["Price"]);
PlaceHolderPrice.Controls.Add(lbl);
PlaceHolderPrice.Controls.Add(new LiteralControl("<br />"));
}
else if (Convert.ToInt32(dayofweek) == Convert.ToInt32(date.DayOfWeek + 1))
{
lbl.ID = "lbl" + j;
lbl.Text = dtprice2.Rows[j]["Price"].ToString();
PriceTotal += Convert.ToDecimal(dtprice2.Rows[j]["Price"]);
PlaceHolderPrice.Controls.Add(lbl);
PlaceHolderPrice.Controls.Add(new LiteralControl("<br />"));
}
}
}
}
}
}
lbldipositamount.Text = PriceTotal.ToString();
// var amount = ((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[3];
int selectedvalue = Convert.ToInt32(ddlList.SelectedItem.Text);
if (selectedvalue != 0)
{
double totalamount = selectedvalue * Convert.ToDouble(PriceTotal);
lbldipositamount.Text = totalamount.ToString();
}
}
}
catch (Exception)
{
throw;
}
}
On dynamically genereted dropdown selection event fired
In above even add this dropdown dynamically, now when this control's event is called , further dynamic control is adding as per the condition.
Issue is this event remove the dynamic other control as well even for other row of the previous selection is hidden or lost , so we retain the dynamic control in any post back and event fire.
protected void ddlAvailable_SelectedIndexChanged(object sender, EventArgs e)
{
// if (UserControlTextBoxChanged != null) dlLodgingAvailableDetails_ItemDataBound(sender, e);
//dlLodgingAvailableDetails.ItemDataBound += new DataListItemEventHandler(dlLodgingAvailableDetails_ItemDataBound);
double amount = 0;
var ddlList = (DropDownList)sender;
var row = (DataListItem)ddlList.NamingContainer;
//get the Id of the row
DataSet ds = new DataSet();
int? Id = Convert.ToInt32(((HiddenField)row.FindControl("hdnItemTypeId")).Value);
double? tamount = Convert.ToDouble(((Label)row.FindControl("lblTotalAmount")).Text);
int? groupid = Convert.ToInt32(ddlLodgingGroup.SelectedValue);
int selectedvalue = Convert.ToInt32(ddlList.SelectedItem.Text);
DateTime? startdate = Convert.ToDateTime(txtFromDate.Text);
DateTime? enddate = Convert.ToDateTime(txtToDate.Text);
ds = LodgingData.SelectLodgingItemTypeDataSet(startdate, enddate, groupid);
DataTable dt = new DataTable();
DataView dv = new DataView();
if (ds != null)
{
dt = ds.Tables[0];
dv = dt.DefaultView;
dv.RowFilter = "LodgingItemTypeId=" + Id;
}
dt = dv.ToTable();
if (dt.Rows.Count > 0)
{
if (tamount != null)
{
amount = Convert.ToDouble(tamount);
}
}
//amount = Convert.ToDouble(((Label)row.FindControl("lblTotalAmount")).Text);
var PlaceHolder1 = ((PlaceHolder)row.FindControl("PlaceHolderAdult"));
double totalamount = 0;
if (selectedvalue != 0)
{
totalamount = selectedvalue * Convert.ToDouble(amount);
((Label)row.FindControl("lblTotalAmount")).Text = totalamount.ToString();
Label lblAdult = new Label();
lblAdult.ID = "lblAdult";
lblAdult.Text = "Adult";
lblAdult.Font.Bold = true;
PlaceHolder1.Controls.Add(lblAdult);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
else
{
totalamount = amount;
}
for (int j = 0; j < selectedvalue; j++)
{
DropDownList ComboBox = new DropDownList();
ComboBox.ID = "ComboBox" + j;
ComboBox.AutoPostBack = false;
ComboBox.Attributes.Add("runat", "server");
ComboBox.Items.Add(new ListItem("0", "0"));
ComboBox.Items.Add(new ListItem("1", "1"));
ComboBox.Items.Add(new ListItem("2", "2"));
ComboBox.SelectedIndexChanged += new EventHandler(Dynamic_Method);
PlaceHolder1.Controls.Add(ComboBox);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
}
I'm not sure how much this will solve for you, but here is an example of preserving a row of data in a gridview with a templatefield containing a dynamically generated dropdownlist
I broke the process into 2 parts
1) Save data currently in Gridview to a session variable
2) Recreate, source, and bind controls
Here's saving the values in the gridview. I use a recursive find control formula I found on this site (but don't remember from where) because my controls are generated and placed inside the gridview row without unique names. For ex. the tbxA that exists in row 1 is different than the tbxA in row 2. This may not apply to you - the key is to find all controls you want to save the values of.
Private Sub SaveValues()
Dim savedTable As New DataTable
savedTable.Columns.Add(New DataColumn("A"))
For i = 0 To GridView1.Rows.Count - 1
Dim existing(0) As Object
existing(0) = TryCast(FindControlRecursive(GridView1.Rows(i), "ddlA"), DropDownList).SelectedValue
savedTable.Rows.Add(existing)
Next
Session("GhostTable") = savedTable
End Sub
Then in Page_Load under(when it IS a postback) set the gridview datasource to the session variable, and databind it. This will trigger the following code for every row:
Keep in mind I also have the datasource of the dropdown list stored in a session variable on page load. This allows the datasource and databind to occur for the dropdown every time it is generated.
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
'Handles databinding of each gridview1 row ddl to gridview templatefield
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlA As New DropDownList()
ddlA.DataSource = Session("lengthList")
ddlA.DataBind()
ddlA.ID = "ddlA"
ddlA.SelectedValue = TryCast(e.Row.DataItem, DataRowView).Row.Item("A").ToString()
e.Row.Cells(1).Controls.Add(ddlA)
End if
End Sub
The ddlA.SelectedValue = TryCast(e.Row.DataItem, DataRowView).Row.Item("A").ToString() is what preserves the data after any postback. It determines which row is being bound, and then repopulates the control with whatever it previously was.
Hope this helps!
To make sure that the gridview is populated every time, call SaveValues in your event handlers.
Protected Sub ddlEmpNumber_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlEmpNumber.SelectedIndexChanged
'Do whatever on selected index change, then the following:
Call SaveValues()
GridView1.DataSource = Session("GhostTable")
GridView1.DataBind()
End Sub
Do it all in the page_init
You're wiring up the eventhandler in the ASP part like this:
<asp:DataList ID ... OnSelectedIndexChanged="dlLodgingAvailableDetails_SelectedIndexChanged" ...
and then in code of the handler attempting to re-wire the other ones:
ComboBox.SelectedIndexChanged += new EventHandler(Dynamic_Method);
Any time you do dynamic stuff on a webforms page, all setup including event handler wireup must be in the page_init. When events run, re-wiring won't work. For what you're doing (asp: ... and during a method call), it's ending up in the wrong part of the lifecycle and the right things aren't there to either add the javascript to call doPostback, or if that runs the __eventargs, etc. have data for controls that aren't built yet (event postbacks are checked for AFTER page init so you have to create the controls before that check) and therefore get ignored.
Note - probably you know this but you have to give controls unique IDs, generally can by done if you have a numeric primary key by just appending to the datarows. (might have to do this manually to child controls in user control, have had to do this with usercontrols in page_init, hopefully you won't)
I work on a big app that dynamically builds every screen in webforms (from a UI generator) in the page-init. All work is done in events, they work like a charm! Page_load is basically empty. Get to know the asp page lifecycle - it's a booger but easier than rewriting your app in HotTowel (although think about that the next app ;-)
Pretty good diagram: http://blogs.msdn.com/b/aspnetue/archive/2010/01/14/asp-net-page-life-cycle-diagram.aspx
Good luck!
I have done every thing as above suggested and other google search site too. But it refresh the data and every time it rebind all dynamic object in the grid.
Finally I have done every thing based on javascript and jquery, without that dynamic control always rebind which I don't want. So any body who are stuck in this situation forgot to r and d and work with javascript/ webmethod with dynamic controls.
Thank all who given an answer

grouping headers in gridview

With reference to this link: https://web.archive.org/web/20210301194237/https://www.4guysfromrolla.com/articles/091708-1.aspx
I want to implement grouping headers for the gridview similar to what is shown in the below listview.
HTML Source:
<asp:ListView ID="ProductsGroupedByDataField" runat="server" DataSourceID="odsAllPlannedLeaves"
<LayoutTemplate>
<table cellspacing="0" cellpadding="5" rules="all" border="1" >
<tr style="background-color:#5D7B9D;color:White">
<th>Start Date</th>
<th>End Date</th>
<th>Date Of Applying</th>
</tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<%# AddGroupingRowIfSupplierHasChanged() %>
<tr class='data<%# Container.DataItemIndex % 2 %>'>
<td><%#GetDate(Eval("Emp_StartDate"))%></td>
<td><%#GetDate(Eval("Emp_EndDate"))%></td>
<td><%#GetEntryDate(Eval("Emp_EntryDate"))%></td>
</tr>
</ItemTemplate>
</asp:ListView>
Method:
int LastEmpId = 0;
protected string AddGroupingRowIfSupplierHasChanged()
{
int CurrentEmpId = Convert.ToInt32(Eval("Emp_Id").ToString());
if (LastEmpId != CurrentEmpId)
{
LastEmpId = CurrentEmpId;
string CurrentEmpName = Eval("Emp_Name").ToString();
string CurrentEmailId = Eval("Emp_EmailID").ToString();
return string.Format("<tr style='text-align:left' class='group'><td colspan='3'>Employee Name: " + CurrentEmpName + " | Email Id: " + CurrentEmailId + "</td></tr>");
}
else
return string.Empty;
}
How can I implement this in a gridview ?
I use this little "break" for the Gridview's subheaders:
private string tmpCategoryName = "";
protected void gdvRowDatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
HiddenField hdnTmpSubTitle = (HiddenField)e.Row.FindControl("hdnTmpSubTitle");
if (!(tmpCategoryName == hdnTmpSubTitle.Value)) {
tmpCategoryName = hdnTmpSubTitle.Value;
Table tbl = e.Row.Parent as Table;
if (tbl != null) {
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.ColumnSpan = this.grdViewProjects.Columns.Count;
cell.Width = Unit.Percentage(100);
cell.Style.Add("font-weight", "bold");
cell.Style.Add("background-color", "#c0c0c0");
cell.Style.Add("color", "white");
HtmlGenericControl span = new HtmlGenericControl("span");
span.InnerHtml = tmpCategoryName;
cell.Controls.Add(span);
row.Cells.Add(cell);
tbl.Rows.AddAt(tbl.Rows.Count - 1, row);
}
}
}
}
This code gets the top level "Category" which is within your Gridview and just creates an extra table row, with one single cell, adds the style and then adds the category name. If the category is different from row to row, then change it. Ensure that your datasource is ordered by your "Category" first (or whatever you're using as a subheading).
Also ensure you place a HiddenField with the "Category" into a Template Field in your Gridview.

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

I am getting the following error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
but all I am trying to do is inside a ASP.NET REPEATER Control
<% if ( Eval("Message").ToString() == HttpContext.Current.Profile.UserName) %>
<% { %>
<asp:ImageButton runat="server" etc.... />
<% } %>
The syntax is
<%# Eval("...") %>
You could do something like
<asp:ImageButton Visible='<%# ShowImg(Eval(Container.DataItem,"Message")) %>' />
and in your codebehind:
boolean ShowImg(string msg)
{
return (msg == HttpContext.Current.Profile.UserName);
}
An alternative is this:
<asp:ImageButton runat="server" Visible='<%# Eval("Message").ToString() == HttpContext.Current.Profile.UserName %>' />
Then there is no need for code behind.
Its too late but i would like to answer it in my way, what i used to achieve it:
<%# Eval("Message").toString()== HttpContext.Current.Profile.UserName)?"<asp:ImageButton runat="server" etc.... />" :""%>
Now this will only show image button if Message is equal to username.
This might help any one else in same situation.
In my situation i needed to check null and empty string...so i implemented like this below:
<%# Eval("DateString")!= null && Eval("DateString")!= ""? "<span class='date'>"+Eval("DateString") + "</span>":"" %>
Thanks
Another way to implement it:
public string nonImage() {
string imgTag = "", Article_OwnerID = "", Article_ID = "", Article_OwnerType = "", imgSrc = "";
DataTable DtArticles = SE_Article.GetArticlesList(UserID, UserID, ProfileType, CounterOfPage, CountPerPage, (short) SE_Action.OwnerType.user, SE_Security.CheckInjection(TxtSearch.Text.Trim()), CategoryID, "all_articles", DrpOrderBy.SelectedValue, DrpSort.SelectedValue);
if (DtArticles != null && DtArticles.Rows.Count > 0) {
Article_OwnerID = DtArticles.Rows[0]["Article_OwnerID"].ToString();
Article_ID = DtArticles.Rows[0]["Article_ID"].ToString();
Article_OwnerType = DtArticles.Rows[0]["Article_OwnerType"].ToString();
}
if (SE_Article.GetArticleCover(Convert.ToInt32(Article_OwnerID), Convert.ToInt32(Article_ID), Convert.ToInt16(Article_OwnerType)) != System.Configuration.ConfigurationManager.AppSettings["NoPhotoArticleThumb"]) {
imgSrc = SE_Article.GetArticleCover(Convert.ToInt32(Article_OwnerID), Convert.ToInt32(Article_ID), Convert.ToInt16(Article_OwnerType));
imgTag = "<img class='img_article_cover' src='" + imgSrc + "' alt='مقاله" + Article_ID + "' />";
}
return imgTag;
}
<% nonImage(); %>

When I add an option to a select the select gets narrower

Inherited an app with a page that has a link that calls the javascript function addValueClick(), when I do this a dialog box pops up, I type in some text, and then the text gets put in the select box. Every time a new option is added to the select it gets about 5 pixels narrower. I can't figure out why this is happening, but it only happens in IE7
Here is the javascript:
function addValueClick()
{
var newValue = prompt("Please enter a new value.","");
if (newValue != null && newValue != "")
{
var lst = document.getElementById("lstValues");
var opt = document.createElement("option");
opt.setAttribute("selected", "true");
opt.appendChild(document.createTextNode(newValue));
lst.appendChild(opt);
updateBtns();
copyValues();
}
}
function copyValues()
{
var frm = document.forms[0];
var lst = frm.elements["lstValues"];
var hid = frm.elements["hidValues"];
var xml = "<root>";
for (var i = 0; i < lst.options.length; i++)
{
xml += "<value seq_num=\"" + (i + 1) + "\">" +
lst.options[i].text + "</value>";
}
xml += "</root>";
hid.value = xml;
}
function updateBtns()
{
var lst = document.getElementById("lstValues");
var iSelected = lst.selectedIndex;
var lnkEdit = document.getElementById("lnkEditValue");
var lnkDelete = document.getElementById("lnkDeleteValue");
var lnkUp = document.getElementById("lnkValueUp");
var lnkDown = document.getElementById("lnkValueDown");
if (iSelected == -1)
{
lnkEdit.style.visibility = "hidden";
lnkDelete.style.visibility = "hidden";
lnkUp.style.visibility = "hidden";
lnkDown.style.visibility = "hidden";
}
else
{
lnkEdit.style.visibility = "visible";
lnkDelete.style.visibility = "visible";
if (iSelected == 0)
lnkUp.style.visibility = "hidden";
else
lnkUp.style.visibility = "visible";
if (iSelected == lst.options.length - 1)
lnkDown.style.visibility = "hidden";
else
lnkDown.style.visibility = "visible";
}
}
EDIT:
The HTML, it's actually ASP.NET. All the listValueChanged() method does is call updateButtons() above.
<tr>
<TD class=body vAlign=top noWrap align=right><b>Values:</TD>
<TD class=body vAlign=top noWrap align=left><asp:ListBox id="lstValues" runat="server" onchange="lstValuesChange();" Rows="9" onselectedindexchanged="lstValues_SelectedIndexChanged"></asp:ListBox></TD>
<TD class=body vAlign=top noWrap align=left>
<TABLE id="Table2" cellSpacing="2" cellPadding="2" border="0">
<TR>
<TD noWrap>
<asp:HyperLink id="lnkAddValue" runat="server" NavigateUrl="javascript:addValueClick();">Add</asp:HyperLink></TD>
</TR>
<TR>
<TD noWrap>
<asp:HyperLink id="lnkEditValue" runat="server" NavigateUrl="javascript:editValueClick();">Edit</asp:HyperLink></TD>
</TR>
<TR>
<TD noWrap>
<asp:HyperLink id="lnkDeleteValue" runat="server" NavigateUrl="javascript:deleteValueClick();">Delete</asp:HyperLink></TD>
</TR>
<TR>
<TD noWrap> </TD>
</TR>
<TR>
<TD noWrap>
<asp:HyperLink id="lnkValueUp" runat="server" NavigateUrl="javascript:valueUpClick();">Up</asp:HyperLink></TD>
</TR>
<TR>
<TD noWrap>
<asp:HyperLink id="lnkValueDown" runat="server" NavigateUrl="javascript:valueDownClick();">Down</asp:HyperLink></TD>
</TR>
</TABLE>
</TD>
</TR>
It may have more to do with the html than the script. What is the select contained in? Based on the JS, I don't think there are any problems here.
I know that adding options via SelectElement.innerHTML = '...'; fails in IE (bug 274).
But I don't know about adding options via .createElement() failing... although I'm not overly surprised.
You can use the JavaScript new Option(); syntax to create them, I'm fairly certain that works without failure.
The issue was where the Option was being added to the Select I changed it to the following and it works perfectly:
function addValueClick()
{
var newValue = prompt("Please enter a new value.","");
if (newValue != null && newValue != "")
{
var lst = document.getElementById("lstValues");
var opt = document.createElement("option");
opt.text = newValue;
opt.value = newValue;
try {
lst.add(opt, null); // real browsers
}
catch (ex) {
lst.add(opt); // IE
}
updateBtns();
copyValues();
}
}

Resources