简体   繁体   中英

How to UpDate WebControls from separate C# Class Library

Need to update the Web controls from a separate c# class library. For example grid data source has to be updated from the class library.

In WebForm1.aspx the I have the following code

<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>

In the code behind

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

In the class Library class 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();
    }
}

I have same search and paging functionality in more than one form. So I created the PageSearch() function to work for all pages. But the problem is I am not able to bind the gird view or the text boxes values from the search.cs class.

Go through the following link on how to expose controls to other classes, this is probably what you want:

ASP.NET expose controls to other class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM