Gagan Marwah

Gagan Marwah

  • NA
  • 11
  • 5.7k

AutoPostBack DropDownLists not Refreshing Gridview

Sep 11 2017 1:33 PM
Hi, I have multiple drop down list which are autopostback. When I select a value in drop down list, the GridView does not refresh automatically. The data only refreshes when I click on next/previous page of GridView. What do I need to do to fix my code?
 
ASPX Page:
  1. Year: <asp:DropDownList ID="DropDownYear" runat="server" AutoPostBack="True" DataSourceID="CommentsYearMonth" DataTextField="METRIC_YEAR"  DataValueField="METRIC_YEAR" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownYear_SelectedIndexChanged">  
  2.             <asp:ListItem Text="Select year" Value="" />   
  3.         </asp:DropDownList>  
  4.             Month:  
  5.         <asp:DropDownList ID="DropDownMonth" runat="server" AutoPostBack="True" DataSourceID="CommentsMonth" DataTextField="METRIC_MONTH_TXT" DataValueField="METRIC_MONTH" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownMonth_SelectedIndexChanged">  
  6.             <asp:ListItem Text="Select month" Value="" />  
  7.         </asp:DropDownList>  
  8.                 Tab:  
  9.         <asp:DropDownList ID="DropDownTabName" runat="server" AutoPostBack="True" DataSourceID="CommentsTabName" DataTextField="TAB_NAME" DataValueField="TAB_NAME" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownTabName_SelectedIndexChanged">  
  10.             <asp:ListItem Text="Select tab name" Value="" />  
  11.         </asp:DropDownList>  
  12.                 Section:  
  13.         <asp:DropDownList ID="DropDownSectionName" runat="server" AutoPostBack="True" DataSourceID="CommentsSectionName" DataTextField="SECTION_NAME" DataValueField="SECTION_NAME" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownSectionName_SelectedIndexChanged">  
  14.             <asp:ListItem Text="Select section name" Value="" />  
  15.         </asp:DropDownList>  
  16.             <br />  
  17.             <br/>  
  18.         </fieldset>  
  19.         </div>  
  20.         <asp:SqlDataSource ID="CommentsSectionName" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT SECTION_NAME FROM CHNGMETRICS.VW_BRD_COMMENT"></asp:SqlDataSource>  
  21.          <asp:SqlDataSource ID="CommentsMonth" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT METRIC_MONTH_TXT, METRIC_MONTH FROM CHNGMETRICS.VW_BRD_COMMENT ORDER BY METRIC_MONTH"></asp:SqlDataSource>  
  22.         <asp:SqlDataSource ID="CommentsYearMonth" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT METRIC_YEAR FROM CHNGMETRICS.VW_BRD_COMMENT ORDER BY METRIC_YEAR"></asp:SqlDataSource>  
  23.         <br />  
  24.         <br />  
  25.         <asp:SqlDataSource ID="CommentsTabName" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT TAB_NAME FROM CHNGMETRICS.VW_BRD_COMMENT"></asp:SqlDataSource>  
  26.         <br />  
  27.   
  28.         <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"  ForeColor="#333333" GridLines="None" PageSize="20"   
  29.             OnPageIndexChanging="GridView1_PageIndexChanging" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"  OnSorting="GridView1_Sorting">  
  30.             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  31.             <Columns>  
  32.   
  33.   
  34.                 <%-- COMMENT ID --%>  
  35.                 <asp:TemplateField>  
  36.                     <ItemTemplate>  
  37.                         <asp:Label  Visible="false" ID="lblID" runat="server" Text='<%# Bind("BRD_COMMENT_ID") %>'>  
  38.   
  39.                         </asp:Label>  
  40.                     </ItemTemplate>  
  41.                     <EditItemTemplate>  
  42.                         <asp:Label ID="txtID" Visible="false" runat="server" Text='<%# Bind("BRD_COMMENT_ID") %>'>  
  43.   
  44.                         </asp:Label>  
  45.                     </EditItemTemplate>  
  46.                 </asp:TemplateField>  
  47.   
  48.                 <%-- COMMENT YEAR --%>  
  49.                 <asp:TemplateField HeaderText="Year">  
  50.                     <ItemTemplate>  
  51.                         <asp:Label ID="lblCommentYear" Visible="true" runat="server" Text='<%# Bind("METRIC_YEAR") %>'>  
  52.   
  53.                         </asp:Label>  
  54.                     </ItemTemplate>  
  55.                     <EditItemTemplate>  
  56.                         <asp:Label ID="txtCommentYear" runat="server" Text='<%# Bind("METRIC_YEAR") %>'>  
  57.   
  58.                         </asp:Label>  
  59.                     </EditItemTemplate>  
  60.                 </asp:TemplateField>  
  61.   
  62.                 <%-- COMMENT MONTH --%>  
  63.                 <asp:TemplateField HeaderText="Month">  
  64.                     <ItemTemplate>  
  65.                         <asp:Label ID="lblCommentMonth" Visible="true" runat="server" Text='<%# Bind("METRIC_MONTH_TXT") %>'>  
  66.   
  67.                         </asp:Label>  
  68.                     </ItemTemplate>  
  69.                     <EditItemTemplate>  
  70.                         <asp:Label ID="txtCommentMonth" runat="server" Text='<%# Bind("METRIC_MONTH_TXT") %>'>  
  71.   
  72.                         </asp:Label>  
  73.                     </EditItemTemplate>  
  74.                 </asp:TemplateField>  
  75.   
  76.                 <%-- COMMENT TAB --%>  
  77.                 <asp:TemplateField HeaderText="Tab">  
  78.                     <ItemTemplate>  
  79.                         <asp:Label ID="lblCommentTab" Visible="true" runat="server" Text='<%# Bind("TAB_NAME") %>'>  
  80.   
  81.                         </asp:Label>  
  82.                     </ItemTemplate>  
  83.                     <EditItemTemplate>  
  84.                         <asp:Label ID="txtCommentTab" runat="server" Text='<%# Bind("TAB_NAME") %>'>  
  85.   
  86.                         </asp:Label>  
  87.                     </EditItemTemplate>  
  88.                 </asp:TemplateField>  
  89.   
  90.                 <%-- COMMENT SECTION --%>  
  91.                 <asp:TemplateField HeaderText="Section">  
  92.                     <ItemTemplate>  
  93.                         <asp:Label ID="lblCommentSection" Visible="true" runat="server" Text='<%# Bind("SECTION_NAME") %>'>  
  94.   
  95.                         </asp:Label>  
  96.                     </ItemTemplate>  
  97.                     <EditItemTemplate>  
  98.                         <asp:Label ID="txtCommentSection" runat="server" Text='<%# Bind("SECTION_NAME") %>'>  
  99.   
  100.                         </asp:Label>  
  101.                     </EditItemTemplate>  
  102.                 </asp:TemplateField>  
  103.   
  104.                 <%-- COMMENT ORDER --%>  
  105.                 <asp:TemplateField HeaderText="Order" SortExpression="COMMENT_ORDER">  
  106.                     <ItemTemplate>  
  107.                         <asp:Label ID="lblCommentOrder" runat="server" Text='<%# Bind("COMMENT_ORDER") %>'>  
  108.   
  109.                         </asp:Label>  
  110.                     </ItemTemplate>  
  111.                     <EditItemTemplate>  
  112.                         <asp:TextBox ID="txtCommentOrder"  Width="20px" runat="server" Text='<%# Bind("COMMENT_ORDER") %>'></asp:TextBox>  
  113.                         <asp:RequiredFieldValidator ID="rfvUpdateCommentOrder" ValidationGroup="UPDATE" runat="server"   
  114.                         ErrorMessage="Comment order is required." ForeColor="Brown"  Display="Dynamic" ControlToValidate="txtCommentOrder"></asp:RequiredFieldValidator>  
  115.                     </EditItemTemplate>  
  116.                 </asp:TemplateField>  
  117.                   
  118.                 <%-- COMMENT TEXT --%>  
  119.                 <asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="Comments">  
  120.                     <ItemTemplate>  
  121.                         <asp:Label ID="lblComment" runat="server" Text='<%# Bind("COMMENT_TEXT") %>'>  
  122.   
  123.                         </asp:Label>  
  124.                     </ItemTemplate>  
  125.                     <EditItemTemplate>  
  126.                         <asp:TextBox ID="txtEditComment"  TextMode="MultiLine" Width="400px" Height="30px" MaxLength="300" runat="server" Text='<%# Bind("COMMENT_TEXT") %>'></asp:TextBox>  
  127.                         <asp:RequiredFieldValidator ID="rfvUpdateCommentText" ValidationGroup="UPDATE" runat="server"   
  128.                         ErrorMessage="Please enter a comment" ForeColor="brown" ControlToValidate="txtEditComment"></asp:RequiredFieldValidator>  
  129.                     </EditItemTemplate>  
  130.                 </asp:TemplateField>  
  131.                 <asp:TemplateField>  
  132.                     <ItemTemplate>  
  133.                         <asp:LinkButton ID ="lnkEdit" runat="server" CommandName="Edit" Text ="Edit"></asp:LinkButton>  
  134.                         <asp:LinkButton OnClientClick="return confirm('Are you sure you want to delete this comment?');" ID ="lnkDelete" runat="server" CommandName="Delete" Text ="Delete"></asp:LinkButton>  
  135.                     </ItemTemplate>  
  136.                     <EditItemTemplate>     
  137.                        <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>     
  138.                        <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>     
  139.                     </EditItemTemplate>  
  140.                 </asp:TemplateField>  
  141.                  
  142.             </Columns>  
  143.             <EditRowStyle BackColor="#999999" />  
  144.             <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  145.             <HeaderStyle  HorizontalAlign="Left" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  146.             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  147.             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  148.             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  149.             <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  150.             <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  151.             <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  152.             <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  153.         </asp:GridView>  
 
 
ASPX.CS Page:
  1. namespace ChngMgmt_Comments_and_Legends  
  2. {  
  3.     public partial class Comments : System.Web.UI.Page  
  4.     {  
  5.         private static readonly ILog logger = LogManager.GetLogger("LoggerName");  
  6.         protected void Page_Load(object sender, EventArgs e)  
  7.         {  
  8.            if(!IsPostBack)  
  9.             {  
  10.                GridView1.DataSource = RefreshGrid();  
  11.                GridView1.DataBind();  
  12.             }  
  13.         }  
  14.           
  15.   
  16.         public DataTable RefreshGrid()  
  17.         {  
  18.             //Gets all the data in grid view for the following columns  
  19.             String queryStr= "SELECT BRD_COMMENT_ID, METRIC_YEAR, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT FROM CHNGMETRICS.VW_BRD_COMMENT WHERE 1=1";  
  20.               
  21.             //If Year drop down is not null the data gets filtered by year  
  22.             if (DropDownYear.SelectedValue != "")  
  23.             {  
  24.                 queryStr += " AND METRIC_YEAR = :METRIC_YEAR";  
  25.                
  26.             }  
  27.             //If Month drop down in not null the data gets filtered by month.  
  28.             if (DropDownMonth.SelectedValue != "")  
  29.             {  
  30.                 queryStr += " AND METRIC_MONTH = :METRIC_MONTH";  
  31.   
  32.             }  
  33.             //if section drop down is not null the data gets filtered by section name  
  34.             if (DropDownSectionName.SelectedValue != "")  
  35.             {  
  36.                 queryStr += " AND SECTION_NAME = :SECTION_NAME";  
  37.   
  38.             }  
  39.             //if TAB drop down is not null the data gets filtered by TAB Name  
  40.             if (DropDownTabName.SelectedValue != "")  
  41.             {  
  42.                 queryStr += " AND TAB_NAME = :TAB_NAME";  
  43.   
  44.             }  
  45.   
  46.             //Added parameters for Year,Month,Section,Tab drop downs  
  47.             OracleCommand command = new OracleCommand(queryStr);  
  48.             if (DropDownYear.SelectedValue != "")  
  49.             {  
  50.                 command.Parameters.AddWithValue("METRIC_YEAR",DropDownYear.SelectedValue);  
  51.   
  52.             }  
  53.             if (DropDownMonth.SelectedValue != "")  
  54.             {  
  55.                 command.Parameters.AddWithValue("METRIC_MONTH", DropDownMonth.SelectedValue);  
  56.   
  57.             }  
  58.             if (DropDownSectionName.SelectedValue != "")  
  59.             {  
  60.                 command.Parameters.AddWithValue("SECTION_NAME", DropDownSectionName.SelectedValue);  
  61.   
  62.             }  
  63.             if (DropDownTabName.SelectedValue != "")  
  64.             {  
  65.                 command.Parameters.AddWithValue("TAB_NAME", DropDownTabName.SelectedValue);  
  66.   
  67.             }  
  68.               
  69.   
  70.             DataTable GridComments = Utility.GetData(command);  
  71.             return GridComments;  
  72.   
  73.   
  74.         }  
  75.         //Drop Downs are auto post back so each time user selects a selection the Grid View needs to refresh  
  76.         protected void DropDownYear_SelectedIndexChanged(object sender, EventArgs e)  
  77.         {  
  78.             RefreshGrid();  
  79.         }  
  80.   
  81.         protected void DropDownMonth_SelectedIndexChanged(object sender, EventArgs e)  
  82.         {  
  83.             RefreshGrid();  
  84.         }  
  85.         protected void DropDownSectionName_SelectedIndexChanged(object sender, EventArgs e)  
  86.         {  
  87.             RefreshGrid();  
  88.         }  
  89.   
  90.         protected void DropDownTabName_SelectedIndexChanged(object sender, EventArgs e)  
  91.         {  
  92.             RefreshGrid();  
  93.         }  
  94.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  95.         {  
  96.             GridView1.EditIndex = e.NewEditIndex;  
  97.             RefreshGrid();  
  98.         }  
  99.         protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  100.         {  
  101.             GridView1.PageIndex = e.NewPageIndex;  
  102.             DataTable dt = RefreshGrid();  
  103.             if (ViewState["Sort"] != null)  
  104.             {  
  105.                 dt.DefaultView.Sort = (string)ViewState["Sort"];  
  106.             }  
  107.             GridView1.DataSource = dt;  
  108.             GridView1.DataBind();  
  109.         }  
  110.   
  111.         // Grid View updating is enabled so users can edit Comments and order  
  112.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  113.         {  
  114.             logger.Debug("Debugging update statement");  
  115.             logger.Error("Catching error(s) while updating data");  
  116.             if (Page.IsValid)  
  117.             {  
  118.                 Label id = GridView1.Rows[e.RowIndex].FindControl("txtID") as Label;  
  119.                 TextBox comment = GridView1.Rows[e.RowIndex].FindControl("txtEditComment") as TextBox;  
  120.                 TextBox order = GridView1.Rows[e.RowIndex].FindControl("txtCommentOrder") as TextBox;  
  121.                 var updateQuery = ("UPDATE CHNGMETRICS.VW_BRD_COMMENT SET COMMENT_ORDER =  :COMMENTORDER , COMMENT_TEXT = :COMMENTTEXT WHERE BRD_COMMENT_ID = :BRDCOMMENTID ");  
  122.                 OracleCommand command = new OracleCommand(updateQuery);  
  123.                   
  124.                 //Add PARAMETERS FOR UPDATE STATEMENT  
  125.                 command.Parameters.Add(new OracleParameter("COMMENTORDER", order.Text));  
  126.                 command.Parameters.Add(new OracleParameter("COMMENTTEXT", comment.Text));  
  127.                 command.Parameters.Add(new OracleParameter("BRDCOMMENTID", Convert.ToInt32(id.Text)));  
  128.                 GridView1.EditIndex = -1;  
  129.                 Utility.Exec(command);  
  130.                 RefreshGrid();  
  131.                 //Year  
  132.                 DropDownYear.Items.Clear();  
  133.                 DropDownYear.DataBind();  
  134.                 DropDownYear.ClearSelection();  
  135.                 DropDownYear.Items.Insert(0, new ListItem("Select year"""));  
  136.                 DropDownYear.SelectedIndex = -1;  
  137.                 //Month  
  138.                 DropDownMonth.Items.Clear();  
  139.                 DropDownMonth.DataBind();  
  140.                 DropDownMonth.ClearSelection();  
  141.                 DropDownMonth.Items.Insert(0, new ListItem("Select month"""));  
  142.                 DropDownMonth.SelectedIndex = -1;  
  143.                 //Tab  
  144.                 DropDownTabName.Items.Clear();  
  145.                 DropDownTabName.DataBind();  
  146.                 DropDownTabName.ClearSelection();  
  147.                 DropDownTabName.Items.Insert(0, new ListItem("Select tab name"""));  
  148.                 DropDownTabName.SelectedIndex = -1;  
  149.                 //Section  
  150.                 DropDownSectionName.Items.Clear();  
  151.                 DropDownSectionName.DataBind();  
  152.                 DropDownSectionName.ClearSelection();  
  153.                 DropDownSectionName.Items.Insert(0, new ListItem("Select section name"""));  
  154.                 DropDownSectionName.SelectedIndex = -1;  
  155.             }  
  156.               
  157.         }  
  158.   
  159.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  160.         {  
  161.             GridView1.EditIndex = -1;  
  162.             RefreshGrid();  
  163.         }  
  164.           
  165.         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  166.         {  
  167.             logger.Debug("Debugging delete statement");  
  168.             logger.Error("Catching error(s) while deleting data");  
  169.             Label id = GridView1.Rows[e.RowIndex].FindControl("lblID") as Label;  
  170.             var deleteQuery = ("DELETE FROM CHNGMETRICS.VW_BRD_COMMENT WHERE BRD_COMMENT_ID = :BRDCOMMENTID ");  
  171.             OracleCommand command = new OracleCommand(deleteQuery);  
  172.             //Add PARAMETERS FOR DELETE STATEMENT  
  173.             command.Parameters.Add(new OracleParameter("BRDCOMMENTID", Convert.ToInt32(id.Text)));  
  174.             Utility.Exec(command);  
  175.             RefreshGrid();  
  176.             //Year  
  177.             DropDownYear.Items.Clear();  
  178.             DropDownYear.DataBind();  
  179.             DropDownYear.ClearSelection();  
  180.             DropDownYear.Items.Insert(0, new ListItem("Select year"""));  
  181.             DropDownYear.SelectedIndex = -1;  
  182.             //Month  
  183.             DropDownMonth.Items.Clear();  
  184.             DropDownMonth.DataBind();  
  185.             DropDownMonth.ClearSelection();  
  186.             DropDownMonth.Items.Insert(0, new ListItem("Select month"""));  
  187.             DropDownMonth.SelectedIndex = -1;  
  188.             //Tab  
  189.             DropDownTabName.Items.Clear();  
  190.             DropDownTabName.DataBind();  
  191.             DropDownTabName.ClearSelection();  
  192.             DropDownTabName.Items.Insert(0, new ListItem("Select tab name"""));  
  193.             DropDownTabName.SelectedIndex = -1;  
  194.             //Section  
  195.             DropDownSectionName.Items.Clear();  
  196.             DropDownSectionName.DataBind();  
  197.             DropDownSectionName.ClearSelection();  
  198.             DropDownSectionName.Items.Insert(0, new ListItem("Select section name"""));  
  199.             DropDownSectionName.SelectedIndex = -1;  
  200.   
  201.         }  
  202.         protected void btnAdd_Click(object sender, EventArgs e)  
  203.         {  
  204.             logger.Debug("Debugging insert statement");  
  205.             logger.Error("Catching error(s) while adding data");  
  206.   
  207.             int month;  
  208.             if (drpDownMonth.Text == "January")  
  209.             {  
  210.                 month = 01;  
  211.             }  
  212.             else if (drpDownMonth.Text == "February")  
  213.             {  
  214.                 month = 02;  
  215.             }  
  216.             else if (drpDownMonth.Text == "March")  
  217.             {  
  218.                 month = 03;  
  219.             }  
  220.             else if (drpDownMonth.Text == "April")  
  221.             {  
  222.                 month = 04;  
  223.             }  
  224.             else if (drpDownMonth.Text == "May")  
  225.             {  
  226.                 month = 05;  
  227.             }  
  228.             else if (drpDownMonth.Text == "June")  
  229.             {  
  230.                 month = 06;  
  231.             }  
  232.             else if (drpDownMonth.Text == "July")  
  233.             {  
  234.                 month = 07;  
  235.             }  
  236.             else if (drpDownMonth.Text == "August")  
  237.             {  
  238.                 month = 08;  
  239.             }  
  240.             else if (drpDownMonth.Text == "September")  
  241.             {  
  242.                 month = 09;  
  243.             }  
  244.             else if (drpDownMonth.Text == "October")  
  245.             {  
  246.                 month = 10;  
  247.             }  
  248.             else if (drpDownMonth.Text == "November")  
  249.             {  
  250.                 month = 11;  
  251.             }  
  252.             else { month = 12;}  
  253.   
  254.   
  255.             //var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT VALUES ('" + "', '" + "', '" + "', '" + "', '" +drpDownYear.Text+ "', '" +month+ "', '" +drpDownMonth.Text+ "', '" +drpDownTabName.Text+ "', '" +drpDownSectionName.Text+ "', '" +drpDownCommentOrder.Text+ "', '" +txtComment.Text+ "')");  
  256.             var insertQuery = ("INSERT INTO CHNGMETRICS.VW_BRD_COMMENT (METRIC_YEAR, METRIC_MONTH, METRIC_MONTH_TXT, TAB_NAME, SECTION_NAME, COMMENT_ORDER, COMMENT_TEXT) VALUES  (:YEAR, :MONTH_NUM, :MONTH_TEXT, :TAB_NAME, :SECTION_NAME, :COMMENT_ORDER, :COMMENT_TEXT)");  
  257.             OracleCommand command = new OracleCommand(insertQuery);  
  258.   
  259.             //ADD PARAMETERS FOR INSERT STATEMENT  
  260.             command.Parameters.Add(new OracleParameter("YEAR", drpDownYear.Text));  
  261.             command.Parameters.Add(new OracleParameter("MONTH_NUM", month));  
  262.             command.Parameters.Add(new OracleParameter("MONTH_TEXT", drpDownMonth.Text));  
  263.             command.Parameters.Add(new OracleParameter("TAB_NAME", drpDownTabName.Text));  
  264.             command.Parameters.Add(new OracleParameter("SECTION_NAME", drpDownSectionName.Text));  
  265.             command.Parameters.Add(new OracleParameter("COMMENT_ORDER", txtCmntOrder.Text));  
  266.             command.Parameters.Add(new OracleParameter("COMMENT_TEXT", txtComment.Text));  
  267.   
  268.   
  269.             Utility.Exec(command);  
  270.             RefreshGrid();  
  271.   
  272.             //Year  
  273.             DropDownYear.Items.Clear();  
  274.             DropDownYear.DataBind();  
  275.             DropDownYear.ClearSelection();  
  276.             DropDownYear.Items.Insert(0, new ListItem("Select year"""));  
  277.             DropDownYear.SelectedIndex = -1;  
  278.             //Month  
  279.             DropDownMonth.Items.Clear();  
  280.             DropDownMonth.DataBind();  
  281.             DropDownMonth.ClearSelection();  
  282.             DropDownMonth.Items.Insert(0, new ListItem("Select month"""));  
  283.             DropDownMonth.SelectedIndex = -1;  
  284.             //Tab  
  285.             DropDownTabName.Items.Clear();  
  286.             DropDownTabName.DataBind();  
  287.             DropDownTabName.ClearSelection();  
  288.             DropDownTabName.Items.Insert(0, new ListItem("Select tab name"""));  
  289.             DropDownTabName.SelectedIndex = -1;  
  290.             //Section  
  291.             DropDownSectionName.Items.Clear();  
  292.             DropDownSectionName.DataBind();  
  293.             DropDownSectionName.ClearSelection();  
  294.             DropDownSectionName.Items.Insert(0, new ListItem("Select section name"""));  
  295.             DropDownSectionName.SelectedIndex = -1;  
  296.               
  297.             txtComment.Text = String.Empty;  
  298.             txtCmntOrder.Text = String.Empty;  
  299.             drpDownYear.Text = "-1";  
  300.             drpDownMonth.Text = "-1";  
  301.             drpDownTabName.Text = "-1";  
  302.             drpDownSectionName.Items.Clear();  
  303.             drpDownSectionName.Items.Insert(0, new ListItem("Select section name""-1"));  
  304.             drpDownSectionName.Items.Add(new ListItem("Performance""Performance"));  
  305.             drpDownSectionName.Items.Add(new ListItem("Compliance""Compliance"));  
  306.             drpDownSectionName.Items.Add(new ListItem("Main""Main"));  
  307.             drpDownSectionName.Text = "-1";  
  308.             Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts","<script>alert('Your comment was successfully added.');</script>");  
  309.         }  
  310.   
  311.         protected void drpDownTabName_SelectedIndexChanged(object sender, EventArgs e)  
  312.         {  
  313.             drpDownSectionName.Items.Clear();  
  314.   
  315.             if (drpDownTabName.SelectedItem.Text == "Watch List")  
  316.             {  
  317.                 drpDownSectionName.Items.Add(new ListItem("Performance""Performance"));  
  318.                 drpDownSectionName.Items.Add(new ListItem("Compliance""Compliance"));                 
  319.             }  
  320.             else if ((drpDownTabName.SelectedValue == "General Stats") || (drpDownTabName.SelectedValue == "Planning") || (drpDownTabName.SelectedValue == "CAB"))  
  321.             {  
  322.                 drpDownSectionName.Items.Add(new ListItem("Main""Main"));  
  323.             }  
  324.         }  
  325.   
  326.   
  327.         protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)  
  328.         {  
  329.             DataTable dt = RefreshGrid();  
  330.             //Sort the data.  
  331.             var sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);  
  332.             dt.DefaultView.Sort = sort;  
  333.             ViewState["Sort"] = sort;  
  334.             GridView1.DataSource = dt.DefaultView;  
  335.             GridView1.DataBind();  
  336.   
  337.         }  
  338.   
  339.         private string GetSortDirection(string column)  
  340.         {  
  341.   
  342.             // By default, set the sort direction to ascending.  
  343.             string sortDirection = "ASC";  
  344.   
  345.             // Retrieve the last column that was sorted.  
  346.             string sortExpression = ViewState["SortExpression"] as string;  
  347.   
  348.             if (sortExpression != null)  
  349.             {  
  350.                 // Check if the same column is being sorted.  
  351.                 // Otherwise, the default value can be returned.  
  352.                 if (sortExpression == column)  
  353.                 {  
  354.                     string lastDirection = ViewState["SortDirection"] as string;  
  355.                     if ((lastDirection != null) && (lastDirection == "ASC"))  
  356.                     {  
  357.                         sortDirection = "DESC";  
  358.                     }  
  359.                 }  
  360.             }  
  361.   
  362.             // Save new values in ViewState.  
  363.             ViewState["SortDirection"] = sortDirection;  
  364.             ViewState["SortExpression"] = column;  
  365.   
  366.             return sortDirection;  
  367.         }  
  368.     }  
  369. }  

Answers (2)