Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

831 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