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
Solved

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

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

840 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