如何使用TemplateField在GridView中隐藏TEXTBOX

时间:2013-05-22 17:39:02

标签: c# asp.net gridview

这是我的编码

我想在点击SEARCH按钮后在if条件中隐藏“txtQtyOrder10”。当我试图从part.aspx.cs文件访问时,它显示错误信息。

我不知道如何才能访问hide文本字段。

这是一些编码,用于参考我正在尝试做的事情。

任何帮助都将受到赞赏。

part.aspx

<asp:GridView ID="gvOutput" runat="server" AutoGenerateColumns="False" 
   BackColor="Transparent" AllowSorting="True"
   BorderColor="Navy" BorderWidth="2px" GridLines="None" OnSorting="OnSort" 
   OnRowCreated="OnRowCreated"
   OnSelectedIndexChanging="gvOutputSelectedIndexChanging" CellPadding="2" 
   DataKeyNames="LineID"
   AllowPaging="True" PageSize="50" OnPageIndexChanging="gvOutput_PageIndexChanging" 
   OnDataBound="OnDataBound" SelectedIndex="1">
   <Columns>
       <asp:TemplateField HeaderText="Select">
           <HeaderTemplate>
               <input id="chkAll" onclick="SelectAllCheckboxes(this);" 
               runat="server" type="checkbox" />
           </HeaderTemplate>
           <ItemTemplate>
               <asp:CheckBox ID="chkSelect" runat="server" 
               Checked='<%# Eval("[Select]") %>'/>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField SortExpression="PROD80" HeaderText="Number">
           <ItemTemplate>
               <asp:LinkButton ID="lnkPart" runat="server" CommandName="Select" 
               Text='<%# Bind("PROD80") %>'></asp:LinkButton>    
           </ItemTemplate>
           <ControlStyle CssClass="grid" />
       </asp:TemplateField>
       <asp:BoundField DataField="DESC80" HeaderText="Description" 
          SortExpression="DESC80" />
       <asp:BoundField DataField="VEND80" HeaderText="Vendor" 
          SortExpression="VEND80" />
       <asp:BoundField DataField="BUYE80" HeaderText="Planner" 
          SortExpression="BUYE80" />
       <asp:BoundField DataField="PMIUM" HeaderText="UOM" 
          SortExpression="PMIUM" />

       <asp:TemplateField HeaderText="Order 10">
           <ItemTemplate>
               <asp:TextBox ID="txtQtyOrder10" runat="server" 
               Text=<%#Bind("QTYORDER10")%> onfocus="this.select()" 
               CssClass="textbox" Height="13px" Width="60px" />
               <ajaxToolkit:FilteredTextBoxExtender ID="ftbeQty10" runat="server" 
                   TargetControlID="txtQtyOrder10" FilterType="Numbers">
               </ajaxToolkit:FilteredTextBoxExtender>
           </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Order 50">
           <ItemTemplate>
               <asp:TextBox ID="txtQtyOrder50" runat="server" 
               Text=<%#Bind("QTYORDER50")%> onfocus="this.select()" 
               CssClass="textbox" Height="13px" Width="60px" />
               <ajaxToolkit:FilteredTextBoxExtender ID="ftbeQty50" runat="server" 
                   TargetControlID="txtQtyOrder50" FilterType="Numbers">
               </ajaxToolkit:FilteredTextBoxExtender>
           </ItemTemplate>
       </asp:TemplateField>                                       
   </Columns>
   <HeaderStyle CssClass="gvOutputHeader" />
   <RowStyle ForeColor="Navy" Height="15px" Font-Size="Small" />
   <SelectedRowStyle BackColor="White" />
   <PagerSettings Position="TopAndBottom" />
</asp:GridView> 

part.aspx.cs

 protected void cmdSearch_Click(object sender, EventArgs e)
    {   //if user clicks on 'Below X Weeks' or 'Below X Weeks w/o PO', 
        //they must enter  a value in txt box.
        if ((!rdBelowXWeeks.Checked || txtWeek.Text != string.Empty) &&     
           (!rdBelowXWeeksWPO.Checked || txtWeek.Text != string.Empty))
    {
        try
        {
            //clear data of a previous search 
            ClearResultsScreen();
            ClearDetailsScreen();

            //get radio button value selected by user (default is all)
            if (rdAll.Checked)
                search.SearchType = rdAll.Value;
            else if (rdBelowTrigger.Checked)
                search.SearchType = rdBelowTrigger.Value;
            else if (rdAboveTrigger.Checked)
                search.SearchType = rdAboveTrigger.Value;
            else if (rdBelowSafety.Checked)
                search.SearchType = rdBelowSafety.Value;
            else if (rdBelowXWeeksWPO.Checked)
                search.SearchType = rdBelowXWeeksWPO.Value




     //*****WOULD LIKE TO HIDE from HERE *************//







        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    else
    {
        lblErrMessage.Text = "ERROR: Number of weeks is required";
        lblErrMessage.Visible = true;
    }

}//end cmdSearch_Click

1 个答案:

答案 0 :(得分:0)

只有你可以隐藏或显示网格内的控件在rowDataBound或任何网格事件中。

请尝试以下代码:

    protected void gvOutput_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        try
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {

            //get radio button value selected by user (default is all)
            if (rdAll.Checked)
                search.SearchType = rdAll.Value;
            else if (rdBelowTrigger.Checked)
                search.SearchType = rdBelowTrigger.Value;
            else if (rdAboveTrigger.Checked)
                search.SearchType = rdAboveTrigger.Value;
            else if (rdBelowSafety.Checked)
                search.SearchType = rdBelowSafety.Value;
            else if (rdBelowXWeeksWPO.Checked)
                search.SearchType = rdBelowXWeeksWPO.Value

              //Hide the Textbox
               TextBox txtQtyOrder10 = (TextBox)e.Row.FindControl("txtQtyOrder10");
               txtQtyOrder10.Visible = false;

            }
        }
        catch (Exception ex)
        {
            LoggingComponent.Instance.LogMessage(bo.Enums.LoggingType.Error, DateTime.Now, ex.Message, bo.Enums.Module.Order, "gvProductsAdded_RowDataBound", bo.Enums.ApplicationLevel.FrontEnd, "Error en los Productos a Despachar");
            DisplayMessage(GetGlobalResourceObject("Messages", "UnknowError").ToString(), bo.Enums.MessageType.Error);
        }


    }

我希望能帮到你。

干杯