Solved

asp.net, gridview, c#, hide text box

Posted on 2012-03-29
4
638 Views
Last Modified: 2012-03-29
Hi,

I have a gridview in which I display results like so

aspx
<asp:GridView ID="grdBuy" runat="server" AutoGenerateColumns="false" OnRowDataBound="RowDataBound"
                        AllowPaging="true" PageSize="5" PagerStyle-HorizontalAlign="Right" OnPageIndexChanging="OnPageIndexChanged"
                        Width="500px" GridLines="None" CssClass="GridResultsSaleorLet">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <table cellpadding="5px" class="PropertiesSaleorLet">
                                               </td>
                                            <td>
                                                <asp:MaskedEditExtender ID="MaskedEditExtender1" TargetControlID="txtPrice" Mask="9, 999, 999"
                                                    MaskType="Number" DisplayMoney="Left" runat="server">
                                                </asp:MaskedEditExtender>
                                                <div class="PoundSymbolforPrice">
                                                    <asp:TextBox ID="txtPrice" runat="server" Visible="true" Width="100px" Text='<%# String.Format("{0}", "£", Eval("Price")) %>'
                                                        CssClass="PriceStyle"></asp:TextBox>
                                                    <asp:Label ID="lblTypeOfPrice" runat="server" CssClass="TypeofPrice" Text='<%#DataBinder.Eval(Container.DataItem,"TypeOfPrice") %>'></asp:Label>
                                                </div>
                                            </td>
                                        </tr>

Open in new window


and c# code

GetPropertyInfoBAL GetPropertiesForSale = new GetPropertyInfoBAL();
            DataSet dsGetPropertiesForSale = new DataSet();
            dsGetPropertiesForSale = GetPropertiesForSale.dsGetPropertiesForSaleBAL(sPostCodetoSearchinWithLike, sSaleorLet);


            int icountTotalProperties = dsGetPropertiesForSale.Tables["Properties"].Rows.Count;

            if (icountTotalProperties >= 1)
            {

                grdBuy.DataSource = dsGetPropertiesForSale.Tables["Properties"];
                grdBuy.DataBind();

                int iCountrowsingrid = grdBuy.Rows.Count;
                int i;

                for (i = 0; i < iCountrowsingrid; i++)
                {
                    TextBox txtPrice = (TextBox)grdBuy.Rows[i].FindControl("txtPrice");
                    Label lblTypeOfPrice = (Label)grdBuy.Rows[i].FindControl("lblTypeOfPrice");


                    if (lblTypeOfPrice.Text == "POA")
                    {
                        txtPrice.Visible = false;
                                            }
                    else
                    {
                        txtPrice.Visible = true;
                    }

                }

Open in new window



It displays everything fine no problem, but my problem is with the IF statement I want to check if the Labels text is POA then visible=false for that box for that row.

My problem is that set visible to false for all text boxes for all rows

please help

Thanks,
0
Comment
Question by:R8VI
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
Rose Babu earned 500 total points
ID: 37781009
use RowDataBound event of the grid. and do the condition there.
protected void grdBuy_RowDataBound(object sender, GridViewRowEventArgs e)
{
	if (e.Row.RowType == DataControlRowType.DataRow)
	{
		Label lblTypeOfPrice = ((Label)e.Row.FindControl("lblTypeOfPrice"));
		TextBox txtPrice = ((TextBox)e.Row.FindControl("txtPrice"));

		if (lblTypeOfPrice.Text == "POA")
		{
			txtPrice.Visible = false;
		}
		else
		{
			txtPrice.Visible = true;
		}
		
	}
}

Open in new window

Try this. it will work.
0
 

Author Comment

by:R8VI
ID: 37781034
Hi,

I tried your suggestion still doesnt work please help

 protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {

            int iCountPages = grdBuy.PageCount;
            int iCurrentPage = grdBuy.PageIndex + 1;
            lblTotalPageCount.Text = "Page " + Convert.ToString(iCurrentPage) + " of " + Convert.ToString(iCountPages);
            lblSaveProperty.Visible = false;


            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtPrice = (TextBox)e.Row.FindControl("txtPrice");
                Label lblTypeOfPrice = (Label)e.Row.FindControl("lblTypeOfPrice");


                if (lblTypeOfPrice.Text == "POA")
                {
                    txtPrice.Visible = false;
                }
                else
                {
                    txtPrice.Visible = true;
                }

            }


        }

Open in new window



thanks

R8VI
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37781039
do it on client (jquery):

 $("#lblTypeOfPrice").each(function(){
      if($(this).text() == "POA"){
        $(this).parent().find("#txtPrice").hide();
      }else{
$(this).parent().find("#txtPrice").show();
}
    });
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 37781051
@R8VI

did u debug your code?

try first show/hide all txtPrice controls just to see your code is running:

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox txtPrice = (TextBox)e.Row.FindControl("txtPrice");
                Label lblTypeOfPrice = (Label)e.Row.FindControl("lblTypeOfPrice");

//run it once then change it to false and see if all txtPrice are hidden
txtPrice.Visible=true;
            }

if the first run txtPrice controls are all displayed and in the 2nd time they are all hidden then check if the IF statement is correct.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now