动态添加Web部件中的控件 [英] add control in web parts dynamically

查看:80
本文介绍了动态添加Web部件中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我必须动态创建Webpart并在该Web部件中设置控件.

但是,当我在控件中设置该值时,则不会设置.它显示了空白控件.

这是我的代码

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.

推荐答案

此处解决方案,

使用Control类设置值.
控件UserCont = wpm.AddWebPart(gwp,Zone1,1).Controls [0];


这里是代码.


公共无效CreateWp(int Cnt)
{
WebPartManager wpm =(WebPartManager)WebPartManager.GetCurrentWebPartManager(this.Page);
ClsCalendar objclsCalendar =新的ClsCalendar();
DataSet ds = new DataSet();
InSync.DataAccess.BlueStar db =新的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"])));
如果(dr!= null)
{
如果(!CheckWebpartExist(dr.LastName +" + dr.FirstName +" + dr.MiddleName))
{

UserControl uc =(UserControl)LoadControl(〜/Includes/CalendarControl.ascx");
uc.ID ="userControl_" + i;
类型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;
控件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();


}
}
}
}
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();


}
}
}
}


这篇关于动态添加Web部件中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆