加载包含Updatepanel的主页中包含listview的aspx页面

时间:2011-12-07 08:14:24

标签: c# asp.net asp.net-ajax updatepanel master-pages

实际上我正在使用ASP.net和C#开发模板 正如你在下面的图片中看到的,我的模板包括3部分,我想要部分刷新,这就是我使用updatepanel的原因。 在此模板中,一旦用户点击顶部菜单,它将刷新侧边菜单,然后当用户点击侧边菜单时,它将刷新中间内容页面。 我在这种情况下使用母版页因为我要在中间内容页面加载aspx页面。根据我的代码,正常页面可以轻松加载到这个部分但是当我想加载包含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>
                        &nbsp;
                    </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>

它没有显示页面。我在这个listview页面后面的代码如下:

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

我加载此页面的母版页代码如下:

<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">
    </asp:ContentPlaceHolder>

</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>
</asp:UpdatePanel>

因为我想首先部分刷新我将ContentPlaceHolder3的Visible设置为false,一旦用户点击相关链接,我只需将代码设置为true即可。 Sample Page 你能请求我指导如何在母版页上加载这种aspx页面。 感谢您的考虑。

1 个答案:

答案 0 :(得分:0)

我使用用户控制方法解决了这个问题 现在我已将页面内容放在uc1中,并使用以下代码在更新面板上调用页面:

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

一旦用户点击相关按钮,我只需将Visible选项更改为True 我希望它会有所帮助。