Click here to Skip to main content
16,016,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have to create webparts dynamically and set control in that web part.

But when i set the value in control then it not set. it show me blank control.

here my code
WebPartManager wpm = (WebPartManager)WebPartManager.GetCurrentWebPartManager(this.Page);
for (int i = 0; i < 5; i++)
 {
  TextBox txtTest = new TextBox();
  txtTest.ID = "txtTest" + i.ToString();
  txtTest.Text = i.ToString()+"_Value";//its value not show on page                 
GenericWebPart gwp = wpm.CreateWebPart(txtTest);
 wpm.AddWebPart(gwp, Zone1, 1);
         }
            
Html text


  <asp:WebPartZone ID="Zone1" runat="server" Width="100%" WebPartVerbRenderMode="TitleBar"
   HeaderText="Left Zone" BorderWidth="0px" LayoutOrientation="Horizontal" MenuLabelText="MenuLatelText">
                            <closeverb visible="false" description="" text=""></closeverb>
                            <minimizeverb visible="true" text="" imageurl="~/Images/inner/minus.gif"></minimizeverb>
                            <restoreverb imageurl="~/Images/inner/plus.gif"></restoreverb>
                            <deleteverb visible="false" text="" />
                            <zonetemplate>
                            </zonetemplate>
                            <TitleBarVerbStyle Font-Size="Smaller" Font-Bold="False"></TitleBarVerbStyle>  

Please give me some solutions.
Posted
Updated 20-Jul-11 2:38am
v5

1 solution

Here solution,

Set value using Control class.
Control UserCont = wpm.AddWebPart(gwp, Zone1, 1).Controls[0];


Here the code.


public void CreateWp(int Cnt)
{
WebPartManager wpm = (WebPartManager)WebPartManager.GetCurrentWebPartManager(this.Page);
ClsCalendar objclsCalendar = new ClsCalendar();
DataSet ds = new DataSet();
InSync.DataAccess.BlueStar db = new InSync.DataAccess.BlueStar();
ds = objclsCalendar.GetResources(ddlProfiles.SelectedItem.Text, int.Parse(Session["UserId"].ToString()), int.Parse(Session["FacilityID"].ToString()), int.Parse(Session["PracticeID"].ToString()));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ResourceDetailsRow dr = db.ResourceDetailsCollection.GetByPrimaryKey(Convert.ToInt32(ds.Tables[0].Rows[i]["ResourceID"]));
if (dr != null)
{
if (!CheckWebpartExist(dr.LastName + " " + dr.FirstName + " " + dr.MiddleName))
{

UserControl uc = (UserControl)LoadControl("~/Includes/CalendarControl.ascx");
uc.ID = "userControl_" + i;
Type ucType = uc.GetType();
PropertyInfo ucResourceIdProperty = ucType.GetProperty("ResourceId");
ucResourceIdProperty.SetValue(uc, dr.ResourceId.ToString() , null);
PropertyInfo ucScheduleSetupIdProperty = ucType.GetProperty("ScheduleSetupId");
ucScheduleSetupIdProperty.SetValue(uc, ds.Tables[0].Rows[i]["ScheduleSetupId"].ToString(), null);
GenericWebPart gwp = wpm.CreateWebPart(uc);
gwp.Title = dr.LastName + " " + dr.FirstName + " " + dr.MiddleName;
Control UserCont = wpm.AddWebPart(gwp, Zone1, 1).Controls[0];
HiddenField hidenResourceID = (HiddenField)UserCont.FindControl("hidenResourceID");
hidenResourceID.Value = Convert.ToString(dr.ResourceId);
HiddenField hidenScheduleSetupId = (HiddenField)UserCont.FindControl("hidenScheduleSetupId");
hidenScheduleSetupId.Value = ds.Tables[0].Rows[i]["ScheduleSetupId"].ToString();


}
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900