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

本文关键字:类库 更新 WebControls 单独 | 更新日期: 2023-09-27 17:58:12

需要从单独的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);
    }
    }

在Library类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类中的文本框值。

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

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

ASP.NET向其他类公开控件