2011-12-07 165 views
0

其实我正在开发一个模板使用ASP.net和C#。
正如你可以看到下面的图片,我的模板包括3部分,我想部分刷新,这就是为什么我使用updatepanel。
在这个模板中,一旦用户点击顶部菜单,它将刷新侧面菜单,然后当用户点击侧面菜单时,它将刷新中间内容页面。 我在这种情况下使用母版页,因为我打算在中间内容页面加载aspx页面。根据我的代码正常的页面很容易在这部分加载,但是当我要加载它的页面包括列表视图:加载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> 

它没有显示的页面。我的这个列表视图页面背后的代码如下:

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的值设为false,并且一旦用户点击相关链接,我只是通过代码将其设置为true。 Sample Page 你可以请求指导我如何在masterpage上加载这种aspx页面。 感谢您的考虑。

回答

0

我已经使用用户控制方法解决了这个问题。
现在我已经把里面UC1页面的内容,并用下面的代码在更新面板调用页面:

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

一旦上了相关按钮,用户点击我只是改变可见选项设置为True。
我希望这会有所帮助。