Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-29
4
Medium Priority
?
694 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
Rose Babu earned 2000 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:Meir Rivkin
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:Meir Rivkin
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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