从父页面填充Web用户控件的控件

时间:2011-12-06 12:08:40

标签: c# asp.net

我有两个网络用户控件,一个网格视图显示结果,另一个包含简单的文本框和基本创建的下拉列表来编辑gridview项目

aspx.cs页面代码是

GridView gView = PlaceHolder2.Controls[0].FindControl("grvBranches") as GridView;
int index = gView.SelectedIndex;

GridViewRow grd = gView.Rows[index];
Label lbl = (Label)grd.FindControl("lblID");

int rowid = 0;
int.TryParse(lbl.ToString(), out rowid);

在rowid中,我包含所选项目的ID

现在我想填写webusercontrol中的所有以下项目

<div class="col2_form1">
 <div class="form1_bar1">
    <div class="form1_txt1_div">
        <asp:Label ID="lblBranchName" runat="server" Text="Branch Name">
        </asp:Label>
    </div>
    <div class="form1_txtfield_div">
        <asp:TextBox runat="server" ID="txtBranchName" class="form1_TxtField" />
    </div>
    <div class="form1_txt2_div">
        <asp:RequiredFieldValidator ID="RFVBranchName" ErrorMessage="*" Display="Dynamic"
            ValidationGroup="Save" ControlToValidate="txtBranchName" runat="server" />
        <%--  <asp:Label Text="Error" ID="lblBranchNameError" runat="server" />--%>
    </div>
</div>
<div class="form1_bar2">
    <div class="form1_txt1_div">
        <asp:Label Text=" Organization Name" ID="lblOrganizationName" runat="server" />
    </div>
    <div class="form1_txtfield_div">
        <asp:DropDownList ID="ddlOrganization" CssClass="form1_TxtField" runat="server">
        </asp:DropDownList>
    </div>
    <div class="form1_txt2_div">
        <%--<asp:Label Text="Error" ID="lblOrganizationNameError" runat="server" />--%>
    </div>
</div>
<div class=" form1_bar1">
    <div class="form1_txt1_div ">
        <asp:Label Text="Address Line1" ID="lblAddresLine1" runat="server" />
    </div>
    <div class=" form1_txtfield_div">
        <asp:TextBox runat="server" ID="txtAddresLine1" CssClass="form1_TxtField" />
    </div>
    <div class="form1_txt2_div ">
        <asp:RequiredFieldValidator ErrorMessage="*" Display="Dynamic" ValidationGroup="Save"
            ControlToValidate="txtAddresLine1" runat="server" />
    </div>
</div>
<div class=" form1_bar2">
    <div class="form1_txt1_div ">
        <asp:Label Text="Address Line2" ID="lblAddresLine2" runat="server" />
    </div>
    <div class="form1_txtfield_div ">
        <asp:TextBox runat="server" ID="txtAddresLine2" CssClass="form1_TxtField" />
    </div>
    <div class="form1_txt2_div ">
    </div>
</div>
<div class="form1_bar1 ">
    <div class="form1_txt1_div">
        <asp:Label Text="Country" ID="lblCountry" runat="server" />
    </div>
    <div class="form1_txtfield_div " style="float: left;">
        <asp:DropDownList ID="ddlCountry" AutoPostBack="true" CssClass="form1_TxtField" runat="server"
            OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
        </asp:DropDownList>
    </div>
    <div class=" form1_txt2_div">
    </div>
</div>
<div class=" form1_bar2">
    <div class="form1_txt1_div ">
        <asp:Label Text="State" ID="lblState" runat="server" />
    </div>
    <div class="form1_txtfield_div ">
        <asp:DropDownList ID="ddlState" AutoPostBack="true" CssClass="form1_TxtField" runat="server"
            OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
        </asp:DropDownList>
    </div>
    <div class="form1_txt2_div ">
    </div>
</div>
<div class="form1_bar1 ">
    <div class="form1_txt1_div ">
        <asp:Label Text="City" ID="lblCity" runat="server" />
    </div>
    <div class="form1_txtfield_div ">
        <asp:DropDownList runat="server" CssClass="form1_TxtField" ID="ddlCity">
        </asp:DropDownList>
    </div>
    <div class="form1_txt2_div ">
    </div>
</div>
<div class="form1_bar2 ">
    <div class="form1_txt1_div ">
        &nbsp;
    </div>
    <div class="form1_txtfield_div">
        <asp:Button Text="Save" Width="100px" runat="server" ID="btnSave" ValidationGroup="Save"
            OnClick="btnSave_Click" />
    </div>
    <div class="form1_txt2_div">
        <asp:Label Text="" ID="lblMsg" Visible="false" runat="server" />
    </div>
</div>

我在webusercontrol上有一个属性

public string ABC_DEF
{
   set  {   txtABC.Text = value;       }
   get  {   return txtABC.Text;       }
}

并在父页面上,即在aspx页面上,我将其称为

if (ViewState["controlname"] != null)
{
  PlaceHolder pl = MainPanel.FindControl(ViewState["controlname"].ToString()) as PlaceHolder;
  pl.Controls.RemoveAt(0);
}
Control uc = (Control) Page.LoadControl("~/usercontrols/control_forms/branch_aspx_form.ascx");

ViewState["path"] = "~/usercontrols/control_forms/branch_aspx_form.ascx";
ViewState["controlname"] = "PlaceHolder1";
PlaceHolder1.Controls.Add(uc);


branch_view[] branch_to_edit = EntityService.ServiceInstance.GetBranchByBranchID(row_id);

foreach (branch_view item in branch_to_edit)
{
       usercontrols.control_forms.branch_aspx_form branch_control = 
                 new usercontrols.control_forms.branch_aspx_form();
      branch_control.ABC_DEF = " hello";  // Call property here
}

感谢 Gurbax

1 个答案:

答案 0 :(得分:1)

在您的用户控件.ascx文件中,为您需要拉到用户控件上的每个字段创建属性,

然后您可以通过指定UserControlId.Property语法从.aspx页面访问它们,如下所示:

public string Name
{
    get { return txtName.Text; }
    set { txtName.Text = value; }
}

public string Surname
{
    get { return txtSurname.Text; }
    set { txtSurname.Text = value; }
}

在.aspx页面上,可以像这样访问属性:

protected void PopulateUserControl_Click(object sender, EventArgs e)
{
    userControlName.Name = "Bob";
    userControlName.Surname = "The Builder";
}