I am trying to bind a repeater with a List Object whch contains n number of (same) user control. I was able to see the values which I wish to assign to the user control throught watchlist while debuging, however I am unable to get these values to display.
- i.e. I am getting a repeater with user control that has empty fields
While the values seem to have been passed to the user control objects in the list I am not able to extract those values in the page_load method in the user control page, thus can't assign them ( or have been assigning blanks) to the control on the page.
below are some code snips to show
====> Here I am binding the rpeater with a list
====> the list contains a list of pricestack objs
rpt_ItemList.DataSource = my_PriceStack_List;
rpt_ItemList.DataBind();
====> below is the code for the user control
====> I seem to be getting blanks for all the varibles
public partial class PriceStack : System.Web.UI.UserControl {
In which event are you assigning the value to the user control from your aspx page. Go through the event sequence. If you are assigning the values to the user control in page load then I would like to inform that the Page_Load event of user control will
be fired first and then the Page_Load event of the page will be fired.
You can try to use OnPreRender method of your user control for the assignments.
Many thanks for the response. I actually have tried both Page_Load and OnPreRender, but in both instance I am getting NullReferenceException for the textbox. Below is the code I have in the user control. (as mentioned in my previous posting I am getting
the expected value for strTitle)
How above variables are assigned. I mean, hwo str_Title will have some value? I am sure, you must be assigning it from your aspx page. Can you post that piece of code as well?
Many thanks for the response. I followed Mike's example and created a delegate method for PreRender. However I still seem to be getting a Null reference for my Txt controls when try to assign values to then. Could y advice?
TWnzUS
Member
1 Points
48 Posts
Binding Repeater to a List ; Assigning varible values to user control
Aug 11, 2009 07:44 PM|LINK
Hi All:
I am trying to bind a repeater with a List Object whch contains n number of (same) user control. I was able to see the values which I wish to assign to the user control throught watchlist while debuging, however I am unable to get these values to display. - i.e. I am getting a repeater with user control that has empty fields
While the values seem to have been passed to the user control objects in the list I am not able to extract those values in the page_load method in the user control page, thus can't assign them ( or have been assigning blanks) to the control on the page. below are some code snips to show
====> Here I am binding the rpeater with a list
====> the list contains a list of pricestack objs
rpt_ItemList.DataSource = my_PriceStack_List;
rpt_ItemList.DataBind();
====> below is the code for the user control
====> I seem to be getting blanks for all the varibles
public partial class PriceStack : System.Web.UI.UserControl {
public int int_ItemID; public string str_Image;
public string str_Title;
public decimal des_DS_Price;
public DateTime dt_Start_time;
public DateTime dt_End_time;
public DataSet ds_OpenInterest;
public string str_Img_Folder;
protected void Page_Load(object sender, EventArgs e) {
Txt_Title.Text = str_Title; txt_DS_Price.Text = des_DS_Price.ToString();
txt_AC_Period.Text = dt_Start_time.ToString() + " ~ " +dt_End_time.ToString();
img_item.ImageUrl = @"~/Image/" + str_Img_Folder + "/" + str_Image; OI_List = new PriceStack_PriceList();
OI_List.ds_OpenInterest = ds_OpenInterest; }
}
Please advice,
Thanks a million,
NihirPorecha
Contributor
3360 Points
611 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 12, 2009 08:37 AM|LINK
In which event are you assigning the value to the user control from your aspx page. Go through the event sequence. If you are assigning the values to the user control in page load then I would like to inform that the Page_Load event of user control will be fired first and then the Page_Load event of the page will be fired.
You can try to use OnPreRender method of your user control for the assignments.
My blog
TWnzUS
Member
1 Points
48 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 13, 2009 01:16 PM|LINK
Hi There:
Many thanks for the response. I actually have tried both Page_Load and OnPreRender, but in both instance I am getting NullReferenceException for the textbox. Below is the code I have in the user control. (as mentioned in my previous posting I am getting the expected value for strTitle)
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Txt_Title.Text = str_Title;
}
Please advice.
thanks a million,
NihirPorecha
Contributor
3360 Points
611 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 13, 2009 01:24 PM|LINK
How above variables are assigned. I mean, hwo str_Title will have some value? I am sure, you must be assigning it from your aspx page. Can you post that piece of code as well?
My blog
TWnzUS
Member
1 Points
48 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 13, 2009 02:00 PM|LINK
====> Here I am creating the price stack usercontrol objs and adding them to a list before binding them to the repeater
// CREATE NEW PRICE STACK OBJ
ps_Temp = get_New_Price_Stack(Int32.Parse(rdr["ItemID"].ToString()), rdr["Title"].ToString(), rdr["Image"].ToString(), rdr["Image_Folder"].ToString().Trim(), decimal.Parse(rdr["DS_Price"].ToString()), DateTime.Parse(rdr["Start_Time"].ToString()), DateTime.Parse(rdr["End_Time"].ToString()), get_new_OI_dataset());
//ADD PRICE STACK TO LIST
my_PriceStack_List.Add(ps_Temp); rpt_ItemList.DataSource = my_PriceStack_List; rpt_ItemList.DataBind();
===> below is the code for the get_New_Price_Stack where I create a new pricestack obj and asign values to the public varibles
private PriceStack get_New_Price_Stack( int ItemID, string Title, string Image , string Image_Folder, Decimal DS_Price, DateTime start_time, DateTime end_time, DataSet ds_OpenInterest)
{
PriceStack ps_Temp = null; ps_Temp = new PriceStack();
ps_Temp.int_ItemID = ItemID; ps_Temp.str_Title = Title;
ps_Temp.str_Image = Image; ps_Temp.str_Img_Folder = Image_Folder;
ps_Temp.des_DS_Price = DS_Price;
ps_Temp.dt_Start_time = start_time;
ps_Temp.dt_End_time = end_time;
ps_Temp.ds_OpenInterest = ds_OpenInterest;
return ps_Temp; }
===== > below is my code for hte PriceStack user control public partial
class PriceStack : System.Web.UI.UserControl {
public int int_ItemID;
public string str_Image;
public string str_Title;
public decimal des_DS_Price;
public DateTime dt_Start_time;
public DateTime dt_End_time;
public DataSet ds_OpenInterest;
public string str_Img_Folder;
protected void Page_Load(object sender, EventArgs e) { }
protected override void OnPreRender(EventArgs e)
{ base.OnPreRender(e); Txt_Title.Text = str_Title; }
}
TWnzUS
Member
1 Points
48 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 13, 2009 02:00 PM|LINK
====> Here I am creating the price stack usercontrol objs and adding them to a list before binding them to the repeater
// CREATE NEW PRICE STACK OBJ
ps_Temp = get_New_Price_Stack(Int32.Parse(rdr["ItemID"].ToString()), rdr["Title"].ToString(), rdr["Image"].ToString(), rdr["Image_Folder"].ToString().Trim(), decimal.Parse(rdr["DS_Price"].ToString()), DateTime.Parse(rdr["Start_Time"].ToString()), DateTime.Parse(rdr["End_Time"].ToString()), get_new_OI_dataset());
//ADD PRICE STACK TO LIST
my_PriceStack_List.Add(ps_Temp);
rpt_ItemList.DataSource = my_PriceStack_List;
rpt_ItemList.DataBind();
===> below is the code for the get_New_Price_Stack where I create a new pricestack obj and asign values to the public varibles
private PriceStack get_New_Price_Stack( int ItemID, string Title, string Image , string Image_Folder, Decimal DS_Price, DateTime start_time, DateTime end_time, DataSet ds_OpenInterest)
{
PriceStack ps_Temp = null; ps_Temp = new PriceStack();
ps_Temp.int_ItemID = ItemID; ps_Temp.str_Title = Title;
ps_Temp.str_Image = Image; ps_Temp.str_Img_Folder = Image_Folder;
ps_Temp.des_DS_Price = DS_Price;
ps_Temp.dt_Start_time = start_time;
ps_Temp.dt_End_time = end_time;
ps_Temp.ds_OpenInterest = ds_OpenInterest;
return ps_Temp; }
===== > below is my code for hte PriceStack user control public partial
class PriceStack : System.Web.UI.UserControl {
public int int_ItemID;
public string str_Image;
public string str_Title;
public decimal des_DS_Price;
public DateTime dt_Start_time;
public DateTime dt_End_time;
public DataSet ds_OpenInterest;
public string str_Img_Folder;
protected void Page_Load(object sender, EventArgs e) { }
protected override void OnPreRender(EventArgs e)
{ base.OnPreRender(e);
Txt_Title.Text = str_Title; }
}
Qin Dian Tan...
All-Star
104600 Points
11584 Posts
Microsoft
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 17, 2009 09:23 AM|LINK
Hi TWnzUS,
Have a look at the following links about how to bind user control in Repeater control:
http://www.c-sharpcorner.com/UploadFile/Mike%20Clark/PartA02272007012412AM/PartA.aspx
http://stackoverflow.com/questions/979840/asp-net-bind-a-value-to-a-custom-user-control-inside-a-repeater
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
TWnzUS
Member
1 Points
48 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 17, 2009 11:53 AM|LINK
Hi Dian:
Many thanks for the response. I followed Mike's example and created a delegate method for PreRender. However I still seem to be getting a Null reference for my Txt controls when try to assign values to then. Could y advice?
protected void Page_Load(object sender, EventArgs e)
{ this.PreRender += new EventHandler(PriceStack_PreRender); }
void PriceStack_PreRender(object sender, EventArgs e)
{ Txt_Title.Text = str_Title; }
thanks a million,
Qin Dian Tan...
All-Star
104600 Points
11584 Posts
Microsoft
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 18, 2009 02:46 AM|LINK
Hi TWnzUS,
Where is the TextBox, is it in other control? Double check the ID. Please show your user control.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
TWnzUS
Member
1 Points
48 Posts
Re: Binding Repeater to a List ; Assigning varible values to user control
Aug 18, 2009 03:35 AM|LINK
====> Here I am creating the price stack usercontrol objs and adding them to a list before binding them to the repeater
// CREATE NEW PRICE STACK OBJ
ps_Temp = get_New_Price_Stack(Int32.Parse(rdr["ItemID"].ToString()), rdr["Title"].ToString(), rdr["Image"].ToString(), rdr["Image_Folder"].ToString().Trim(), decimal.Parse(rdr["DS_Price"].ToString()), DateTime.Parse(rdr["Start_Time"].ToString()), DateTime.Parse(rdr["End_Time"].ToString()), get_new_OI_dataset());
//ADD PRICE STACK TO LIST
my_PriceStack_List.Add(ps_Temp);
rpt_ItemList.DataSource = my_PriceStack_List;
rpt_ItemList.DataBind();
===> below is the code for the get_New_Price_Stack where I create a new pricestack obj and asign values to the public varibles
private PriceStack get_New_Price_Stack( int ItemID, string Title, string Image , string Image_Folder, Decimal DS_Price, DateTime start_time, DateTime end_time, DataSet ds_OpenInterest)
{
PriceStack ps_Temp = null; ps_Temp = new PriceStack();
ps_Temp.int_ItemID = ItemID; ps_Temp.str_Title = Title;
ps_Temp.str_Image = Image; ps_Temp.str_Img_Folder = Image_Folder;
ps_Temp.des_DS_Price = DS_Price;
ps_Temp.dt_Start_time = start_time;
ps_Temp.dt_End_time = end_time;
ps_Temp.ds_OpenInterest = ds_OpenInterest;
return ps_Temp; }
===== > below is my code for hte PriceStack user control public partial
public partial class PriceStack : System.Web.UI.UserControl
{
public int int_ItemID;
public string str_Image;
public string str_Title;
public decimal des_DS_Price;
public DateTime dt_Start_time;
public DateTime dt_End_time;
public DataSet ds_OpenInterest;
public string str_Img_Folder;
protected void Page_Load(object sender, EventArgs e)
{
this.PreRender += new EventHandler(PriceStack_PreRender);
}
void PriceStack_PreRender(object sender, EventArgs e)
{
base.OnPreRender(e);
Txt_Title.Text = str_Title;
}
}
</div></div>