Solved

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

Posted on 2012-03-29
4
642 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

862 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

23 Experts available now in Live!

Get 1:1 Help Now