如何从单独的C#类库中更新WebControl

时间:2013-02-07 09:25:23

标签: c#

需要从单独的c#类库更新Web控件。例如,必须从类库中更新网格数据源。

在WebForm1.aspx中,我有以下代码

<body>
<form id="form1" runat="server">
<div>
    <table>
    <tr>
        <td>
            <div>
              <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td>Search</td>
                                        <td>
                                           <span>
                                           <asp:TextBox ID="txtSearchBox" EnableTheming="false" AutoPostBack="True"  
                                                runat="server" ontextchanged="txtSearchBox_TextChanged" ></asp:TextBox>
                                           </span>
                                           <span>
                                           <asp:Button ID="btnPre" runat="server" Text="<" CssClass="btnPrevious" 
                                                EnableTheming="false" onclick="btnPre_Click" />
                                           </span>  
                                           <span>
                                               <asp:TextBox ID="txtPage_Index" EnableTheming="false" CssClass="textboxTiny" AutoPostBack="True"  
                                                runat="server" ontextchanged="txtPage_Index_TextChanged" ></asp:TextBox></span>
                                               <span>
                                            <asp:Button ID="btnNext" runat="server" Text=">" CssClass="btnNext" 
                                                EnableTheming="false" onclick="btnNext_Click" />
                                         </span>
                                        </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                   <td colspan="3" style="margin-left: 40px">  
                     <asp:GridView ID="grdSource" runat="server" class="grid" Width="100%" 
                    AutoGenerateColumns="False" AllowPaging="true">
                    <Columns>
                        <asp:TemplateField>
                            <ItemStyle Width="20px" />
                            <ItemStyle />
                            <ItemTemplate>
                                   <asp:CheckBox ID="chkSelectFeesType" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="name" HeaderText="Name"/>
                   </Columns>
                    <HeaderStyle CssClass="gridheader" />
                    <RowStyle CssClass="gridrow" />
               </asp:GridView>
                   </td>
               </tr>

             </table>
            </div>
        </td>
    </tr>

</table>
</div>
</form>

在背后的代码中

public partial class WebForm1 : System.Web.UI.Page
{
    BO.Search search = new Search();
    List<Employee> employees = new List<Employee>();
    System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as   
                                                                System.Web.UI.Page;

    protected void Page_Load(object sender, EventArgs e)
    {

        System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;

        employees.Add(new BO.Employee("AAA", 01));
        employees.Add(new BO.Employee("BBB", 02));
        employees.Add(new BO.Employee("CCC", 03));
        employees.Add(new BO.Employee("DDD", 04));
        employees.Add(new BO.Employee("EEE", 05));

    }

    protected void btnPre_Click(object sender, EventArgs e)
    {
       search.PageSearch(ref grdSource, employees, ref page);
    }
    protected void txtPageIndex_TextChanged(object sender, EventArgs e)
    {
        search.PageSearch(ref grdSource, employees, ref page);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        search.PageSearch(ref grdSource, employees, ref page);
    }
    }

在类库Search.cs

public class Search
{


    public void PageSearch<T>(ref GridView grdSource, List<T> list, ref Page page)
    {
        TextBox txtSearchBox = (TextBox)page.FindControl("txtSearchBox");
        TextBox txtPage_Index = (TextBox)page.FindControl("txtPage_Index");

        if (grdSource.PageIndex > 0)
        {
            if (txtSearchBox.Text.Length > 0)
            {
                grdSource.DataSource = list.FindAll(i => i.Equals(txtSearchBox.Text.ToUpper()));
            }
            else
            {
                grdSource.DataSource = list;
            }
            grdSource.PageIndex = grdSource.PageIndex - 1;
        }
        grdSource.DataSource = list;
        grdSource.DataBind();

        txtPage_Index.Text = grdSource.PageIndex.ToString();
    }
}

我在多个表单中具有相同的搜索和分页功能。所以我创建了PageSearch()函数来适用于所有页面。但问题是我无法绑定来自search.cs类的gird视图或文本框值。

1 个答案:

答案 0 :(得分:0)

通过以下链接了解如何将控件公开给其他类,这可能是你想要的:

ASP.NET expose controls to other class