加载aspx页面,该页面包括包含Updatepanel的母版页中的listview [英] load aspx page which is include listview in Masterpage that contain Updatepanel

查看:89
本文介绍了加载aspx页面,该页面包括包含Updatepanel的母版页中的listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在使用ASP.net和C#开发模板.
我的模板包括3部分,我想部分刷新,这就是为什么我使用updatepanel的原因. 在此模板中,一旦用户单击顶部菜单,它将刷新侧面菜单,然后,当用户单击侧面菜单时,它将刷新中间内容页面.
在这种情况下,我将使用母版页,因为我将在中间内容页面加载aspx页面.根据我的代码,正常页面很容易在此部分加载,但是当我要加载包含listview的页面时:

actually I''m developing a template using ASP.net and C#.
my template include 3 part and i want to have partial refreshing, that''s why i''m using updatepanel.
in this template once the user click at the top menu it will refresh the side menu then when the user click on the side menu it will refresh the middle content page.
i''m using masterpage in this scenario because i''m going to load aspx page at the middle content page. based on my code the normal page easily load at this part but when i want to load the page which is include listview:

<%@ Page Title="" Language="C#" MasterPageFile="~/MainMasterPage.master" AutoEventWireup="true" CodeFile="ContentPage1.aspx.cs" Inherits="ContentPage1" %>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server" Visible="true">
<asp:Panel ID="ContentPanel" runat="server" Visible="true">
    <fieldset style=" height:350px;">
        <legend>Details</legend>
        <asp:Label ID="TestLable" runat="server" Text="This is a test"></asp:Label>
        <!--------- ListView Block --------->
        <asp:ListView ID="lvEmployee" runat="server"
        onitemediting="lvEmployee_ItemEditing"
        onitemupdating="lvEmployee_ItemUpdating"
        onitemcanceling="lvEmployee_ItemCanceling"
        onitemdeleting="lvEmployee_ItemDeleting"
        InsertItemPosition="LastItem"
        OnSorting="lvEmployee_Sorting"
        oniteminserting="lvEmployee_ItemInserting"
        DataKeyNames="EmpID" >

        <LayoutTemplate>
        <!---- Layout Template: is the place to design the form layout ---->
                <table id="Table1"  runat="server">
                    <tr id="Tr1"  runat="server">
                        <td id="Td1"  runat="server">
                            <table ID="itemPlaceholderContainer"  runat="server" border="1" style="background-color: #DCDCDC;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr id="Tr2"  runat="server" style="">
                                    <th id="Th1"  runat="server">
                                    </th>
                                    <th id="Th2"  runat="server">
                                        EmpID
                                        <asp:ImageButton ID="imEmpID" CommandArgument="EmpID" CommandName="Sort" ImageUrl="~/img/asc.png" runat="server" />
                                    </th>
                                    <th id="Th3"  runat="server">
                                        EmpName
                                        <asp:ImageButton ID="imEmpName" CommandArgument="EmpName" CommandName="Sort" ImageUrl="~/img/asc.png" runat="server" />
                                    </th>
                                    <th id="Th4"  runat="server">
                                        Department
                                    </th>
                                    <th id="Th5"  runat="server">
                                        Age
                                    </th>
                                    <th id="Th6"  runat="server">
                                        Address
                                    </th>
                                </tr>
                                <tr ID="itemPlaceholder"  runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr id="Tr3"  runat="server">
                        <td id="Td2"  runat="server" style="text-align: center;background-color: #CCCCCC;">
                            <asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You received the following errors:" ShowMessageBox="true" ShowSummary="false" ValidationGroup="VGEditTmp" />
                            <asp:ValidationSummary ID="ValidationSummary2" runat="server" HeaderText="You received the following errors:" ShowMessageBox="true" ShowSummary="false" ValidationGroup="VGInsertTmp" />
                        </td>
                    </tr>
                </table>
                <br />
                <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvEmployee" PageSize="5" onprerender="DataPager1_PreRender">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="true" ShowLastPageButton="true"/>
                        <asp:TemplatePagerField>
                            <PagerTemplate>
                                <span style="color:Maroon;">Records:
                                    <%# Container.StartRowIndex >= 0 ? (Container.StartRowIndex + 1) : 0 %>
                                    -
                                    <%# (Container.StartRowIndex + Container.PageSize) > Container.TotalRowCount ? Container.TotalRowCount : (Container.StartRowIndex + Container.PageSize)%>
                                    of
                                    <%# Container.TotalRowCount %>
                                    <span style="color:Red;">
                                    OR
                                    </span>
                                    <span style="color: Blue;">
                                    Page
                                    <%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>
                                    of
                                    <%# Math.Ceiling((double)Container.TotalRowCount / Container.PageSize) %>
                                    </span>
                                </span>
                            </PagerTemplate>
                        </asp:TemplatePagerField>
                    </Fields>
                </asp:DataPager>
            </LayoutTemplate>

            <ItemTemplate>
            <!---- Item Template: is the place to design how to show the items inside the form  ---->
                <tr style="background-color:#FFF8DC;color: #000000;">
                    <td>
                        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this Employee Details?');" />
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="false" />
                    </td>
                    <td>
                        <asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("EmpID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="EmpNameLabel" runat="server" Text='<%# Eval("EmpName") %>' />
                    </td>
                    <td>
                        <asp:Label ID="DepartmentLabel" runat="server" Text='<%# Eval("Department") %>' />
                    </td>
                    <td>
                        <asp:Label ID="AgeLabel" runat="server" Text='<%# Eval("Age") %>' />
                    </td>
                    <td>
                        <asp:Label ID="AddressLabel" runat="server" Text='<%# Eval("Address") %>' />
                    </td>
                </tr>
            </ItemTemplate>

            <EditItemTemplate>
            <!---- Edit Item Template: is the place to design the edit form  ---->
                <tr style="background-color:#008A8C;color: #FFFFFF;">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" ValidationGroup="VGEditTmp" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
                    </td>
                    <td>
                        <asp:Label ID="EmpIDLabel1" runat="server" Text='<%# Eval("EmpID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>' ValidationGroup="VGEditTmp" />
                        <asp:RequiredFieldValidator runat="server" ID="RequiredName" ControlToValidate="EmpNameTextBox" ErrorMessage="Name is required" ValidationGroup="VGEditTmp" Display="None" ></asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="regName" runat="server" ControlToValidate="EmpNameTextBox" ValidationExpression="^[a-zA-Z'.\s]{1,50}" ErrorMessage="Enter a valid Name" ValidationGroup="VGEditTmp" Display="None"></asp:RegularExpressionValidator>
                    </td>
                    <td>
                        <asp:TextBox ID="DepartmentTextBox" runat="server" Text='<%# Bind("Department") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' ValidationGroup="VGEditTmp" />
                        <asp:RangeValidator ID="ValidateAge" runat="server" ControlToValidate="AgeTextBox" MinimumValue="18" MaximumValue="50" Type="Integer" ErrorMessage="Age should be within the valid interval" ValidationGroup="VGEditTmp" SetFocusOnError="true" Display="None"></asp:RangeValidator>
                    </td>
                    <td>
                        <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
                    </td>
                </tr>
            </EditItemTemplate>

            <InsertItemTemplate>
            <!---- Insert Item Template: is the place to design the insert form  ---->
                <tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" ValidationGroup="VGInsertTmp" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" CausesValidation="false" />
                    </td>
                    <td>
                         
                    </td>
                    <td>
                        <asp:TextBox ID="EmpNameTextBox" runat="server" Text='<%# Bind("EmpName") %>' ValidationGroup="VGInsertTmp" />
                        <asp:RequiredFieldValidator runat="server" ID="RequiredName" ControlToValidate="EmpNameTextBox" ErrorMessage="Name is required" ValidationGroup="VGInsertTmp" Display="None"></asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="regName" runat="server" ControlToValidate="EmpNameTextBox" ValidationExpression="^[a-zA-Z'.\s]{1,50}" ErrorMessage="Enter a valid Name" ValidationGroup="VGInsertTmp" Display="None"></asp:RegularExpressionValidator>
                    </td>
                    <td>
                        <asp:TextBox ID="DepartmentTextBox" runat="server" Text='<%# Bind("Department") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="AgeTextBox" runat="server" Text='<%# Bind("Age") %>' ValidationGroup="VGInsertTmp" />
                        <asp:RangeValidator ID="ValidateAge" runat="server" ControlToValidate="AgeTextBox" MinimumValue="18" MaximumValue="50" Type="Integer" ErrorMessage="Age should be within the valid interval" ValidationGroup="VGInsertTmp" SetFocusOnError="true" Display="None"></asp:RangeValidator>
                    </td>
                    <td>
                        <asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
        </asp:ListView>
            <asp:HiddenField ID="hid_UpdateQT" Value="" runat="server" />
            <asp:HiddenField ID="hid_SortExpression" Value="" runat="server" />

    </fieldset>
</asp:Panel>
</asp:Content>


it is not showing the page. my code behind of this listview page is as below:


it is not showing the page. my code behind of this listview page is as below:

protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
            BindLV("");
        }
    }

    public DataTable GetEmployee(string query)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlDataAdapter ada = new SqlDataAdapter(query, con);
        DataTable dtEmp = new DataTable();
        ada.Fill(dtEmp);
        return dtEmp;
    }

    private void BindLV(string SortExpression)
    {
        string UpdateQuery = "Select * from Employee" + SortExpression;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        hid_UpdateQT.Value = UpdateQuery;

        lvEmployee.Items.Clear();
        lvEmployee.DataSource = GetEmployee(UpdateQuery);
        lvEmployee.DataBind();
    }

    protected void lvEmployee_Sorting(object sender, ListViewSortEventArgs e)
    {
        ImageButton imEmpID = lvEmployee.FindControl("imEmpID") as ImageButton;
        ImageButton imEmpName = lvEmployee.FindControl("imEmpName") as ImageButton;

        string DefaultSortIMG = "~/img/asc.png";
        string imgUrl = "~/img/desc.png";
        string sortExpression = hid_SortExpression.Value;

        if (!"".Equals(sortExpression))
        {
            if (e.SortExpression.Equals(sortExpression))
            {
                hid_SortExpression.Value = "";
                sortExpression = "";
                imgUrl = DefaultSortIMG;
            }
            else
            {
                hid_SortExpression.Value = e.SortExpression;
                sortExpression = e.SortExpression;
            }
        }
        else
        {
            hid_SortExpression.Value = e.SortExpression;
            sortExpression = e.SortExpression;
        }

        switch (e.SortExpression)
        {
            case "EmpID":
                if (imEmpName != null)
                    imEmpName.ImageUrl = DefaultSortIMG;

                if (imEmpID != null)
                    imEmpID.ImageUrl = imgUrl;

                break;

            case "EmpName":
                if (imEmpID != null)
                    imEmpID.ImageUrl = DefaultSortIMG;

                if (imEmpName != null)
                    imEmpName.ImageUrl = imgUrl;

                break;
        }
        BindLV(" order by " + e.SortExpression + " " + (!"".Equals(sortExpression) ? "ASC" : "DESC"));
    }

    protected void DataPager1_PreRender(object sender, EventArgs e)
    {
        //string strtmp = Request.Params["hid_UpdateQT"];
        string strtmp = hid_UpdateQT.Value;


        if (null == strtmp || "".Equals(strtmp))
        {
            strtmp = "Select * from Employee";
        }

        lvEmployee.DataSource = GetEmployee(strtmp);
        lvEmployee.DataBind();
    }

    protected void lvEmployee_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvEmployee.EditIndex = e.NewEditIndex;
    }

    protected void lvEmployee_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        string empid = "", name = "", dept = "", age = "", address = "";
        Label lbl = (lvEmployee.Items[e.ItemIndex].FindControl("EmpIDLabel1")) as Label;
        if (lbl != null)
            empid = lbl.Text;
        TextBox txt = (lvEmployee.Items[e.ItemIndex].FindControl("EmpNameTextBox")) as TextBox;
        if (txt != null)
            name = txt.Text;
        txt = (lvEmployee.Items[e.ItemIndex].FindControl("DepartmentTextBox")) as TextBox;
        if (txt != null)
            dept = txt.Text;
        txt = (lvEmployee.Items[e.ItemIndex].FindControl("AgeTextBox")) as TextBox;
        if (txt != null)
            age = txt.Text;
        txt = (lvEmployee.Items[e.ItemIndex].FindControl("AddressTextBox")) as TextBox;
        if (txt != null)
            address = txt.Text;
        string UpdateQuery = "UPDATE [Employee] SET [EmpName] = '" + name + "', [Department] = '" + dept + "', [Age] = '" + age + "', [Address] = '" + address + "' WHERE [EmpID] = '" + empid + "'";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand(UpdateQuery, con);
        com.ExecuteNonQuery();
        con.Close();
        lvEmployee.DataSource = GetEmployee("Select * from Employee");
        lvEmployee.DataBind();
        lvEmployee.EditIndex = -1;
    }

    protected void lvEmployee_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvEmployee.EditIndex = -1;
    }

    protected void lvEmployee_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        string empid = "";
        Label lbl = (lvEmployee.Items[e.ItemIndex].FindControl("EmpIDLabel")) as Label;
        if (lbl != null)
            empid = lbl.Text;
        string DeleteQuery = "Delete from Employee WHERE [EmpID] = '" + empid + "'";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand(DeleteQuery, con);
        com.ExecuteNonQuery();
        con.Close();
        lvEmployee.DataSource = GetEmployee("Select * from Employee");
        lvEmployee.DataBind();
        lvEmployee.EditIndex = -1;
    }

    protected void lvEmployee_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        string name = "", dept = "", age = "", address = "";
        TextBox txt = (e.Item.FindControl("EmpNameTextBox")) as TextBox;
        if (txt != null)
            name = txt.Text;
        txt = (e.Item.FindControl("DepartmentTextBox")) as TextBox;
        if (txt != null)
            dept = txt.Text;
        txt = (e.Item.FindControl("AgeTextBox")) as TextBox;
        if (txt != null)
            age = txt.Text;
        txt = (e.Item.FindControl("AddressTextBox")) as TextBox;
        if (txt != null)
            address = txt.Text;
        string INSERTQuery = "INSERT INTO [Employee] (EmpName,Department,Age,Address) VALUES ('" + name + "','" + dept + "','" + age + "','" + address + "')";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand(INSERTQuery, con);
        com.ExecuteNonQuery();
        con.Close();
        lvEmployee.DataSource = GetEmployee("Select * from Employee");
        lvEmployee.DataBind();
        lvEmployee.EditIndex = -1;
    }



and my masterpage code to load this page is as below:



and my masterpage code to load this page is as below:

<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
    <contenttemplate>

        <!-- Initial the Page which is going to show the details of the sub-menu  -->
        <asp:ContentPlaceHolder id="ContentPlaceHolder3" runat="server" Visible="false">
        

    </contenttemplate>

    <triggers>
        <asp:AsyncPostBackTrigger ControlID="Button1"     EventName="Click" /> 
        <asp:AsyncPostBackTrigger ControlID="Button2"     EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="Button3"     EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="Button4"     EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click" /> 
        <asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton4" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton5" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton6" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton7" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton8" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="LinkButton9" EventName="Click" />
    </triggers>



because i want to have partial refreshing at first i set the Visible of the ContentPlaceHolder3 to false and once the user click on the related link i just set it as true by code behind.
could you pleas guide me how to load this king of aspx page at the masterpage.
Appreciate your consideration.



because i want to have partial refreshing at first i set the Visible of the ContentPlaceHolder3 to false and once the user click on the related link i just set it as true by code behind.
could you pleas guide me how to load this king of aspx page at the masterpage.
Appreciate your consideration.

推荐答案

i have solved this problem using User Control method.
now i have put content of the page inside uc1 and used the below code to call the page at the update panel:

i have solved this problem using User Control method.
now i have put content of the page inside uc1 and used the below code to call the page at the update panel:

<uc1:WebUserControl ID="WebUserControl1"  runat="server" Visible="false" />





once the user click on the related button i just change the Visible option to True.
i hope it would be helpful.





once the user click on the related button i just change the Visible option to True.
i hope it would be helpful.


这篇关于加载aspx页面,该页面包括包含Updatepanel的母版页中的listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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