Repeater Control does not populate the first data - c#

After binding data to repeater control I am trying to populate some control inside it through Placeholder.
I have written the following .
protected void RepeaterDetails_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
foreach (Control ri in RepeaterDetails.Items)
{
//if (ri.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
//{
Label lblDatatype = (Label)e.Item.FindControl("lblDatatype");
if (lblDatatype.Text != null)
{
if (lblDatatype.Text.ToLower() == "list")
{
PlaceHolder RepeaterPanel = (PlaceHolder)e.Item.FindControl("RepeaterPanel");
DropDownList ddl = new DropDownList();
ddl.Width = 150;
RepeaterPanel.Controls.Add(ddl);
i++;
break;
}
else
{
PlaceHolder RepeaterPanel = (PlaceHolder)e.Item.FindControl("RepeaterPanel");
tbx = new TextBox();
tbx.ID = "TextBox" + i;
tbx.Width = 150;
RepeaterPanel.Controls.Add(tbx);
i++;
break;
}
}
// }
}
}
catch
{
}
}
But the repeater control does not populate the first value.
please see the attached image
List and textboxes are getting populated except the first one. Can anyone help?

Related

Dynamically created RadioButtons in a Gridview losing state

Background Info:
For various reasons I'm having to dynamically create a RadioButtonList in a column of my GridView.
The grid presents the user with a bunch of data on which they have to leave a comment and make a decision on one of 3 radio options:
Naturally, there are a number of rows on the grid. And the way this is to work is the user makes comments and decisions on all the items before saving to the database.
This screen is to be used multiple times, so the radio buttons have to reflect the value that's stored in the database.
The Problem:
Everything works apart from one thing: the radio buttons are always being reset to their initial state (value = 1) because the grid and the controls are being re-created on postback.
Code:
Here's the working code, some of it redacted/edited...
protected void TheGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
[...]
foreach (DataControlFieldCell dc in e.Row.Cells)
{
switch (dc.ContainingField.ToString())
{
[...]
case (_ColReview):
int status = int.Parse(dc.Text);
if (status == 0)
{
dc.Text = "Not sent for review";
}
else
{
var comment = new TextBox();
comment.ID = "ReviewCommentTextBox";
comment.CssClass = "form-control";
comment.Width = 290;
dc.Controls.Add(comment);
var radioButtons = new RadioButtonList();
var rb = new ListItem();
if (status == 2) rb.Selected = true;
rb.Value = "2";
rb.Text = "Yes, Add to Watch List";
radioButtons.Items.Add(rb);
rb = new ListItem();
if (status == 3) rb.Selected = true;
rb.Value = "3";
rb.Text = "No, do not add to Watch List";
radioButtons.Items.Add(rb);
rb = new ListItem();
//initial value in database is 1, hence this will be initially selected
if (status == 1) rb.Selected = true;
rb.Value = "1";
rb.Text = "Skip: no decision";
radioButtons.ID = "RowRadioButtonList";
radioButtons.Items.Add(rb);
dc.Controls.Add(radioButtons);
}
break;
}
}
}
}
protected void BulkupdateLinkButton_Click(object sender, EventArgs e)
{
foreach(GridViewRow gvr in TheGridView.Rows)
{
[...]
int radioItemValue = 0;
foreach (DataControlFieldCell dc in gvr.Cells)
{
string cellName = dc.ContainingField.ToString();
string cellText = dc.Text;
switch(cellName)
{
[...]
case (_ColReview):
TextBox tb = (TextBox)gvr.FindControl("ReviewCommentTextBox");
comment = tb.Text;
RadioButtonList rbl = (RadioButtonList)gvr.FindControl("RowRadioButtonList");
foreach(ListItem li in rbl.Items)
{
//Issue arrives here: selected item is reset on postback, value is always 1
if (li.Selected)
{
radioItemValue = ToolBox.ConvertToInt(li.Value);
}
}
break;
}
}
if (!string.IsNullOrEmpty(comment) && (radioItemValue > 0))
{
if (radioItemValue != 1) // 1 = pending/skip
{
//code to add update the record and save the comment
[...]
}
}
}
}
Idea on solution
Now I can get around this by using a HiddenField in each row and setting up some JavaScript/JQuery to record the chosen RadioButton but I can't help but think I'm missing a trick here? Can anyone offer up a better/neater solution?
To restore a RadioButtonList to the saved selected value I believe you need to set the RadioButtonList.SelectedIndex property of the list, not the Selected property of the ListItem

how to bind drop down list at edit item template Edit mode occurs in grid view

i just want to bind two drop down list dynamically when grid view in row editing mode. here i declares one code block that dynamically fetches that row state and binds up those two drop down list.
here is code :
protected void GV_ViewCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
...............
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DropDownList dl = (DropDownList)e.Row.FindControl("DDL_Types1");
dl.DataSource = db.PartyTypes.Select(t => t).ToList();
dl.DataBind();
dl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "type_id").ToString();
DropDownList dl1 = (DropDownList)e.Row.FindControl("DDL_CountryNames1");
dl1.DataSource = db.Countries.Select(c => c).ToList();
if (!string.IsNullOrEmpty(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))
{
dl1.SelectedValue = DataBinder.Eval(e.Row.DataItem, "country_id").ToString();
DropDownList dl2 = (DropDownList)e.Row.FindControl("DDL_StateNames1");
dl2.DataSource = db.States.Where(s => s.country_id.Equals(int.Parse(DataBinder.Eval(e.Row.DataItem, "country_id").ToString()))).Select(s => s).ToList();
dl2.DataBind();
}
DataRowView rowView1 = (DataRowView)e.Row.DataItem;
if (rowView1["UserOFC"] != null)
{
(e.Row.FindControl("chk_UserOFC1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserOFC").ToString());
}
if (rowView1["UserVAT"] != null)
{
(e.Row.FindControl("chk_UserVAT1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserVAT").ToString());
}
if (rowView1["UserINV"] != null)
{
(e.Row.FindControl("chk_UserINV1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserINV").ToString());
}
if (rowView1["UserNone"] != null)
{
(e.Row.FindControl("chk_UserNone1") as CheckBox).Checked = Convert.ToBoolean(e.Row.DataItem.Equals("UserNone").ToString());
}
}
}
..................
}
Is this right method to binds drop down list at row editing mode.
please help me....
It's fine.But there are some other ways.

Convert gridview field into Dropdownlist

i need to convert a field in gridview to a dropdownlist,
but i need to do this in codebehind, and I cannot add a templatefield in apsx(but it could be created at run time execution...)
I populate my grid with this code:
foreach (var item in response.Select(x => x.idMatriz).Distinct())
{
dr = dt.NewRow();
for (int i = 0; i < colunas; i++)
{
dr[i] = response.Where(x => x.Propriedade == dt.Columns[i].ToString() && x.idMatriz == item).Select(x => x.Valor).FirstOrDefault();
}
dt.Rows.Add(dr);
}
It works but i need this fileds be a dropdown....
any help?
It looks like all you need to do is dynamically create a template field and add it to the gridview.
var field = new TemplateField {HeaderText = col.ColumnName}
gridView.Columns.Add(field);
After that, on the row created event of the gridview create and wire up the dropdown.
public void DynamicGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
var grid = sender as GridView;
if (grid == null)
{
return;
}
for (var i = 0; i < grid.Columns.Count; i++)
{
var column = grid.Columns[i] as TemplateField;
if (column == null)
continue;
var cell = e.Row.Cells[i];
var dropdown = new DropDownList();
cell.Controls.Add(dropdown);
}
}

How to Fill Repeater ItemTemplate from Code Behind?

I have to create a repeater dynamically from code behind. I want to fill the item template of this repeater from code behind but I couldn't find any kind of object to synchronize with the item template of the repeater.
Code:
Repeater rpr = new Repeater();
rpr.ItemTemplate = ??
protected override void OnItemCreated(RepeaterItemEventArgs e)
{
base.OnItemCreated(e);
if (e.Item.DataItem != null && (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView dt = (DataRowView)e.Item.DataItem;
if (dt.DataView.Table.Columns["IsHeading"] != null)
{
if ((dt["IsHeading"].ToString()) == "true")
{
ItemHeaderContainer container = new ItemHeaderContainer();
ItemHeaderTemplate.InstantiateIn(container);
container.DataItem = e.Item.DataItem;
container.DataBind();
}
}
}
}
Refer:
http://www.neowin.net/forum/topic/658854-aspnet-repeaters-with-dynamic-itemtemplates/
You have to create instance of ITemplate. Read this example
http://www.codeproject.com/Articles/240760/Dynamically-create-item-templates-server-side

error when find div ingridview nested in datalist

I had datalist and in datalist gridview and in gridview div and I wanted to find this div I did my code but error apeared (object refrence....) here (Techgr1.Attributes.Add("Class", "ff");
)
protected void Datalist_Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
string LanguageID = Globals.GetSuitableLanguage(Page);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Techgr1 = e.Item.FindControl("TechnologyGr") as HtmlGenericControl;
}
GridView gridfeature = (GridView)e.Item.FindControl("grid_features");
foreach (DataControlField column in gridfeature.Columns)
{
column.HeaderText = Globals.Translate(column.HeaderText, LanguageID);
Techgr1.Attributes.Add("Class", "ff");
}
}
Try this:
protected void Datalist_Categories_ItemDataBound(object sender, DataListItemEventArgs e)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
string LanguageID = Globals.GetSuitableLanguage(Page);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Techgr1 = e.Item.FindControl("TechnologyGr") as HtmlGenericControl;
}
GridView gridfeature = (GridView)e.Item.FindControl("grid_features");
foreach (DataControlField column in gridfeature.Columns)
{
column.HeaderText = Globals.Translate(column.HeaderText, LanguageID);
if(Techgr1 != null)
{
Techgr1.Attributes.Add("Class", "ff");
}
}
}

Categories

Resources